Postponed (maintainer needs more info)
Project:
Drupal core
Version:
main
Component:
phpunit
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
15 Mar 2019 at 22:00 UTC
Updated:
20 Mar 2026 at 17:43 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
mile23TIMECOP!
This is a listener that fails tests over a time threshold unless they belong to a group.
This patch sets the threshold at 10 seconds, so there should be at least some fails.
Fail message looks like this:
Comment #4
mile23Cranked it up to 60 seconds, and marked all the tests I could stand doing manually based on the results of the last test. Let's see what happens.
Comment #6
mile23Bump up to 70 seconds, because so many more were hovering around 60.
Winner winner chicken dinners from the last test results:
Drupal\Tests\simpletest\Functional\SimpletestUiTestat 458 seconds seems impressive, but then, I give you:Yes, that's almost 20 minutes, so that test now belongs to the
##slowgroup so it will sort even earlier in the batch. Verified in the JUnit: https://dispatcher.drupalci.org/job/drupal_patches/88989/artifact/jenkin...Comment #8
dwwThis is looking pretty good. Almost there. ;)
- Most of the remaining tests are hovering around 70, so it looks like 75 might be enough? Maybe this is a bit too fragile and we should bump to 80 to reduce random failures caused by this. Frankly, even 90 seems reasonable. But let's see how the bot likes 75 for now.
- One of the fails is from something that was trying to be #slow already, but with a typo (
@sgroup #slow).- While
##slowseems like a reasonable approach to a 20+ minute test(!), sadly, none of the plumbing in here is setup to handle that. We just have a single string for the one slow group, and we're testing for it via in_array(), not some kind of preg_match() or strpos() in a loop. Seems like a bit of trouble to refactor this so that slow_group is actually an array of $slow_groups, or to do something other than in_array() to test if a given test matches the slow group. So I just moved that one test back to #slow for now.Comment #10
mile23Yah, I thought about that later, and I was going to go back and add a way to specify that, but if you'd rather not that's fine.
I think the solution to the edge cases is: Come up with a starting point, like say 70 seconds. Then we run it and mark all the tests that fail. Then we set the threshold significantly higher so that it's not a lot of fail whale all day long.
If we had a good way to understand the timing stats, we could come up with a reasonable threshold. I have a sneaking suspicion that 70 seconds is way too short, and we run the risk of having too many tests marked #slow.
Of course we don't keep timing info in our results junit because we use run-tests.sh, but we do have timing info in phpunit's junit files as an artifact, like this: https://dispatcher.drupalci.org/job/drupal_patches/89295/artifact/jenkin...
There are a few different visualization solutions out there, but none look trivial enough to experiment at the moment.
Comment #11
dwwSounds good. So how about 90 for now and leave everything in #slow?
Comment #12
dwwI'm personally triggered by references to "cops", and "timecop" isn't a word. "Speedtrap" is for catching something going too fast, this is the reverse. "Timetrap" makes no sense, either. Frankly, nothing about this fits with the metaphor of cops and speeding. ;)
So here's a re-roll to call this the
TimeLimitListenerTraitand friends. That's what this is about: a time limit. If you're taking too long, and you don't warn folks you're #slow, you fail.Note: this will need a final re-roll once #3038901: Speed up the tests on DrupalCI by not running slow tests last lands, to remove the tests it already marked as #slow. Marking this postponed to reflect that.
Comment #13
mile23Clearly you've never seen Timecop! https://youtu.be/IwzDxz7IUnU
Anyway, no problem at all.
Comment #14
dwwYeah, somehow I missed that one. ;) Thanks for the comic relief.
Cheers,
-Derek
Comment #15
lendudeFirst off, this is very cool! (and I totally got the Timecop reference :-)
Couple of things I see
- We only have one cut off for all testsuites, this fails to catch the kernel test WorkspaceIntegrationTest which gives a 1:30 speed boots when marked #slow, can we figure out a way to somehow control this per suite?
- The build in #12 gives: 56 min build duration, so that is 10 minutes slower then #3038901: Speed up the tests on DrupalCI by not running slow tests last, but the build in #8 was 46 min build duration so is #12 a weird outlier? Worth checking though.
edit: I think it did actually catch WorkspaceIntegrationTest, but the point remains that the criterium for kernel tests should be quicker then functional tests
Comment #16
lendudeAnother thing that comes to mind, the listener is triggered at the end of each test method, not at the end of each test class (right?). run-tests.sh runs by test class (right?), so ideally we would somehow detect slow classes, not just slow methods.
Comment #17
mile23I think we might have marked too many #slow here, and lost the benefit. Early patches were marking 60 seconds as #slow, and later we moved that to 70.
It would be great to have some real data about timing, so we could establish a good cutoff point.
Having configuration per suite is cumbersome, but doable. Until #2863055: Move TestDiscovery out of simpletest module, minimize dependencies happens, we'd introduce a dependency on simpletest module for
TestDiscovery::getPhpunitTestSuite(). Run-tests.sh has that dependency but phpunit does not, so maybe a follow-up.Comment #18
dwwSplit off #3041950: [PP-2] Separate time limits for #slow per test suite, blocked on this and #2863055: Move TestDiscovery out of simpletest module, minimize dependencies.
For now, let's fix the obvious kernel tests at #3038901: Speed up the tests on DrupalCI by not running slow tests last while we sort out the others.
If there are now too many in #slow, perhaps we need multiple tiers and support ##slow?
Comment #19
larowlanblocker is in
Comment #20
lendudeI think we just had a 5 minute increase in running time in HEAD somewhere over the last couple of days, cause the HEAD test where up to 57-58 minutes before the other #slow issue landed
Comment #21
dwwSimple re-roll now that the parent is in. No further changes.
I'm sure now that #2843147: Add JSON:API to core as a stable module landed, our test times have seen a big jump. ;)
Comment #22
mile23How to: Generate neeto graphs of test run execution time per test.
Visit the test run artifacts: https://dispatcher.drupalci.org/job/drupal_patches/89803/artifact/jenkin...
Visit the phpunit-xml artifacts per test suite, in this case functional tests: https://dispatcher.drupalci.org/job/drupal_patches/89803/artifact/jenkin...
Scroll down and get all the files as a zip file.
Extract, and rename all the files so they have leading zeros (that way they'll be in execution order *and* alphabetical order):
rename -e 's/\d+/sprintf("%04d",$&)/e' -- *.xmlMerge them with junit merge: https://github.com/andreas-weber/php-junit-merge
Set up https://github.com/marmelab/phpunit-d3-report
Paste your merged xml into that thing.
End up with this:
This is an inside-to-outside spiral. Closer to the inside shows start time. Larger dots are execution time.
In an ideal world the place on the spiral would be the order of completion, but that's not what drupalci can tell us. But it does tell us that we've been pretty successful.
Here's the kernel tests:
Same deal. All dots are sub-30 second except the giant in the center. That's
WorkspaceIntegrationTestat 5:20.Unit tests:
Don't be misled. The biggest ones test the quick start feature, and the longest one is 12 seconds, which I think is a pretty good amount of time to install a whole Drupal. But we could still optimize a little more.
Functional Javascript:
The big one at the top right is
QuickEditIntegrationTest, 1:04. So clearly that's one we missed on the first round.This has been another episode of Finger Painting with Mile23.
Comment #23
mile23Comment #24
lendudeWooooow! Just ..... wooooow! Awesome stuff! Gonna print them and put them on my wall :D
Great way to see that we really got this about as good as we can get it. Maybe some work on functional javascript, but not a lot of gain in that.
My remaining puzzle is WorkspaceIntegrationTest, ran locally:
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.
Testing Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest
Time: 42.58 seconds, Memory: 6.00MB
OK (7 tests, 682 assertions)
Process finished with exit code 0
So why the hell does that take 5:20 on the bot?
Comment #25
amateescu commented@Lendude, that's because the default test environment uses MySQL 5.5, which has a big performance problem with queries that involve a lot of joins, see #2983639: Re-enable a bit of test coverage for Workspaces for more details :)
I just queued a PHP 7.1 & MySQL 5.7 test on #21 which should come back with a better time for
WorkspaceIntegrationTest.Comment #26
amateescu commentedOpened #3042310: Change the issue testing default MySQL version to 5.7: 20% faster than the EOL'd 5.5 to discuss switching to MySQL 5.7 as the default testing version.
Comment #27
wim leers#22: 😮 🤩
Comment #28
mile23I neglected to state that the first graph is only functional tests.
Looking at #22 again today I think the main takeaway is that #3041950: [PP-2] Separate time limits for #slow per test suite would be worth it, especially if we're going to keep each suite in a separate test run. It's especially visible in the kernel tests where we have an outer ring of long tests starting relatively late. If the premise of long-tests-first is correct then we can improve each phase.
And even if we were to change gears and decide to run all the tests in one big group, we'd still benefit from efficient packing on the longest tests.
Also keep in mind that this is possible because of concurrency, which we get from run-tests.sh, which no one dare touch, because it's brittle and generally untestable. #2624926: Refactor run-tests.sh for Console component. And the replacement is considered to be paratest, which has limitations: #2781123-12: [experiment] Explore paratest to run our phpunit tests in parallel
Comment #30
lendudeRequeued #21 and a new slow test got introduced, so here is it added to the patch
Comment #31
dwwRelatively simple, elegant solution to improve test bot performance and ensure that the goodness from #3038901: Speed up the tests on DrupalCI by not running slow tests last isn't undone by new tests. That's been in for a while and hasn't caused any problems, so we want to enforce that we keep marking things #slow that need it.
#22 beautifully shows why this is working (as an initial fix -- see the followup issues in #28 for future improvements).
Code is clean, bot is happy.
Therefore, RTBC.
Thanks,
-Derek
p.s. Disclaimer: Yes, I know I have my fingerprints on the patch, but I still feel okay RTBC'ing.
Comment #32
mile23Thanks.
The main caveat is that your test runner machine may not be as fast as the testbot, leading to false fails.
We might introduce an environmental variable so you can turn it off from the command line, and/or so that you can tweak the various timeouts. (That's how it would have to work, since the listener is implemented as a trait, so you can't just remove it from phpunit.xml.)
Leaving RTBC so a maintainer can say the whole thing's a bad idea before I add to the patch. :-)
Comment #33
mile23Added the ability to bypass the listener with
DRUPAL_TEST_DISABLE_TIME_LIMIT=true.Comment #34
dwwgetenv() on a killswitch seems like a Really Smart Idea(tm). I can totally see wanting/needing to set that, great point. Thanks!
The interdiff is simple and clean. Bot is still happy. Back to RTBC (for now). ;)
Once this lands, we might want to mention the change to the test suite and the env var in some docs. Possibilities include:
core/tests/README.md
https://www.drupal.org/docs/8/phpunit/running-phpunit-tests (for setting the env var if needed while running tests)
https://www.drupal.org/docs/8/phpunit/phpunit-file-structure-namespace-a... (for mentioning #slow @group as possible metadata when writing tests)
...
However, I'm not sure it's worth tagging for "needs documentation" updates yet. I suspect people will simply learn about the existence of this when they write really slow tests, submit a patch, see a failure, and read the self-documenting text assertion failure message:
But, if the problem is their test running environment is slower than the bot, the solution might be to set the killswitch env var, instead of adding anything to the #slow group. *Perhaps* it's worth updating this error message to mention the killswitch, too? Something like:
(more or less).
Thoughts?
Thanks,
-Derek
Comment #35
lendude@dww I wrote down the same points in a comment while you posted #34 :D
So #34 +1 is all I need to say! I think adding a reference to the env var in the error message is probably worth the effort.
Comment #36
dwwHasty RTBC @ #34. I believe the logic at the
getenv()call was backwards.This patch adds the message (could still maybe use some word-smithing help) and inverts the logic:
Before:
If I set
DRUPAL_TEST_DISABLE_TIME_LIMIT=true(as documented), then we gettrue !== FALSE, which isTRUE, and then we calltimeLimitEndTest(). If I don't set the env var, we getFALSE !== FALSE, which isFALSE, and we don't calltimeLimitEndTest(). :(Now:
So, if the env var isn't set, or is set to FALSE value, we call the method (as intended). If it's set to any non-FALSE value, we skip
timeLimitEndTest().Perhaps to handle edge cases like setting
DRUPAL_TEST_DISABLE_TIME_LIMIT=0orDRUPAL_TEST_DISABLE_TIME_LIMIT=falsewe should really use something like:Thoughts?
I also considered inverting the name / behavior of the env var, to call it
DRUPAL_TEST_ENABLE_TIME_LIMIT, but that also gets complicated since we want the undefined case to default toTRUE. So, I think double-negative hell is the best we can hope for with this.Someone else please confirm I'm not losing my mind and RTBC or NW as appropriate. ;)
Thanks!
-Derek
Comment #37
mile23Yah, I should have worked harder on
DRUPAL_TEST_DISABLE_TIME_LIMIT.getenv($name)always returns a string, unless the environmental variable isn't set, in which case it returns FALSE. https://www.php.net/manual/en/function.getenv.phpSo my code (#33) only skips if the variable isn't set, and does the wrong thing if you were to say
DRUPAL_TEST_DISABLE_TIME_LIMIT=false, because the value is'false'.We can't use
empty()because the string'false'is not empty.So that leaves us checking whether it's FALSE, and then checking if the value is 'true.'
Here's a patch with a test and everything. :-)
This could benefit 8.7.x, but a maintainer can figure out if it's worth backporting later.
Comment #39
dwwRandom fail from Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTest :/
Let's try again, bot...
Comment #40
lendudeNice coverage, @Mile23++
Updated the IS with the kill switch and added a CR because of the possible disruption to contrib that will start seeing these failures once this lands, https://www.drupal.org/node/3064147
Comment #41
catchHas there been any discussion of flipping the default? So that DrupalCI core test runs explicitly enable this, but people's random and potentially slow environments running locally don't have to worry about it.
Also seems pretty optional for contrib which tend to have a lot fewer tests in total.
Comment #42
mile23#41 seems eminently reasonable. :-)
Comment #43
dww#41 would also make inverting the name easier to avoid double negative hell as I discussed at the end of #36. So tentatively +1 to that.
My only hesitation is wondering which case results in more total pain/confusion/cost:
A) People with slow local environments see tests fail that aren't really a failure. They see a big obvious error message about why, they set the env var as needed.
B) People with normal environments write slow tests that pass locally and fail on the bot, resulting in issue/patch churn on d.o.
In short: this would introduce 1 more thing that by default will be different between the local test environment and the d.o testbot, and ultimately, things have to pass on the d.o testbot to result in committed patches and fixed issues. Granted, hopefully people aren't writing too many slow tests, so B should be fairly rare. Not sure about the relative performance of a d.o testbot vs. your average Drupal dev's laptop or other "local" testing environment, so I don't have a good intuition for how likely A is going to actually be a problem.
If we pick B as the lesser evil and invert the default behavior, I'd strongly recommend that we still mention the env var in the test error message so that if people see success locally but are looking at failed test results on d.o, they'll see something about this variable being set to know why the time limit is being enforced.
If we go that far, maybe the env var should define the time limit in seconds instead of being a bool? E.g.:
Does that help us get closer to #3041950: [PP-2] Separate time limits for #slow per test suite? Is it easy for the test bot to set this env var differently for each test suite?
Comment #44
lendudeSwitched from disable to enable, also means it's added to the phpunit.xml.dist. Lets see if I got everything.
Comment #46
dwwI think we need a follow-up issue in the d.o infra queue about setting this correctly on the test bots, right? Or do the test bots start from the shipped phpunit.xml.dist so a change like this actually gets picked up? Doesn't look like it (see below).
Early return from this function means we don't enforce the limit. We need:
if (!$this->timeLimitEnabled()) {However, the combo of #1 and #2 means that the bot did enforce time limits on #44, uncovering some more slow layout_builder tests. So also NW to add that to #slow, too. ;)
Comment #47
dwwAdds
@group #slowto the test(s) that failed in #44. Note that #44 failed on 2 tests, but it's the same test method in a base class shared by 2 different test classes.Fixed logic bug I pointed out in #46.2
Not sure how to resolve #46.1.
This will probably pass, but that's not too interesting...
Comment #48
dwwRe: #46.1: I asked in #testing via Slack and @mixologic said:
So, back to NW to add an option to run-tests.sh to support this stuff and to change drupalci.yml to enable it. No followup needed.
Comment #49
mile23So now we have a few things:
DRUPAL_TEST_TIME_FAIL=90(set to FALSE to turn it off?).--time-fail 90.testgroupskey, since it's just a string that's added at the end, but we should do it right.This is a better design than the follow-up implies: #3041950: [PP-2] Separate time limits for #slow per test suite
It's also why I started with
DRUPAL_TEST_DISABLE_TIME_LIMIT. :-)BTW, if you're wondering why
TestSiteApplicationTestdoesn't belong to the #slow group despite taking almost 2 minutes, it's because the testbot never runs it. #2878269: Modify TestDiscovery so the testbot runs all the testsComment #50
mondrakeFWIW, #2905007: Allow run-tests.sh to report skipped/incomplete PHPUnit tests is proposing to switch the execution of PHPUnit spawns to Symfony Process component, instead of
exec. That would allow to set a timeout limit for the execution of the process, so failing the test with an exception when the process gets over the time limit. No need for listeners and no need to actually wait for the test to complete.Comment #54
mondrakeComment #60
catchThis would help with #3386091: [PP-1] Reduce test pipeline times to around 18 minutes and always report back unit tests although we might need to think about distribution of the slow tests when running in parallel.
Comment #62
mondrakeIMHO we should really really try to avoid implementing listeners at this point… they’re removed in PHPUnit 10. We would be adding an obstacle here.
Comment #63
catchComment #64
catchComment #65
catchComment #66
catchMight be ready for review here. Current threshold set at 60s.
Comment #67
catchOnce this is green I'll lower the threshold to 30s to see how many tests are affected. We should probably tag any tests that are up to 25% or so faster than the threshold to allow for variation, so if we leave it at 60s, add #slow to all tests 45s and over. Now that slow tests are distributed between runners a 45s threshold might be OK even.
Comment #68
mondrakeSee #62. IMHO we should not implement listeners any more. They are deprecated since PHPUnit 8 IIRC. Can the same be achieved in
tearDownor in the test runner?For example, in #3217904: [meta] Support PHPUnit 10 in Drupal 11's MR I try to use Symfony Process component to run PHPUnit, where you can set set a timeout
Comment #69
wim leers🤩🤩🤩
LOVE THIS! Automating the finding of slow tests. Perfect. That's how it should be. No human should spend time on this.
🤯🤩
In today's Drupal core world, this sounds like dark magic.
In 2024's Drupal core world, we'll have forgotten it was ever any different. (I hope.)
For now, just one question: shouldn't we only enforce this limit on GitLab CI, and not by default on local development? Because many local development environments won't be able to match GitLab CI's speed.
Comment #70
catchSee the phpunit.xml.dist hunks - it's configurable in there.
Comment #71
catch@mondrake that makes sense. I'll split out the run-tests.sh and test changes to a separate issue, then we can continue working on the enforcement here.
Comment #72
catchHere's the spin-off #3388365: Distribute @group #slow tests between test runners and mark more tests.
Comment #73
catchIf it's possible, the ideal place to do this would be in run-tests.sh itself, based on an environment variable(s) - the advantage of this is that run-tests.sh --all with concurrency runs file-at-a-time so a test with multiple methods or using a dataprovider which wouldn't fail a per-method timer can be several minutes in total. i.e. 12 29 second methods is a six minute test that will be below a 30 second method threshold.
Comment #74
mstrelan commentedIs it possible to use timings from .phpunit.result.cache? Then we don't need our own listener.
We can run
phpunit --cache-result --cache-result-file=.phpunit.result.cache --testsuite=functional. This gives us a json document with the timings of each test method. The major difference here is that it is per method rather than the whole class, so we'd have to sum up the timings for a whole class.Comment #76
kim.pepperAgree with #74. PHPUnit already caches timing information. We just need a way to read it.
We are building a prototype symfony command that reads this info to do test splitting by file based on timings.
Comment #77
larowlanWhat does `phpunit --order-by=duration` yield?
Comment #78
mstrelan commentedRe #77:
I ran the following in order to generate the result cache:
./vendor/bin/phpunit -c core/phpunit.xml.dist --testsuite=unit --cache-result --cache-result-file=.phpunit.result.cacheThen order by duration:
Note that this is the same order with or without the cache result file, maybe I'm missing something. Either way, we'd need to use reflection or something else to determine the file path from the class name.
Comment #79
catchhttps://git.drupalcode.org/project/drupal/-/pipelines/23348/test_report (which I've just found this morning) orders test by slowest descending, alhtough again by method rather than class, so is good to compare.
Comment #80
catchRe-titling this one for the result cache ordering idea.
Comment #81
kim.pepperAs mentioned in #76 we wrote a little experimental library to read the result cache timing info. See the readme for more info: https://github.com/previousnext/phpunit-splitter
Comment #82
catchAfter working on #3462763: Use artifacts to share the phpstan result and cspell caches from core to MRs, it feels like reliably getting the result cache from parallel jobs will be relatively tricky and hard to debug, it was bad enough with one artifact from one job, but thinking about this issue again gave me an idea for a simpler version which still automates most of what we use @group #slow for: #3465602: Order tests by number of public methods to optimize gitlab job times.
Comment #83
catch#3497431: Deprecate TestDiscovery test file scanning, use PHPUnit API instead improves the ordering/distribution from run-tests.sh further, so I think this is probably not worth the additional complexity.
Comment #84
mondrakeComment #85
mondrakeComment #87
mondrakehttps://github.com/shipmonk-rnd/phpunit-parallel-job-balancer is a possible helper here; https://drupal.slack.com/archives/C07V7UZNVSM/p1772812929388829
Comment #88
mondrakeWe could use a BFD algorithm to address the Bin packing problem of allocating tests to parallel execution bins.
Ref: https://www.3dbinpacking.com/en/blog/bin-packing-optimization-strategies/