Problem/Motivation

In #3038901: Speed up the tests on DrupalCI by not running slow tests last we formalized adding @group #slow to tests which take a long time to run. This way we can prioritize them to run early.

In that issue we discussed how to determine which tests should be marked as #slow.

Instead of manually marking tests slow, we can probably use the phpunit result cache to find the slowest tests, and order by that instead. This will require run-tests.sh integration and etc. so not trivial to do.

Proposed resolution

Add a listener that can be disabled with and env variable

Remaining tasks

none

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3040694

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Mile23 created an issue. See original summary.

mile23’s picture

Status: Active » Needs review
StatusFileSize
new6.07 KB

TIMECOP!

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:

3) Drupal\Tests\Component\PhpStorage\MTimeProtectedFastFileStorageTest::testSecurity
TIMECOP: Enforcing time limit of 0.5 seconds. This test took 1.0045540332794 seconds, which is too long. Annotate it with @group #slow to prevent further failures.

Status: Needs review » Needs work

The last submitted patch, 2: 3040694_2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new13.6 KB
new7.91 KB

Cranked 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.

Status: Needs review » Needs work

The last submitted patch, 4: 3040694_4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new27.53 KB
new15.74 KB

Bump 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\SimpletestUiTest at 458 seconds seems impressive, but then, I give you:

1) Drupal\Tests\system\Functional\Module\InstallUninstallTest::testInstallUninstall
TIMECOP: Enforcing time limit of 60 seconds. This test took 1150.0931479931 seconds, which is too long. Annotate it with @group #slow to prevent further failures.

Yes, that's almost 20 minutes, so that test now belongs to the ##slow group so it will sort even earlier in the batch. Verified in the JUnit: https://dispatcher.drupalci.org/job/drupal_patches/88989/artifact/jenkin...

Status: Needs review » Needs work

The last submitted patch, 6: 3040694_6.patch, failed testing. View results

dww’s picture

Status: Needs work » Needs review
StatusFileSize
new27.28 KB
new1.38 KB

This 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 ##slow seems 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.

Status: Needs review » Needs work

The last submitted patch, 8: 3040694-8.patch, failed testing. View results

mile23’s picture

While ##slow seems like a reasonable approach

Yah, 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.

dww’s picture

Status: Needs work » Needs review
StatusFileSize
new27.28 KB
new358 bytes

Sounds good. So how about 90 for now and leave everything in #slow?

dww’s picture

Title: Add an automated way for slow tests to fail drupalci builds » [PP-1] Add an automated way for slow tests to fail drupalci builds
Status: Needs review » Postponed
StatusFileSize
new27.3 KB
new7.74 KB

I'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 TimeLimitListenerTrait and 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.

mile23’s picture

Clearly you've never seen Timecop! https://youtu.be/IwzDxz7IUnU

Anyway, no problem at all.

dww’s picture

Yeah, somehow I missed that one. ;) Thanks for the comic relief.

Cheers,
-Derek

lendude’s picture

First 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

lendude’s picture

Another 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.

mile23’s picture

I 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.

dww’s picture

Split 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?

larowlan’s picture

Title: [PP-1] Add an automated way for slow tests to fail drupalci builds » Add an automated way for slow tests to fail drupalci builds
Status: Postponed » Needs review

blocker is in

lendude’s picture

I 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

dww’s picture

StatusFileSize
new24.65 KB
new1.74 KB

Simple 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. ;)

mile23’s picture

Issue summary: View changes
StatusFileSize
new707.69 KB
new578.28 KB
new373.31 KB
new204.55 KB

How 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' -- *.xml

Merge 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 WorkspaceIntegrationTest at 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.

mile23’s picture

Issue summary: View changes
lendude’s picture

Wooooow! 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?

amateescu’s picture

@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.

amateescu’s picture

Opened #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.

wim leers’s picture

#22: 😮 🤩

mile23’s picture

I 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

Status: Needs review » Needs work

The last submitted patch, 21: 3040694-21.patch, failed testing. View results

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new393 bytes
new25.43 KB

Requeued #21 and a new slow test got introduced, so here is it added to the patch

dww’s picture

