Skip to content

Sorting tests by duration recomputes the duration of a test suite for every comparison - #7035

Merged
sebastianbergmann merged 1 commit into
sebastianbergmann:13.4from
kamil-tekiela:ByDuration-apply
Oct 6, 2026
Merged

sebastianbergmann merged 1 commit into
sebastianbergmann:13.4from
kamil-tekiela:ByDuration-apply

Conversation

@kamil-tekiela

@kamil-tekiela kamil-tekiela commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Sorting shouldn't fetch the weights at each comparison. It's better to sort the precomputed weights and then map them to the Test objects array. The new approach should theoretically be ~.5% faster, but it should also prevent accidental bugs if, at any point in time, weight is no longer a pure method. And if weight becomes more computationally expensive, the performance won't drop exponentially.

@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.57%. Comparing base (400384b) to head (b23197e).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff              @@
##               13.4    #7035      +/-   ##
============================================
- Coverage     99.57%   99.57%   -0.01%     
- Complexity     9907     9908       +1     
============================================
  Files           964      964              
  Lines         30083    30081       -2     
============================================
- Hits          29956    29954       -2     
  Misses          127      127              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@sebastianbergmann

Copy link
Copy Markdown
Owner

Thank you for this.

Since PHPUnit 13.2.5, the weight of a test suite is the sum of the durations of the tests it contains. ByDuration computes that sum inside the comparator, so usort() walks the entire subtree of a test suite every time it compares that test suite with another one. On a synthetic test suite with about 37,000 tests, sorting by duration takes 380 ms with the current code and 93 ms with your change.

I consider this a fix for a performance regression and would like to ship it in PHPUnit 13.4.2. Can you please rebase your branch onto 13.4, squash it into a single commit, and change the base branch of this pull request to 13.4? ByDuration.php is identical on 13.4 and main, so the rebase will not run into conflicts. I will merge the change up into main after that.

@sebastianbergmann sebastianbergmann changed the title Sort weights instead of Test objects Sorting tests by duration recomputes the duration of a test suite for every comparison Oct 6, 2026
@sebastianbergmann sebastianbergmann added type/performance Issues related to resource consumption (time and memory) version/13 Something affects PHPUnit 13 labels Oct 6, 2026
@kamil-tekiela
kamil-tekiela changed the base branch from main to 13.4 October 6, 2026 10:56
@sebastianbergmann
sebastianbergmann merged commit cb0579c into sebastianbergmann:13.4 Oct 6, 2026
32 checks passed
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

type/performance Issues related to resource consumption (time and memory) version/13 Something affects PHPUnit 13

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants