Problem/Motivation

The TestDiscovery cache leads to DX issues while trying to write tests.

Write a test or two, look for them in the Simpletest UI, and they won't be discovered. That's because the cache took over.

Using run-tests.sh to run tests turns off the caching behavior, so new tests will be discovered there.

Thus the only use-case for the cache is to speed up the display of the Simpletest UI. The Simpletest UI might end up being removed for Drupal 9, and we are trying to deprecate simpletest module: #2866082: [Plan] Roadmap for Simpletest

See #17 for some timing information. The majority of the time spent TTFB for the simpletest UI form is rendering, not test file discovery. The cache has a negligible effect on speed improvement.

This issue could be considered as part of the process of deprecating simpletest. See this related issue: #2863055: Move TestDiscovery out of simpletest module, minimize dependencies

Proposed resolution

Remove the caching behavior from TestDiscovery.

Remaining tasks

User interface changes

API changes

Data model changes

Original issue

This always bugged me. No reason to cache the information of available tests.

The caching only hinders development.

Related issues:
#1541674: Remove the registry
#1683884: Update, simplify, and speedup run-tests.sh for PSR-0, and allow to run all tests of a module

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

I guess that's a left-over of the times when Simplenews had to load and parse all .test files which was possibly very slow.

The thing is, we're not going back to parsing PHP files, but we are going to back to recursive directory scans of possibly hundreds (a real site with contrib modules) of directories. I'm not sure I agree with removing that cache without a performance test.

sun’s picture

Issue tags: +Testing system

What annoys me with that caching is that the only time I'm (reasonably) running tests is when I'm developing tests, and doing so most often means to create new test classes. Consequently, caches first need to be flushed every single time.

IMHO, Simpletest module should be optimized for developer experience. And with that, I mean the experience for active contributors, not passive developers.

But if performance tests are needed, one could do that with a D7 site, commenting out the cache_get() line. The only difference between D8 and D7 is the class autoloader. We are not manually parsing .test files in D7.

sun’s picture

sun’s picture

Issue summary: View changes

Updated issue summary.

sun’s picture

Issue summary: View changes
Issue tags: +DX (Developer Experience)
FileSize
7.92 KB

I could very well imagine that the discovery + info retrieval process could be made faster, too. (no changes in this patch though)

For example, via #697760: Replace getInfo() in tests with native phpDoc + annotations (following PHPUnit)

sun’s picture

Assigned: Unassigned » sun
sun’s picture

sun’s picture

Restarted from scratch + merged into #697760: Replace getInfo() in tests with native phpDoc + annotations (following PHPUnit)

Not marking this as a duplicate of that issue just yet though.

Status: Needs review » Needs work

The last submitted patch, 4: drupal8.simpletest-cache.4.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

Title: Remove caching of test classes » Remove caching of test classes in TestDiscovery
Assigned: sun » Unassigned
FileSize
2.35 KB

I spent some time verifying the UI display of skipped tests for #2296615: Accurately support multiple @groups per test class. The workflow was: Show the list of tests, clear cache, reload, clear cache, reload...

By far the slowest part of the simpletest UI form is the rendering, not the file scan.

run-tests.sh uses the same test discovery code, but it only performs the scan once, and really shouldn't be using a cache.

This patch just removes references to the cache from TestDiscovery.

Still to do:

  • Adjust test_discovery service definition so it doesn't need the file cache service.
  • Ensure that no part of core passes the cache service in.
Mile23’s picture

Status: Needs work » Needs review
Berdir’s picture

Yes, I also noticed the fact that rendering that table takes far more time than discovery.

Do you have any specific numbers on how long discovery vs rendering takes?

We could relatively easily add render caching for the table (we could even make it so that we use a pre_render callback and only call the discovery on a cache miss) but we have no reliable way for invalidation, maybe we could just keep the button and that invalidates a custom cache tag that we use on that table? The result is that we only have caching in the UI and caching that actually helps a lot more than what we have right now.

Status: Needs review » Needs work

The last submitted patch, 13: 1667822_13.patch, failed testing.

Mile23’s picture

Status: Needs work » Needs review
FileSize
1.71 KB
866 bytes

Web Profiler reports that admin/config/development/testing takes about 25 seconds TTFB.

Added is a patch that runs TestDiscovery a bunch of times to compare cached and uncached performance.

Setting $iterations to 1, the as-is cached getTestClasses() takes about 2.2 seconds. Uncached is about 1.8, which seems like a lot of overhead for managing a cache, so maybe my numbers are bad. Obviously this will vary with machine and OS and all kinds of other mysterious factors.

Anyway, if you advance it to 2 iterations it's obvious that you want the cache. :-) Two cached calls takes 2.2 seconds, uncached is 3.5.

So if we're never calling it more than once we want to kill the cache, but if we need to call it multiple times per request, then we like the cache.

However, we don't like the cache due to DX and UX, because as mentioned the cache persists too well and we have to figure out that we need to clear it, and then clear it in order to recognize a new test.

So we have two options:

  1. Decide that we will never call TestDiscovery->getTestClasses() more than once or else live with the consequences.
  2. Give SimpletestTestForm a way to discover tests without the cache. The patch below does this by invalidating the cache before TestDiscovery can use it.

Also: TestDiscovery::getTestClasses() instantiates a SimpleAnnotationReader but then never uses it.

Mile23’s picture

dawehner’s picture

Do we have a follow up with the work @Berdir suggested which is caching the actual HTML of that page?

Mile23’s picture

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

Soft-blocker for this issue could use some attention: #2893117: Improve HTML caching of Simpletest UI test form

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

Status: Needs review » Postponed

The solution in #2893117-21: Improve HTML caching of Simpletest UI test form removes the cache from TestDiscovery, so this issue might be redundant.

Mile23’s picture

Status: Postponed » Closed (outdated)

#2893117: Improve HTML caching of Simpletest UI test form is in, and it removes persistent caching of discovered tests. It keeps an internal cache of discovered tests per instance, to avoid repeated file system scans per request.

That makes this issue out of date.

Mile23’s picture

Status: Closed (outdated) » Postponed

This is re-opened because #2893117: Improve HTML caching of Simpletest UI test form was reverted, but postponed because that issue is really the solution.

Mile23’s picture

Status: Postponed » Closed (outdated)

And now we have #2893117: Improve HTML caching of Simpletest UI test form back again, which still makes this issue outdated by removing the caching of test classes from TestDiscovery, other than a per-request cache.