Skip to content

Reducing python test execution time#9466

Open
MohamedBilelBesbes wants to merge 1 commit intomozilla:masterfrom
MohamedBilelBesbes:TestRuntimeFix
Open

Reducing python test execution time#9466
MohamedBilelBesbes wants to merge 1 commit intomozilla:masterfrom
MohamedBilelBesbes:TestRuntimeFix

Conversation

@MohamedBilelBesbes
Copy link
Copy Markdown
Contributor

It was reported here that adding the voting system caused an increase of 12% in python unit test execution time.

Upon investigation, it turned out that running 5 statistical detectors twice per signature (once per voting strategy) was the main bottleneck. Now, detection runs once and the result is shared. On top of that, the 5 detector objects were being reconstructed on every loop iteration across the series; pinning them to a module-level constant eliminates that overhead entirely. Both fixes compound across every test that touches alert generation.

Changes in alerts.py:

  • Extracted prepare_series_for_detection to isolate the expensive DB fetch + detect_methods_changes step from the voting logic so it could be reused across multiple strategy calls.
  • Added _trim_series_after helper to encapsulate the timestamp-based series filtering, replacing an inline list comprehension.
  • Introduced CPD_METHODS module-level constant so build_cpd_methods() is called only once at import time instead of on every alert generation cycle, removing redundant instantiation of all detector objects across get_methods_detecting_at_index, create_alert, and prepare_series_for_detection.

Changes in tasks.py are related to creating the series with detections running once at first and used subsequently for all voting systems in use.

Changes in test_methods_alerts.py are related to reducing the calls of build_cpd_methods

@gmierz is there a way to measure whether there is indeed a reduction of python unit tests runtime given the fixes of this PR?

@Archaeopteryx
Copy link
Copy Markdown
Collaborator

Maybe you can access this overview for the pipeline execution time for PRs. Reduction is 25-55s. Thank you for the improvements.

Comment thread treeherder/perf/alerts.py Outdated
Comment thread treeherder/perf/alerts.py
# Track which indices we've already added detections for (to avoid duplicates
# in both Phase 1 and the fallback equal voting strategy)
alerted_indices = set()
last_alerted_index = -detection_index_tolerance - 1
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this may have been added after my last review (I don't remember seeing it before at least). Just a note for future PRs, please try to keep the scope limited to make the reviews quicker and less error-prone.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, I could still separate the new changes into a separate PR if you want.

Comment thread treeherder/perf/alerts.py
# Track which indices we've already added detections for (to avoid duplicates
# in both Phase 1 and the fallback equal voting strategy)
alerted_indices = set()
last_alerted_index = -detection_index_tolerance - 1
Copy link
Copy Markdown
Collaborator

@gmierz gmierz Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why is there a negative sign now for detection_index_tolerance? This seems convoluted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically the negative sign is just there so the very first data point doesn't get skipped cuz it sets the starting value far enough back that the proximity check never accidentally fires before any real alert has been created

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.

3 participants