function PerformanceTestTrait::assertMetricsByName

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

Asserts metrics against the stored expectations.

Expected queries are stored in the folder named TestClassNameAssertions in the same directory.

Parameters

string $name: A identifier for the expected queries. Must be unique for the given test.

\Drupal\Tests\PerformanceData $performance_data: An instance of the performance data value object.

array $metrics: Customize the number of default metrics to assert in case no data is stored yet.

20 calls to PerformanceTestTrait::assertMetricsByName()
JsonApiPerformanceTest::testGetIndividual in core/modules/jsonapi/tests/src/FunctionalJavascript/JsonApiPerformanceTest.php
Tests performance of the navigation toolbar.
NodePerformanceTest::testPromotedContentCacheInvalidation in core/modules/node/tests/src/FunctionalJavascript/NodePerformanceTest.php
Tests the impact of node cache invalidation on the promoted content view.
NodePerformanceTest::testPromotedContentPage in core/modules/node/tests/src/FunctionalJavascript/NodePerformanceTest.php
Tests performance for the promoted content page view.
OpenTelemetryPerformanceTest::doTestFrontAndRecipesPages in core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryPerformanceTest.php
Checks the asset requests made when the front and recipe pages are visited.
OpenTelemetryPerformanceTest::doTestFrontAndRecipesPagesAuthenticated in core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryPerformanceTest.php
Checks the front and recipe page asset requests as an authenticated user.

... See full list

File

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

Class

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

Namespace

Drupal\Tests

Code

protected function assertMetricsByName(string $name, PerformanceData $performance_data, array $metrics = []) : void {
  $filename = $this->getExpectationsFilename($name, 'yml');
  $content = file_exists($filename) ? file_get_contents($filename) : '';
  if (!empty($content)) {
    $expected = Yaml::decode($content);
  }
  else {
    // No existing data found. Use a default list of metrics to update if no
    // custom list is provided.
    $expected = array_flip($metrics) ?: [
      'QueryCount' => 0,
      'CacheGetCount' => 0,
      'CacheGetCountByBin' => [],
      'CacheSetCount' => 0,
      'CacheDeleteCount' => 0,
      'CacheTagInvalidationCount' => 0,
      'CacheTagLookupQueryCount' => 0,
      'CacheTagGroupedLookups' => [],
      'ScriptCount' => 0,
      'ScriptBytes' => 0,
      'StylesheetCount' => 0,
      'StylesheetBytes' => 0,
    ];
  }
  if (empty($content) || $this->shouldUpdate()) {
    foreach ($expected as $name => $metric) {
      $expected[$name] = $performance_data->{"get{$name}"}();
    }
    file_put_contents($filename, Yaml::encode($expected));
  }
  else {
    $this->assertMetrics($expected, $performance_data);
  }
}

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