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

Merge branch '3436527-record-fize-size' into '11.x'

Initial implementation of collecting stylesheet and script size.

See merge request !7213
parents c9d53fd4 ace26dff
No related branches found
No related tags found
No related merge requests found
Pipeline #136891 passed
Pipeline: drupal

#136893

    <?php
    declare(strict_types=1);
    namespace Drupal\Tests\demo_umami\FunctionalJavascript;
    use Drupal\FunctionalJavascriptTests\PerformanceTestBase;
    use Drupal\Tests\PerformanceData;
    /**
    * Tests demo_umami profile performance.
    *
    * @group #slow
    */
    class AssetAggregationAcrossPagesTest extends PerformanceTestBase {
    /**
    * {@inheritdoc}
    */
    protected $profile = 'demo_umami';
    /**
    * Checks the asset requests made when the front and recipe pages are visited.
    */
    public function testFrontAndRecipesPages() {
    $performance_data = $this->doRequests();
    $this->assertSame(4, $performance_data->getStylesheetCount());
    $this->assertSame(90241, $performance_data->getStylesheetBytes());
    $this->assertSame(2, $performance_data->getScriptCount());
    $this->assertSame(14150, $performance_data->getScriptBytes());
    }
    /**
    * Checks the asset requests made when the front and recipe pages are visited.
    */
    public function testFrontAndRecipesPagesAuthenticated() {
    $user = $this->createUser();
    $this->drupalLogin($user);
    $this->rebuildAll();
    $performance_data = $this->doRequests();
    $this->assertSame(4, $performance_data->getStylesheetCount());
    $this->assertSame(94355, $performance_data->getStylesheetBytes());
    $this->assertSame(2, $performance_data->getScriptCount());
    $this->assertSame(264076, $performance_data->getScriptBytes());
    }
    /**
    * Helper to do requests so the above test methods stay in sync.
    */
    protected function doRequests(): PerformanceData {
    $performance_data = $this->collectPerformanceData(function () {
    $this->drupalGet('<front>');
    // Give additional time for the request and all assets to be returned
    // before making the next request.
    sleep(2);
    $this->drupalGet('articles');
    }, 'umamiFrontAndRecipePages');
    return $performance_data;
    }
    }
    ......@@ -35,6 +35,10 @@ public function testFrontPageAuthenticatedWarmCache(): void {
    $performance_data = $this->collectPerformanceData(function () {
    $this->drupalGet('<front>');
    }, 'authenticatedFrontPage');
    $this->assertSame(2, $performance_data->getStylesheetCount());
    $this->assertSame(47552, $performance_data->getStylesheetBytes());
    $this->assertSame(1, $performance_data->getScriptCount());
    $this->assertSame(132038, $performance_data->getScriptBytes());
    $expected_queries = [
    'SELECT "session" FROM "sessions" WHERE "sid" = "SESSION_ID" LIMIT 0, 1',
    ......
    ......@@ -63,6 +63,10 @@ public function testFrontPageHotCache() {
    $this->assertSame(0, $performance_data->getCacheTagChecksumCount());
    $this->assertSame(1, $performance_data->getCacheTagIsValidCount());
    $this->assertSame(0, $performance_data->getCacheTagInvalidationCount());
    $this->assertSame(1, $performance_data->getScriptCount());
    $this->assertSame(7075, $performance_data->getScriptBytes());
    $this->assertSame(2, $performance_data->getStylesheetCount());
    $this->assertSame(45495, $performance_data->getStylesheetBytes());
    }
    /**
    ......
    ......@@ -54,6 +54,8 @@ public function testAnonymous() {
    $this->drupalGet('');
    }, 'standardFrontPage');
    $this->assertNoJavaScript($performance_data);
    $this->assertSame(1, $performance_data->getStylesheetCount());
    $this->assertSame(7434, $performance_data->getStylesheetBytes());
    $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',
    ......@@ -97,6 +99,8 @@ public function testAnonymous() {
    $this->drupalGet('node/1');
    }, 'standardNodePage');
    $this->assertNoJavaScript($performance_data);
    $this->assertSame(1, $performance_data->getStylesheetCount());
    $this->assertSame(7159, $performance_data->getStylesheetBytes());
    $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',
    ......@@ -124,6 +128,8 @@ public function testAnonymous() {
    $this->drupalGet('user/' . $user->id());
    }, 'standardUserPage');
    $this->assertNoJavaScript($performance_data);
    $this->assertSame(1, $performance_data->getStylesheetCount());
    $this->assertSame(7159, $performance_data->getStylesheetBytes());
    $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',
    ......
    ......@@ -21,6 +21,16 @@ class PerformanceData {
    */
    protected int $scriptCount = 0;
    /**
    * The total stylesheet bytes requested.
    */
    protected int $stylesheetBytes = 0;
    /**
    * The total script bytes requested.
    */
    protected int $scriptBytes = 0;
    /**
    * The number of database queries recorded.
    */
    ......@@ -76,6 +86,16 @@ public function setStylesheetCount(int $count): void {
    $this->stylesheetCount = $count;
    }
    /**
    * Sets the stylesheet bytes.
    *
    * @param int $bytes
    * The stylesheet bytes recorded.
    */
    public function setStylesheetBytes(int $bytes): void {
    $this->stylesheetBytes = $bytes;
    }
    /**
    * Gets the stylesheet request count.
    *
    ......@@ -86,6 +106,16 @@ public function getStylesheetCount(): int {
    return $this->stylesheetCount;
    }
    /**
    * Gets the stylesheet bytes count.
    *
    * @return int
    * The stylesheet bytes recorded.
    */
    public function getStylesheetBytes(): int {
    return $this->stylesheetBytes;
    }
    /**
    * Sets the script request count.
    *
    ......@@ -96,6 +126,16 @@ public function setScriptCount(int $count) {
    $this->scriptCount = $count;
    }
    /**
    * Sets the script bytes.
    *
    * @param int $bytes
    * The script bytes recorded.
    */
    public function setScriptBytes(int $bytes): void {
    $this->scriptBytes = $bytes;
    }
    /**
    * Gets the script request count.
    *
    ......@@ -106,6 +146,16 @@ public function getScriptCount(): int {
    return $this->scriptCount;
    }
    /**
    * Gets the script bytes count.
    *
    * @return int
    * The script bytes recorded.
    */
    public function getScriptBytes(): int {
    return $this->scriptBytes;
    }
    /**
    * Logs a database query.
    *
    ......
    ......@@ -348,18 +348,62 @@ protected function processChromeDriverPerformanceLogs(?string $service_name): Pe
    private function collectNetworkData(array $messages, PerformanceData $performance_data): void {
    $stylesheet_count = 0;
    $script_count = 0;
    $stylesheet_bytes = 0;
    $script_bytes = 0;
    $stylesheet_urls = [];
    $script_urls = [];
    // Collect the CSS and JavaScript responses from the network log build an
    // associative array so that if multiple page or AJAX requests have
    // requested styles and scripts, only unique files will be counted.
    foreach ($messages as $message) {
    if ($message['method'] === 'Network.responseReceived') {
    if ($message['params']['type'] === 'Stylesheet') {
    $stylesheet_count++;
    $url = $message['params']['response']['url'];
    $stylesheet_urls[$url] = $url;
    }
    if ($message['params']['type'] === 'Script') {
    $script_count++;
    $url = $message['params']['response']['url'];
    $script_urls[$url] = $url;
    }
    }
    }
    // Get the actual files from disk when calculating filesize, to ensure
    // consistency between testing environments. The performance log has
    // 'encodedDataLength' for network requests, however in the case that the
    // file has already been requested by the browser, this will be the length
    // of a HEAD response for 304 not modified or similar. Additionally, core's
    // aggregation adds the basepath to CSS aggregates, resulting in slightly
    // different file sizes depending on whether tests run in a subdirectory or
    // not.
    foreach ($stylesheet_urls as $url) {
    $stylesheet_count++;
    if ($GLOBALS['base_path'] === '/') {
    $filename = ltrim(parse_url($url, PHP_URL_PATH), '/');
    $stylesheet_bytes += strlen(file_get_contents($filename));
    }
    else {
    $filename = str_replace($GLOBALS['base_path'], '', parse_url($url, PHP_URL_PATH));
    // Strip the basepath from the contents of the file so that tests
    // running in a subdirectory get the same results.
    $stylesheet_bytes += strlen(str_replace($GLOBALS['base_path'], '/', file_get_contents($filename)));
    }
    }
    foreach ($script_urls as $url) {
    $script_count++;
    if ($GLOBALS['base_path'] === '/') {
    $filename = ltrim(parse_url($url, PHP_URL_PATH), '/');
    }
    else {
    $filename = str_replace($GLOBALS['base_path'], '', parse_url($url, PHP_URL_PATH));
    }
    $script_bytes += strlen(file_get_contents($filename));
    }
    $performance_data->setStylesheetCount($stylesheet_count);
    $performance_data->setStylesheetBytes($stylesheet_bytes);
    $performance_data->setScriptCount($script_count);
    $performance_data->setScriptBytes($script_bytes);
    }
    /**
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment