Skip to content
Snippets Groups Projects
Commit 43cc332a authored by catch's avatar catch
Browse files

Issue #3477191 by goz, pavel.bulat, benjifisher, catch: Add...

Issue #3477191 by goz, pavel.bulat, benjifisher, catch: Add PerformanceTestTrait::assertMetrics() so it is easier to write performance tests
parent fe830ea9
No related branches found
No related tags found
4 merge requests!11197Issue #3506427 by eduardo morales alberti: Remove responsive_image.ajax from hook,!11131[10.4.x-only-DO-NOT-MERGE]: Issue ##2842525 Ajax attached to Views exposed filter form does not trigger callbacks,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!617Issue #3043725: Provide a Entity Handler for user cancelation
Pipeline #392379 passed with warnings
Pipeline: drupal

#392388

    Pipeline: drupal

    #392385

      Pipeline: drupal

      #392380

        ...@@ -25,10 +25,14 @@ public function testFrontAndRecipesPages(): void { ...@@ -25,10 +25,14 @@ public function testFrontAndRecipesPages(): void {
        $performance_data = $this->collectPerformanceData(function () { $performance_data = $this->collectPerformanceData(function () {
        $this->doRequests(); $this->doRequests();
        }, 'umamiFrontAndRecipePages'); }, 'umamiFrontAndRecipePages');
        $this->assertSame(6, $performance_data->getStylesheetCount());
        $this->assertLessThan(125000, $performance_data->getStylesheetBytes()); $expected = [
        $this->assertSame(1, $performance_data->getScriptCount()); 'ScriptCount' => 1,
        $this->assertLessThan(12000, $performance_data->getScriptBytes()); 'ScriptBytes' => 11850,
        'StylesheetCount' => 6,
        'StylesheetBytes' => 124450,
        ];
        $this->assertMetrics($expected, $performance_data);
        } }
        /** /**
        ...@@ -41,10 +45,15 @@ public function testFrontAndRecipesPagesAuthenticated(): void { ...@@ -41,10 +45,15 @@ public function testFrontAndRecipesPagesAuthenticated(): void {
        $performance_data = $this->collectPerformanceData(function () { $performance_data = $this->collectPerformanceData(function () {
        $this->doRequests(); $this->doRequests();
        }, 'umamiFrontAndRecipePagesAuthenticated'); }, 'umamiFrontAndRecipePagesAuthenticated');
        $this->assertSame(6, $performance_data->getStylesheetCount());
        $this->assertLessThan(132500, $performance_data->getStylesheetBytes()); $expected = [
        $this->assertSame(2, $performance_data->getScriptCount()); 'ScriptCount' => 2,
        'ScriptBytes' => 249650,
        'StylesheetCount' => 6,
        ];
        $this->assertMetrics($expected, $performance_data);
        $this->assertLessThan(250000, $performance_data->getScriptBytes()); $this->assertLessThan(250000, $performance_data->getScriptBytes());
        } }
        /** /**
        ......
        ...@@ -36,10 +36,6 @@ public function testFrontPageAuthenticatedWarmCache(): void { ...@@ -36,10 +36,6 @@ public function testFrontPageAuthenticatedWarmCache(): void {
        $performance_data = $this->collectPerformanceData(function () { $performance_data = $this->collectPerformanceData(function () {
        $this->drupalGet('<front>'); $this->drupalGet('<front>');
        }, 'authenticatedFrontPage'); }, 'authenticatedFrontPage');
        $this->assertSame(2, $performance_data->getStylesheetCount());
        $this->assertLessThan(44000, $performance_data->getStylesheetBytes());
        $this->assertSame(1, $performance_data->getScriptCount());
        $this->assertLessThan(125000, $performance_data->getScriptBytes());
        $expected_queries = [ $expected_queries = [
        'SELECT "session" FROM "sessions" WHERE "sid" = "SESSION_ID" LIMIT 0, 1', 'SELECT "session" FROM "sessions" WHERE "sid" = "SESSION_ID" LIMIT 0, 1',
        ...@@ -49,13 +45,21 @@ public function testFrontPageAuthenticatedWarmCache(): void { ...@@ -49,13 +45,21 @@ public function testFrontPageAuthenticatedWarmCache(): void {
        ]; ];
        $recorded_queries = $performance_data->getQueries(); $recorded_queries = $performance_data->getQueries();
        $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
        $this->assertSame(4, $performance_data->getQueryCount());
        $this->assertSame(42, $performance_data->getCacheGetCount()); $expected = [
        $this->assertSame(0, $performance_data->getCacheSetCount()); 'QueryCount' => 4,
        $this->assertSame(0, $performance_data->getCacheDeleteCount()); 'CacheGetCount' => 42,
        $this->assertSame(0, $performance_data->getCacheTagChecksumCount()); 'CacheSetCount' => 0,
        $this->assertSame(11, $performance_data->getCacheTagIsValidCount()); 'CacheDeleteCount' => 0,
        $this->assertSame(0, $performance_data->getCacheTagInvalidationCount()); 'CacheTagChecksumCount' => 0,
        'CacheTagIsValidCount' => 11,
        'CacheTagInvalidationCount' => 0,
        'ScriptCount' => 1,
        'ScriptBytes' => 123850,
        'StylesheetCount' => 2,
        'StylesheetBytes' => 43600,
        ];
        $this->assertMetrics($expected, $performance_data);
        } }
        } }
        ...@@ -67,17 +67,21 @@ protected function testFrontPageHotCache(): void { ...@@ -67,17 +67,21 @@ protected function testFrontPageHotCache(): void {
        $expected_queries = []; $expected_queries = [];
        $recorded_queries = $performance_data->getQueries(); $recorded_queries = $performance_data->getQueries();
        $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
        $this->assertSame(0, $performance_data->getQueryCount());
        $this->assertSame(1, $performance_data->getCacheGetCount()); $expected = [
        $this->assertSame(0, $performance_data->getCacheSetCount()); 'QueryCount' => 0,
        $this->assertSame(0, $performance_data->getCacheDeleteCount()); 'CacheGetCount' => 1,
        $this->assertSame(0, $performance_data->getCacheTagChecksumCount()); 'CacheSetCount' => 0,
        $this->assertSame(1, $performance_data->getCacheTagIsValidCount()); 'CacheDeleteCount' => 0,
        $this->assertSame(0, $performance_data->getCacheTagInvalidationCount()); 'CacheTagChecksumCount' => 0,
        $this->assertSame(1, $performance_data->getScriptCount()); 'CacheTagIsValidCount' => 1,
        $this->assertLessThan(12000, $performance_data->getScriptBytes()); 'CacheTagInvalidationCount' => 0,
        $this->assertSame(2, $performance_data->getStylesheetCount()); 'ScriptCount' => 1,
        $this->assertLessThan(42000, $performance_data->getStylesheetBytes()); 'ScriptBytes' => 11850,
        'StylesheetCount' => 2,
        'StylesheetBytes' => 41200,
        ];
        $this->assertMetrics($expected, $performance_data);
        } }
        /** /**
        ......
        ...@@ -61,12 +61,15 @@ protected function testNodePageHotCache(): void { ...@@ -61,12 +61,15 @@ protected function testNodePageHotCache(): void {
        $this->drupalGet('node/1'); $this->drupalGet('node/1');
        }, 'umamiNodePageHotCache'); }, 'umamiNodePageHotCache');
        $this->assertSession()->pageTextContains('quiche'); $this->assertSession()->pageTextContains('quiche');
        $this->assertSame($performance_data->getQueryCount(), 0); $expected = [
        $this->assertSame($performance_data->getCacheGetCount(), 1); 'QueryCount' => 0,
        $this->assertSame($performance_data->getCacheSetCount(), 0); 'CacheGetCount' => 1,
        $this->assertSame($performance_data->getCacheDeleteCount(), 0); 'CacheSetCount' => 0,
        $this->assertSame(0, $performance_data->getCacheTagChecksumCount()); 'CacheDeleteCount' => 0,
        $this->assertSame(1, $performance_data->getCacheTagIsValidCount()); 'CacheTagChecksumCount' => 0,
        'CacheTagIsValidCount' => 1,
        ];
        $this->assertMetrics($expected, $performance_data);
        } }
        /** /**
        ......
        ...@@ -84,8 +84,6 @@ protected function testAnonymous(): void { ...@@ -84,8 +84,6 @@ protected function testAnonymous(): void {
        $this->drupalGet(''); $this->drupalGet('');
        }, 'standardFrontPage'); }, 'standardFrontPage');
        $this->assertNoJavaScript($performance_data); $this->assertNoJavaScript($performance_data);
        $this->assertSame(1, $performance_data->getStylesheetCount());
        $this->assertLessThan(3500, $performance_data->getStylesheetBytes());
        $expected_queries = [ $expected_queries = [
        'SELECT "base_table"."id" AS "id", "base_table"."path" AS "path", "base_table"."alias" AS "alias", "base_table"."langcode" AS "langcode" FROM "path_alias" "base_table" WHERE ("base_table"."status" = 1) AND ("base_table"."alias" LIKE "/node" ESCAPE ' . "'\\\\'" . ') AND ("base_table"."langcode" IN ("en", "und")) ORDER BY "base_table"."langcode" ASC, "base_table"."id" DESC', 'SELECT "base_table"."id" AS "id", "base_table"."path" AS "path", "base_table"."alias" AS "alias", "base_table"."langcode" AS "langcode" FROM "path_alias" "base_table" WHERE ("base_table"."status" = 1) AND ("base_table"."alias" LIKE "/node" ESCAPE ' . "'\\\\'" . ') AND ("base_table"."langcode" IN ("en", "und")) ORDER BY "base_table"."langcode" ASC, "base_table"."id" DESC',
        ...@@ -127,21 +125,24 @@ protected function testAnonymous(): void { ...@@ -127,21 +125,24 @@ protected function testAnonymous(): void {
        ]; ];
        $recorded_queries = $performance_data->getQueries(); $recorded_queries = $performance_data->getQueries();
        $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
        $this->assertSame(36, $performance_data->getQueryCount()); $expected = [
        $this->assertSame(123, $performance_data->getCacheGetCount()); 'QueryCount' => 36,
        $this->assertSame(45, $performance_data->getCacheSetCount()); 'CacheGetCount' => 123,
        $this->assertSame(0, $performance_data->getCacheDeleteCount()); 'CacheSetCount' => 45,
        $this->assertSame(37, $performance_data->getCacheTagChecksumCount()); 'CacheDeleteCount' => 0,
        $this->assertSame(43, $performance_data->getCacheTagIsValidCount()); 'CacheTagChecksumCount' => 37,
        $this->assertSame(0, $performance_data->getCacheTagInvalidationCount()); 'CacheTagIsValidCount' => 43,
        'CacheTagInvalidationCount' => 0,
        'StylesheetCount' => 1,
        'StylesheetBytes' => 3450,
        ];
        $this->assertMetrics($expected, $performance_data);
        // Test node page. // Test node page.
        $performance_data = $this->collectPerformanceData(function () { $performance_data = $this->collectPerformanceData(function () {
        $this->drupalGet('node/1'); $this->drupalGet('node/1');
        }, 'standardNodePage'); }, 'standardNodePage');
        $this->assertNoJavaScript($performance_data); $this->assertNoJavaScript($performance_data);
        $this->assertSame(1, $performance_data->getStylesheetCount());
        $this->assertLessThan(3500, $performance_data->getStylesheetBytes());
        $expected_queries = [ $expected_queries = [
        'SELECT "base_table"."id" AS "id", "base_table"."path" AS "path", "base_table"."alias" AS "alias", "base_table"."langcode" AS "langcode" FROM "path_alias" "base_table" WHERE ("base_table"."status" = 1) AND ("base_table"."alias" LIKE "/node/1" ESCAPE ' . "'\\\\'" . ') AND ("base_table"."langcode" IN ("en", "und")) ORDER BY "base_table"."langcode" ASC, "base_table"."id" DESC', 'SELECT "base_table"."id" AS "id", "base_table"."path" AS "path", "base_table"."alias" AS "alias", "base_table"."langcode" AS "langcode" FROM "path_alias" "base_table" WHERE ("base_table"."status" = 1) AND ("base_table"."alias" LIKE "/node/1" ESCAPE ' . "'\\\\'" . ') AND ("base_table"."langcode" IN ("en", "und")) ORDER BY "base_table"."langcode" ASC, "base_table"."id" DESC',
        ...@@ -157,13 +158,18 @@ protected function testAnonymous(): void { ...@@ -157,13 +158,18 @@ protected function testAnonymous(): void {
        ]; ];
        $recorded_queries = $performance_data->getQueries(); $recorded_queries = $performance_data->getQueries();
        $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
        $this->assertSame(10, $performance_data->getQueryCount());
        $this->assertSame(93, $performance_data->getCacheGetCount());
        $this->assertSame(16, $performance_data->getCacheSetCount());
        $this->assertSame(0, $performance_data->getCacheDeleteCount());
        $this->assertCountBetween(24, 25, $performance_data->getCacheTagChecksumCount()); $this->assertCountBetween(24, 25, $performance_data->getCacheTagChecksumCount());
        $this->assertCountBetween(39, 40, $performance_data->getCacheTagIsValidCount()); $this->assertCountBetween(39, 40, $performance_data->getCacheTagIsValidCount());
        $this->assertSame(0, $performance_data->getCacheTagInvalidationCount()); $expected = [
        'QueryCount' => 10,
        'CacheGetCount' => 93,
        'CacheSetCount' => 16,
        'CacheDeleteCount' => 0,
        'CacheTagInvalidationCount' => 0,
        'StylesheetCount' => 1,
        'StylesheetBytes' => 3150,
        ];
        $this->assertMetrics($expected, $performance_data);
        // Test user profile page. // Test user profile page.
        $this->user = $this->drupalCreateUser(); $this->user = $this->drupalCreateUser();
        ...@@ -171,8 +177,6 @@ protected function testAnonymous(): void { ...@@ -171,8 +177,6 @@ protected function testAnonymous(): void {
        $this->drupalGet('user/' . $this->user->id()); $this->drupalGet('user/' . $this->user->id());
        }, 'standardUserPage'); }, 'standardUserPage');
        $this->assertNoJavaScript($performance_data); $this->assertNoJavaScript($performance_data);
        $this->assertSame(1, $performance_data->getStylesheetCount());
        $this->assertLessThan(3500, $performance_data->getStylesheetBytes());
        $expected_queries = [ $expected_queries = [
        'SELECT "base_table"."id" AS "id", "base_table"."path" AS "path", "base_table"."alias" AS "alias", "base_table"."langcode" AS "langcode" FROM "path_alias" "base_table" WHERE ("base_table"."status" = 1) AND ("base_table"."alias" LIKE "/user/2" ESCAPE ' . "'\\\\'" . ') AND ("base_table"."langcode" IN ("en", "und")) ORDER BY "base_table"."langcode" ASC, "base_table"."id" DESC', 'SELECT "base_table"."id" AS "id", "base_table"."path" AS "path", "base_table"."alias" AS "alias", "base_table"."langcode" AS "langcode" FROM "path_alias" "base_table" WHERE ("base_table"."status" = 1) AND ("base_table"."alias" LIKE "/user/2" ESCAPE ' . "'\\\\'" . ') AND ("base_table"."langcode" IN ("en", "und")) ORDER BY "base_table"."langcode" ASC, "base_table"."id" DESC',
        ...@@ -192,13 +196,18 @@ protected function testAnonymous(): void { ...@@ -192,13 +196,18 @@ protected function testAnonymous(): void {
        ]; ];
        $recorded_queries = $performance_data->getQueries(); $recorded_queries = $performance_data->getQueries();
        $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
        $this->assertSame(14, $performance_data->getQueryCount()); $expected = [
        $this->assertSame(78, $performance_data->getCacheGetCount()); 'QueryCount' => 14,
        $this->assertSame(17, $performance_data->getCacheSetCount()); 'CacheGetCount' => 78,
        $this->assertSame(0, $performance_data->getCacheDeleteCount()); 'CacheSetCount' => 17,
        $this->assertSame(23, $performance_data->getCacheTagChecksumCount()); 'CacheDeleteCount' => 0,
        $this->assertSame(32, $performance_data->getCacheTagIsValidCount()); 'CacheTagChecksumCount' => 23,
        $this->assertSame(0, $performance_data->getCacheTagInvalidationCount()); 'CacheTagIsValidCount' => 32,
        'CacheTagInvalidationCount' => 0,
        'StylesheetCount' => 1,
        'StylesheetBytes' => 3150,
        ];
        $this->assertMetrics($expected, $performance_data);
        } }
        /** /**
        ...@@ -243,13 +252,16 @@ protected function testLogin(): void { ...@@ -243,13 +252,16 @@ protected function testLogin(): void {
        ]; ];
        $recorded_queries = $performance_data->getQueries(); $recorded_queries = $performance_data->getQueries();
        $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
        $this->assertSame(17, $performance_data->getQueryCount()); $expected = [
        $this->assertSame(84, $performance_data->getCacheGetCount()); 'QueryCount' => 17,
        $this->assertSame(1, $performance_data->getCacheSetCount()); 'CacheGetCount' => 84,
        $this->assertSame(1, $performance_data->getCacheDeleteCount()); 'CacheSetCount' => 1,
        $this->assertSame(1, $performance_data->getCacheTagChecksumCount()); 'CacheDeleteCount' => 1,
        $this->assertSame(37, $performance_data->getCacheTagIsValidCount()); 'CacheTagChecksumCount' => 1,
        $this->assertSame(0, $performance_data->getCacheTagInvalidationCount()); 'CacheTagIsValidCount' => 37,
        'CacheTagInvalidationCount' => 0,
        ];
        $this->assertMetrics($expected, $performance_data);
        $this->drupalLogout(); $this->drupalLogout();
        } }
        ...@@ -297,13 +309,16 @@ protected function testLoginBlock(): void { ...@@ -297,13 +309,16 @@ protected function testLoginBlock(): void {
        ]; ];
        $recorded_queries = $performance_data->getQueries(); $recorded_queries = $performance_data->getQueries();
        $this->assertSame($expected_queries, $recorded_queries); $this->assertSame($expected_queries, $recorded_queries);
        $this->assertSame(18, $performance_data->getQueryCount()); $expected = [
        $this->assertSame(105, $performance_data->getCacheGetCount()); 'QueryCount' => 18,
        $this->assertSame(1, $performance_data->getCacheSetCount()); 'CacheGetCount' => 105,
        $this->assertSame(1, $performance_data->getCacheDeleteCount()); 'CacheSetCount' => 1,
        $this->assertSame(1, $performance_data->getCacheTagChecksumCount()); 'CacheDeleteCount' => 1,
        $this->assertSame(43, $performance_data->getCacheTagIsValidCount()); 'CacheTagChecksumCount' => 1,
        $this->assertSame(0, $performance_data->getCacheTagInvalidationCount()); 'CacheTagIsValidCount' => 43,
        'CacheTagInvalidationCount' => 0,
        ];
        $this->assertMetrics($expected, $performance_data);
        } }
        /** /**
        ......
        ...@@ -590,16 +590,21 @@ private function openTelemetryTracing(array $messages, string $service_name): vo ...@@ -590,16 +590,21 @@ private function openTelemetryTracing(array $messages, string $service_name): vo
        * Maximum value. * Maximum value.
        * @param int $actual * @param int $actual
        * The number to assert against. * The number to assert against.
        * @param string $message
        * The message to display.
        * *
        * @return void * @return void
        * *
        * @throws \PHPUnit\Framework\ExpectationFailedException * @throws \PHPUnit\Framework\ExpectationFailedException
        */ */
        protected function assertCountBetween(int $min, int $max, int $actual) { protected function assertCountBetween(int $min, int $max, int $actual, string $message = '') {
        if (!empty($message)) {
        $message .= " ";
        }
        static::assertThat( static::assertThat(
        $actual, $actual,
        static::logicalAnd(static::greaterThanOrEqual($min), static::lessThanOrEqual($max)), static::logicalAnd(static::greaterThanOrEqual($min), static::lessThanOrEqual($max)),
        "$actual is greater or equal to $min and is smaller or equal to $max", "$message$actual is greater or equal to $min and is smaller or equal to $max",
        ); );
        } }
        ...@@ -621,4 +626,59 @@ protected static function isDatabaseCache(DatabaseEvent $event): bool { ...@@ -621,4 +626,59 @@ protected static function isDatabaseCache(DatabaseEvent $event): bool {
        return FALSE; return FALSE;
        } }
        /**
        * Assert metrics from a performance data value object.
        *
        * @param array $expected
        * The expected metrics.
        * @param \Drupal\Tests\PerformanceData $performance_data
        * An instance of the performance data value object.
        *
        * @return void
        */
        protected function assertMetrics(
        array $expected,
        PerformanceData $performance_data,
        ): void {
        // Allow those metrics to have a range of +/- 50 bytes, so small changes
        // are not significant enough to break tests.
        $assertRange = [
        'ScriptBytes',
        'StylesheetBytes',
        ];
        foreach ($expected as $name => $metric) {
        if (in_array($name, $assertRange)) {
        $this->assertCountBetween($metric - 50, $metric + 50, $performance_data->{"get$name"}(), "Asserting $name");
        }
        else {
        $this->assertSame($metric, $performance_data->{"get$name"}(), "Asserting $name");
        }
        }
        }
        /**
        * Get metrics from a performance data value object.
        *
        * @param \Drupal\Tests\PerformanceData $performance_data
        * An instance of the performance data value object.
        *
        * @return array
        * An array of metrics.
        */
        protected function getMetrics(PerformanceData $performance_data): array {
        return [
        'StylesheetCount' => $performance_data->getStylesheetCount(),
        'ScriptCount' => $performance_data->getScriptCount(),
        'StylesheetBytes' => $performance_data->getStylesheetBytes(),
        'ScriptBytes' => $performance_data->getScriptBytes(),
        'QueryCount' => $performance_data->getQueryCount(),
        'CacheGetCount' => $performance_data->getCacheGetCount(),
        'CacheSetCount' => $performance_data->getCacheSetCount(),
        'CacheDeleteCount' => $performance_data->getCacheDeleteCount(),
        'CacheTagChecksumCount' => $performance_data->getCacheTagChecksumCount(),
        'CacheTagIsValidCount' => $performance_data->getCacheTagIsValidCount(),
        'CacheTagInvalidationCount' => $performance_data->getCacheTagInvalidationCount(),
        ];
        }
        } }
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Finish editing this message first!
        Please register or to comment