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.

18 calls to PerformanceTestTrait::assertMetricsByName()
JsonApiPerformanceTest::testGetIndividual in core/modules/jsonapi/tests/src/FunctionalJavascript/JsonApiPerformanceTest.php
Tests performance of the navigation toolbar.
MultipleRequestsPerformanceTest::doTestFrontAndRecipesPages in core/profiles/demo_umami/tests/src/FunctionalJavascript/MultipleRequestsPerformanceTest.php
Checks the asset requests made when the front and recipe pages are visited.
MultipleRequestsPerformanceTest::doTestFrontAndRecipesPagesAuthenticated in core/profiles/demo_umami/tests/src/FunctionalJavascript/MultipleRequestsPerformanceTest.php
Checks the front and recipe page asset requests as an authenticated user.
MultipleRequestsPerformanceTest::doTestFrontAndRecipesPagesEditor in core/profiles/demo_umami/tests/src/FunctionalJavascript/MultipleRequestsPerformanceTest.php
Checks the front and recipe page asset requests as an editor.
MultipleRequestsPerformanceTest::doTestLogin in core/profiles/demo_umami/tests/src/FunctionalJavascript/MultipleRequestsPerformanceTest.php
Tests the performance of logging in.

... See full list

File

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

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) {
      $new_metrics = $performance_data->{"get{$name}"}();
      if (str_ends_with($name, 'Bytes')) {
        // Update byte metrics only if they differ from the current
        // value by more than the allowed tolerance.
        $tolerance = $this->calculateAllowedByteTolerance($metric);
        if (abs($metric - $new_metrics) > $tolerance) {
          $expected[$name] = $new_metrics;
        }
      }
      else {
        $expected[$name] = $new_metrics;
      }
    }
    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.