Skip to content

Commit

Permalink
Closes #7161: Host Google Fonts - Measure time (#7163)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanna-meda authored Dec 5, 2024
2 parents b96427d + 37ad77a commit 0c3c6fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function rewrite_fonts( $html ): string {
return $html;
}

// For test purposes.
$start_time = microtime( true );

$html_nocomments = $this->hide_comments( $html );

$v1_fonts = $this->find( '<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])(?<url>(?:https?:)?\/\/fonts\.googleapis\.com\/css[^\d](?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments );
Expand All @@ -80,6 +83,11 @@ public function rewrite_fonts( $html ): string {
return $html;
}

// Count fonts - for test purposes.
$total_v1 = count( $v1_fonts );
$total_v2 = count( $v2_fonts );
$total_fonts = $total_v1 + $total_v2;

foreach ( $v1_fonts as $font ) {
$html = $this->replace_font( $font, $html );
}
Expand All @@ -92,6 +100,13 @@ public function rewrite_fonts( $html ): string {
$html = $this->remove_preconnect_and_prefetch( $html );
}

// End time measurement.
$end_time = microtime( true );

// Log the total execution time and number of fonts processed, with breakdown.
$duration = $end_time - $start_time;
Logger::debug( "Total execution time for Host Google Fonts Feature in seconds -- $duration. Fonts processed: $total_fonts | Total v1: $total_v1 | Total v2: $total_v2", [ 'Host Fonts Locally' ] );

return $html;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testShouldReturnAsExpected( $config, $expected ) {

$this->assertSame(
$expected['html'],
apply_filters( 'rocket_buffer', $config['html'] ) // @phpstan-ignore-line
wpm_apply_filters_typed('string', 'rocket_buffer', $config['html'])
);
}

Expand Down

0 comments on commit 0c3c6fa

Please sign in to comment.