Status: Needs review » Reviewed & tested by the community

Relatively 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.

mile23’s picture

Thanks.

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. :-)

mile23’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new25.5 KB
new660 bytes

Added the ability to bypass the listener with DRUPAL_TEST_DISABLE_TIME_LIMIT=true.

dww’s picture

Status: Needs review » Reviewed & tested by the community

getenv() 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:

'TIME: Enforcing time limit of ' . $this->timeThreshold . ' seconds. This test took ' . $time . ' seconds, which is too long. Annotate it with @group ' . $this->slowGroup . ' to prevent further failures.'

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:

'TIME: Enforcing time limit of ' . $this->timeThreshold . ' seconds. This test took ' . $time . ' seconds, which is too long. Annotate it with @group ' . $this->slowGroup . ' to prevent further failures, or set the DRUPAL_TEST_DISABLE_TIME_LIMIT environment variable to true to disable time limits for tests.'

(more or less).

Thoughts?

Thanks,
-Derek

lendude’s picture

@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.

dww’s picture

Version: 8.7.x-dev » 8.8.x-dev
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new25.38 KB
new1.56 KB

Hasty 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 (getenv('DRUPAL_TEST_DISABLE_TIME_LIMIT') !== FALSE) {
        $this->timeLimitEndTest($test, $time);
      }

