decred / tinydecred

Python tools for Decred
ISC License
27 stars 14 forks source link

tests: several unit tests are too slow #157

Closed teknico closed 4 years ago

teknico commented 4 years ago

Several unit tests are too slow:

$ poetry run pytest --durations=10 tests/unit/
=== test session starts ===
platform linux -- Python 3.7.5, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
benchmark: 3.2.3 (defaults: timer=time.perf_counter disable_gc=False
    min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10
    warmup=False warmup_iterations=100000)
rootdir: ~/devel/decred/tinydecred/decred
plugins: benchmark-3.2.3, cov-2.8.1, pudb-0.7.0, profiling-1.7.0
collected 181 items

tests/unit/crypto/test_crypto.py ......
[...]
tests/unit/wallet/test_accounts.py ....

=== slowest 10 test durations ===
7.85s call     tests/unit/wallet/test_accounts.py::test_discover
2.13s call     tests/unit/wallet/test_accounts.py::test_account_manager
1.41s call     tests/unit/dcr/test_account.py::TestAccount::test_main
1.25s call     tests/unit/dcr/test_account.py::TestAccount::test_txsExistForKey
1.15s call     tests/unit/dcr/test_txscript.py::test_merge_scripts
1.07s call     tests/unit/wallet/test_accounts.py::test_change_addresses
1.00s call     tests/unit/dcr/test_rpc_unit.py::test_WebsocketClient
0.91s call     tests/unit/crypto/secp256k1/test_curve.py::test_scalar_mult_rand
0.61s call     tests/unit/dcr/test_blockchain_unit.py::test_discoverAddresses
0.40s call     tests/unit/dcr/test_account.py::TestAccount::test_gap_handling
=== 181 passed in 21.03s ===

The wallet/test_accounts.py::test_discover is especially slow. Profiling shows that it's due to many calls to the underlying secp256k1 code, written in pure Python.

Since we're not testing the secp256k1 code in the higher-level test, a workaround would be to monkeypatch the calls to ExtendedKey.child, making them return precomputed values.

A similar approach could be used in other slow tests.