-
Notifications
You must be signed in to change notification settings - Fork 795
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
Speed up unit tests #1336
Speed up unit tests #1336
Conversation
Calls to encrypt_string() result in calls to pyAesCrypt.encryptStream(). These calls are very slow (about .150ms). Modifying these tests to use static ciphertext instead of encrypting the file each time saves approximately 300ms when running the unit test suite.
This allows you to skip slow tests by running `pytest -m 'not slow'`.
WmiExploiter relies on impacket. Importing impacket is slow, which has a negative impact on the speed of pytest collection. SSHExploiter is much quicker to import.
The ZerologonExploiter relies on impacket. Importing impacket is slow (approximately .72s). By moving the import statement in zerologon tests and marking them as slow, the import (and tests) can now be skipped by running `pytest -m 'not slow'`.
Codecov Report
@@ Coverage Diff @@
## develop #1336 +/- ##
===========================================
- Coverage 40.31% 40.09% -0.23%
===========================================
Files 475 474 -1
Lines 13874 13878 +4
===========================================
- Hits 5593 5564 -29
- Misses 8281 8314 +33
Continue to review full report at Codecov.
|
@@ -360,7 +360,7 @@ def _encrypt_or_decrypt_config(config, is_decrypt=False): | |||
parent_config_arr = config_arr | |||
config_arr = config_arr[config_key_part] | |||
|
|||
if isinstance(config_arr, collections.Sequence) and not isinstance(config_arr, str): | |||
if isinstance(config_arr, collections.abc.Sequence) and not isinstance(config_arr, str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rationale behind this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you do any more intricate profiling than just running the pytest? With |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
# Emulate timeout - ttl is manually deleted here, since we're using mongomock and not a | ||
# real mongo instance. | ||
sleep(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this here in the first place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
¯\(ツ)/¯
@@ -2,7 +2,7 @@ | |||
|
|||
import pytest | |||
|
|||
from infection_monkey.exploit.wmiexec import WmiExploiter | |||
from infection_monkey.exploit.sshexec import SSHExploiter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we changing the exploiter ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See commit messages: 2496ed0
What does this PR do?
Resolves #1125
This PR uses a number of tricks to speed up unit test execution:
test_monkey.py
.Results
Collection Time
Collection time is sped up by 34%

Total Run Time
Total run time for the entire suite is sped up by 30%

Option to skip slow tests
For some slow tests, it would cost more to speed them up than it would save. A nice workaround for this is to use pytest markers to mark them as slow. They can then be skipped by running
pytest -m 'not slow'
. This speeds up the total runtime by 60%, at the expense of skipping 14 tests.PR Checklist
Was the documentation framework updated to reflect the changes?Testing Checklist