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

Use instance=True in create_autospec #2434

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

samtygier-stfc
Copy link
Collaborator

@samtygier-stfc samtygier-stfc commented Dec 11, 2024

Save time, by preventing create_autospec creating both a class and instance mock.

Issue

Closes #2433

Description

Without setting instance, calls create_autospec() create a mock that behaves as an instance or a class. This doubles the already long time it takes to inspect the class to make the auto_spec.

Using the profiler on
python -m cProfile -o profile-after -m pytest mantidimaging/gui/windows/main/test/presenter_test.py
the line for create_autospec is reduced from
513/264 0.693 0.001 31.746 0.120 mock.py:2697(create_autospec)
to
264 0.434 0.002 16.114 0.061 mock.py:2697(create_autospec)
Showing that the time is halved (31.746 -> 16.114), and the internal second call is prevented (513 calls, reduced to 264).

Overall the runtime of the full unit test suite, was reduced from around 174s to 143s.
For running python -m pytest -p no:xdist -p no:randomly, and using the pytest reported time. There is more run to run variation without disabling randomly, but still a notable improvement.

Before: 173.64s 172.07s 174.11s 175.10s 173.90s
After: 144.57s 141.80s 141.47s 144.13s 144.25s

All tests were done with CPU set to 2GHz to avoid variations
sudo cpupower frequency-set -g performance ; sudo cpupower frequency-set --max 2G

Testing & Acceptance Criteria

Check that auto mocks, are still catching problems, by adding call to a non-existent method in mantidimaging/gui/windows/main/test/presenter_test.py
e.g.
self.view.does_not_exist()

Documentation

Release notes

Notes

Change was performed with Comby
comby "create_autospec(:[arg1])" "create_autospec(:[arg1], instance=True)" .py -stats -i

Save time, by preventing create_autospec creating both a class and instance mock.
@coveralls
Copy link

Coverage Status

coverage: 73.923%. remained the same
when pulling f2f3917 on 2433-autospec-speedup
into 5216e1c on main.

@samtygier-stfc samtygier-stfc marked this pull request as ready for review December 11, 2024 14:44
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 this pull request may close these issues.

Work around create_autospec slowness
2 participants