Use instance=True in create_autospec #2434
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Save time, by preventing create_autospec creating both a class and instance mock.
Issue
Closes #2433
Description
Without setting
instance
, callscreate_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 from513/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