Problem/Motivation
A high proportion of the time spent in kernel tests is building a container for each test method. This can take several hundred milliseconds for each method, and we have thousands of kernel test methods in core.
Following #3583505: Use Symfony PhpDumper instead of a serialized array container structure it should theoretically be possible to re-use the same on-disk container between either each method of a kernel class, or ideally every method of every kernel class that shares the same module list.
Steps to reproduce
Proposed resolution
Subclass DrupalKernel to a new KernelTestDrupalKernel class. In this class, support re-using the initial container definition from when it's first booted. This allows the first method of a kernel test class to build the container from scratch, and the subsequent methods to get it from cache.
Remaining tasks
Stacked on top of #1411074: Add a flag to set up test environment only once per test class for kernel tests and has a hard dependency on that issue.
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3613347
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
Comment #3
catchThis is starting to approach something that works.
Some tests are completely fine, quite a few seem to fail at the last method for some reason (may be a coincidence), others are failing on several methods.
Things I looked into so far but didn't rule in or out yet:
- Some of the test failures look like they're related to \Drupal:: calls in the test or related code, this could be the changes in KernelTestBase resulting in the container object out of sync, if so that would break loads of things.
- One thing I noticed is that because we use the site path in e.g. APCu backend prefixes, the generated container between each test method is not exactly the same. For APCu this doesn't matter but for other tests it might e.g. if the site path doesn't match the actual site path. One option might be to base the site path on the test class name, and setup/teardown everything in that directory each time. This is not a big change in KernelTestBase but also it doesn't fix any of the tests either so didn't commit any changes for that yet.
Comment #4
catchComment #5
catchPostponing this on #1411074: Add a flag to set up test environment only once per test class for kernel tests which should make it much, much easier and less error-prone.
Comment #6
catchPushed a new branch based on the latest in #1411074: Add a flag to set up test environment only once per test class for kernel tests.
Comment #8
catchhttps://git.drupalcode.org/project/drupal/-/merge_requests/16844 is green now.
Did some local profiling and this is successfully avoiding a container rebuild for every method of kernel tests that share the database - it will automatically kick in when that's the case as tests are converted based on #1411074: Add a flag to set up test environment only once per test class for kernel tests.
Comment #9
catch