Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit tests are slow #1214

Closed
azmeuk opened this issue Aug 12, 2023 · 2 comments · Fixed by #1215
Closed

unit tests are slow #1214

azmeuk opened this issue Aug 12, 2023 · 2 comments · Fixed by #1215

Comments

@azmeuk
Copy link
Contributor

azmeuk commented Aug 12, 2023

Testing the whole test suite takes 59s for the 118 tests on my computer (which is very capable).

With pytest-xdist and 8 CPUs this goes down to 17s (pytest -n auto)

pytest --durations 10 gives us those stats:

2.08s call     ihatemoney/tests/api_test.py::TestAPI::test_project
1.99s call     ihatemoney/tests/api_test.py::TestAPI::test_bills_with_calculation
1.97s call     ihatemoney/tests/history_test.py::TestHistory::test_clear_ip_records
1.88s call     ihatemoney/tests/api_test.py::TestAPI::test_member
1.77s call     ihatemoney/tests/api_test.py::TestAPI::test_bills
1.62s call     ihatemoney/tests/api_test.py::TestAPI::test_currencies
1.31s call     ihatemoney/tests/budget_test.py::TestBudget::test_edit_project
1.08s call     ihatemoney/tests/history_test.py::TestHistory::test_disable_clear_no_new_records
1.02s call     ihatemoney/tests/history_test.py::TestHistory::test_project_privacy_edit2
1.00s call     ihatemoney/tests/history_test.py::TestHistory::test_project_privacy_edit

Looking a bit more closely with pytest-profiling and snakeviz, I can see that the longest function call is werkzeug.security.check_password_hash from the need_auth decorator:

if project and check_password_hash(project.password, auth.password):

It seems to be a known issue, and one way to mitigate this is to generate lower security password hashes.

By using method='pbkdf2:sha1:1000' in generate_password_hash tests run in 24s (instead of 59s) or 8s with xdist (instead of 17s).

I suggest to use a faster and less secure hash algorithm to run unit tests, but still use the default algorithm in other environment. I think a simple solution would be to add two configuration parameters PASSWORD_HASH_METHOD and PASSWORD_HASH_SALT that would be passed as arguments in generate_password_hash. By default those vars would be unset, except in the unit test environment where they would have convenient values.

I would volunteer to provide a PR for this.

What do you think?

@zorun
Copy link
Collaborator

zorun commented Aug 12, 2023

Good catch, looks good to me, go for it!

@almet
Copy link
Member

almet commented Aug 12, 2023

Thanks for the detailed explanation. It's very useful to learn about pytest-xdist and pytest-profiling :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants