function PerformanceTestTrait::collectPerformanceData

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/PerformanceTestTrait.php \Drupal\Tests\PerformanceTestTrait::collectPerformanceData()

Executes a callable and collects performance data.

Parameters

callable $callable: A callable, for example ::drupalGet().

string|null $service_name: An optional human readable identifier to enable sending traces to an Open Telemetry endpoint (if configured).

Return value

\Drupal\Tests\PerformanceData A PerformanceData value object.

14 calls to PerformanceTestTrait::collectPerformanceData()
AssetAggregationAcrossPagesTest::doRequests in core/profiles/demo_umami/tests/src/FunctionalJavascript/AssetAggregationAcrossPagesTest.php
Helper to do requests so the above test methods stay in sync.
OpenTelemetryAuthenticatedPerformanceTest::testFrontPageAuthenticatedWarmCache in core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryAuthenticatedPerformanceTest.php
Logs front page tracing data with an authenticated user and warm cache.
OpenTelemetryFrontPagePerformanceTest::testFrontPageColdCache in core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryFrontPagePerformanceTest.php
Logs front page tracing data with a cold cache.
OpenTelemetryFrontPagePerformanceTest::testFrontPageCoolCache in core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryFrontPagePerformanceTest.php
Logs front page tracing data with a lukewarm cache.
OpenTelemetryFrontPagePerformanceTest::testFrontPageHotCache in core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryFrontPagePerformanceTest.php
Logs front page tracing data with a hot cache.

... See full list

File

core/tests/Drupal/Tests/PerformanceTestTrait.php, line 99

Class

PerformanceTestTrait
Provides various methods to aid in collecting performance data during tests.

Namespace

Drupal\Tests

Code

public function collectPerformanceData(callable $callable, ?string $service_name = NULL) : PerformanceData {
    // Clear all existing performance logs before collecting new data. This is
    // necessary because responses are returned back to tests prior to image
    // and asset responses are returning to the browser, and before
    // post-response tasks are guaranteed to have run. Assume that if there is
    // no performance data logged by the child request within one second, that
    // this means everything has finished.
    $collection = \Drupal::keyValue('performance_test');
    while ($collection->get('performance_test_data')) {
        $collection->deleteAll();
        sleep(1);
    }
    $session = $this->getSession();
    $session->getDriver()
        ->getWebDriverSession()
        ->log('performance');
    $collection->deleteAll();
    $return = $callable();
    $performance_data = $this->processChromeDriverPerformanceLogs($service_name);
    if (isset($return)) {
        $performance_data->setReturnValue($return);
    }
    $performance_test_data = $collection->get('performance_test_data');
    if ($performance_test_data) {
        // This property is set by \Drupal\Core\Test\TestSetupTrait and is needed.
        if (!isset($this->databasePrefix)) {
            throw new \Exception('Cannot log queries without knowing the database prefix.');
        }
        // Separate queries into two buckets, one for queries from the cache
        // backend, and one for everything else (including those for cache tags).
        $cache_get_count = 0;
        $cache_set_count = 0;
        $cache_delete_count = 0;
        $cache_tag_is_valid_count = 0;
        $cache_tag_invalidation_count = 0;
        $cache_tag_checksum_count = 0;
        foreach ($performance_test_data['database_events'] as $event) {
            // Don't log queries from the database cache backend because they're
            // logged separately as cache operations.
            if (!static::isDatabaseCache($event)) {
                // Make the query easier to read and log it.
                static::logQuery($performance_data, str_replace([
                    $this->databasePrefix,
                    "\r\n",
                    "\r",
                    "\n",
                ], [
                    '',
                    ' ',
                    ' ',
                    ' ',
                ], $event->queryString), $event->args);
            }
        }
        foreach ($performance_test_data['cache_operations'] as $operation) {
            if (in_array($operation['operation'], [
                'get',
                'getMultiple',
            ], TRUE)) {
                $cache_get_count++;
            }
            elseif (in_array($operation['operation'], [
                'set',
                'setMultiple',
            ], TRUE)) {
                $cache_set_count++;
            }
            elseif (in_array($operation['operation'], [
                'delete',
                'deleteMultiple',
            ], TRUE)) {
                $cache_delete_count++;
            }
        }
        foreach ($performance_test_data['cache_tag_operations'] as $operation) {
            match ($operation['operation']) {    CacheTagOperation::GetCurrentChecksum => $cache_tag_checksum_count++,
                CacheTagOperation::IsValid => $cache_tag_is_valid_count++,
                CacheTagOperation::InvalidateTags => $cache_tag_invalidation_count++,
            
            };
        }
        $performance_data->setCacheGetCount($cache_get_count);
        $performance_data->setCacheSetCount($cache_set_count);
        $performance_data->setCacheDeleteCount($cache_delete_count);
        $performance_data->setCacheTagChecksumCount($cache_tag_checksum_count);
        $performance_data->setCacheTagIsValidCount($cache_tag_is_valid_count);
        $performance_data->setCacheTagInvalidationCount($cache_tag_invalidation_count);
    }
    return $performance_data;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.