If I set DRUPAL_TEST_DISABLE_TIME_LIMIT=true (as documented), then we get true !== FALSE, which is TRUE, and then we call timeLimitEndTest(). If I don't set the env var, we get FALSE !== FALSE, which is FALSE, and we don't call timeLimitEndTest(). :(

Now:

      if (getenv('DRUPAL_TEST_DISABLE_TIME_LIMIT') === FALSE) {
        $this->timeLimitEndTest($test, $time);
      }

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=0 or DRUPAL_TEST_DISABLE_TIME_LIMIT=false we should really use something like:

      if (empty(getenv('DRUPAL_TEST_DISABLE_TIME_LIMIT'))) {
        $this->timeLimitEndTest($test, $time);
      }

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 to TRUE. 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

mile23’s picture

StatusFileSize
new27.54 KB
new3.43 KB

Yah, 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.php

So 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.

Status: Needs review » Needs work

The last submitted patch, 37: 3040694_37.patch, failed testing. View results

dww’s picture

Status: Needs work » Needs review

Random fail from Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTest :/
Let's try again, bot...

lendude’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Nice 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

catch’s picture

Has 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.

mile23’s picture

Status: Reviewed & tested by the community » Needs work

#41 seems eminently reasonable. :-)

dww’s picture

#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.:

'TIME: The DRUPAL_TEST_TIME_LIMIT environment variable is set to ' . $this->timeThreshold . ' seconds. This test took ' . $time . ' seconds, which is too long. Annotate it with @group ' . $this->slowGroup . ' to prevent further failures, or unset the DRUPAL_TEST_TIME_LIMIT environment variable to disable time limits for tests.'

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?

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new4.28 KB
new28.16 KB

Switched from disable to enable, also means it's added to the phpunit.xml.dist. Lets see if I got everything.

Status: Needs review » Needs work

The last submitted patch, 44: 3040694-44.patch, failed testing. View results

dww’s picture

Issue tags: +Needs followup
  1. +++ b/core/phpunit.xml.dist
    @@ -27,6 +27,8 @@
    +    <!-- To disable the time limit checking for long running test comment the next line -->
    +    <env name="DRUPAL_TEST_ENABLE_TIME_LIMIT" value="true"/>
    

    I 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).

  2. +++ b/core/tests/Drupal/Tests/Listeners/TimeLimitListenerTrait.php
    @@ -63,7 +63,7 @@
       protected function timeLimitEndTest($test, $time) {
    -    if ($this->timeLimitDisabled()) {
    +    if ($this->timeLimitEnabled()) {
           return;
    

    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. ;)

dww’s picture

Status: Needs work » Needs review
StatusFileSize
new28.73 KB
new1.06 KB

Adds @group #slow to 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...

dww’s picture

Status: Needs review » Needs work
Issue tags: -Needs followup

Re: #46.1: I asked in #testing via Slack and @mixologic said:

Goes back to the same ol problem. The testbots do not run phpunit. They run run-tests.sh. If you're going to be adding features to unit tests that are configurable with an env variable, then you should add that as another command line switch to run-tests.sh, and then you would be able to control it from drupalci as another option automatically. (from drupalci.yml that is)

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.

mile23’s picture

So now we have a few things:

  • Add a configurable test listener to PHPUnit, something like DRUPAL_TEST_TIME_FAIL=90 (set to FALSE to turn it off?).
  • Add a command-line configuration to run-tests.sh, something like --time-fail 90.
  • Add support for this command to the drupalci testbot run-tests plugin. It's possible to hack around this using the testgroups key, since it's just a string that's added at the end, but we should do it right.
  • Modify core's drupalci.yml so that various test suites have different timeouts.

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 TestSiteApplicationTest doesn'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 tests

mondrake’s picture

FWIW, #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.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

mondrake’s picture

Component: simpletest.module » phpunit

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

This 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.

mondrake’s picture

IMHO 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.

catch’s picture

Title: Add an automated way for slow tests to fail drupalci builds » Add an automated way to identify slow tests on Gitlab CI
Issue summary: View changes
catch’s picture

Issue summary: View changes
catch’s picture

Title: Add an automated way to identify slow tests on Gitlab CI » Add an automated way to identify slow tests and distribute them amongst runners on Gitlab CI
catch’s picture

Issue summary: View changes
Status: Needs work » Needs review

Might be ready for review here. Current threshold set at 60s.

catch’s picture

Once 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.

mondrake’s picture

Status: Needs review » Needs work

See #62. IMHO we should not implement listeners any more. They are deprecated since PHPUnit 8 IIRC. Can the same be achieved in tearDown or 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

    $process = new Process($command, \Drupal::root() . "/core", $process_environment_variables);
    $process->setTimeout(300);
    $process->run();
wim leers’s picture

Priority: Normal » Major
Issue tags: +Test suite performance, +DX (Developer Experience)

🤩🤩🤩

LOVE THIS! Automating the finding of slow tests. Perfect. That's how it should be. No human should spend time on this.

Once 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.

🤯🤩

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.

catch’s picture

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.

See the phpunit.xml.dist hunks - it's configurable in there.

catch’s picture

@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.

catch’s picture

Title: Add an automated way to identify slow tests and distribute them amongst runners on Gitlab CI » Add an automated way for slow tests to fail drupalci builds
Issue summary: View changes
Priority: Major » Normal
Issue tags: -Test suite performance, -DX (Developer Experience)
catch’s picture

If 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.

mstrelan’s picture

Is 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.

kim.pepper’s picture

Agree 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.

larowlan’s picture

What does `phpunit --order-by=duration` yield?

mstrelan’s picture

Re #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.cache

Then order by duration:

$ ./vendor/bin/phpunit -c core/phpunit.xml.dist --testsuite=unit --list-tests --order-by=duration | head -n 10
PHPUnit 9.6.8 by Sebastian Bergmann and contributors.

Available test(s):
 - Drupal\Tests\CSpell\SortTest::testFileExists
 - Drupal\Tests\CSpell\SortTest::testSorted
 - Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts#0
 - Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts#1
 - Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts#2
 - Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts#3
 - Drupal\Tests\Core\DrupalKernel\DrupalKernelTest::testTrustedHosts#4

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.

catch’s picture

https://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.

catch’s picture

Title: Add an automated way for slow tests to fail drupalci builds » Use the phpunit result cache to order tests by slowest first in gitlab jobs
Issue summary: View changes

Re-titling this one for the result cache ordering idea.

kim.pepper’s picture

As 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

catch’s picture

After 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.

catch’s picture

Status: Needs work » Postponed (maintainer needs more info)

#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.

mondrake’s picture

Issue tags: +PHPUnit 11
mondrake’s picture

Issue tags: -PHPUnit 11 +PHPUnit 12

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

mondrake’s picture

Title: Use the phpunit result cache to order tests by slowest first in gitlab jobs » Use previous PHPUnit JUnit results to pack tests in bins when parallelizing

We 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/