Skip to content

Commit

Permalink
Closes #7170 Fonts download data measurement (PR #7173)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona authored Dec 12, 2024
1 parent eb81681 commit 0afcb6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions inc/Engine/Media/Fonts/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ public function exists( string $file ): bool {
}

/**
* Writes font css to path
* Writes CSS & fonts locally
*
* @param string $font_url The font url to save locally.
* @param string $provider The url of the page.
* @param string $css_url The CSS url to save locally.
* @param string $provider The font provider.
*
* @return bool
*/
public function write_font_css( string $font_url, string $provider ): bool {
public function write_font_css( string $css_url, string $provider ): bool {
$font_provider_path = $this->get_font_provider_path( $provider );
$css_filepath = $this->get_absolute_path( $font_provider_path, 'css/' . $this->hash_to_path( $this->hash_url( $font_url ) ) . '.css' );
$css_filepath = $this->get_absolute_path( $font_provider_path, 'css/' . $this->hash_to_path( $this->hash_url( $css_url ) ) . '.css' );
$fonts_basepath = $this->get_absolute_path( $font_provider_path, 'fonts' );

if ( ! rocket_mkdir_p( dirname( $css_filepath ) ) ) {
Expand All @@ -74,7 +74,7 @@ public function write_font_css( string $font_url, string $provider ): bool {

$start_time = microtime( true );

$css_content = $this->get_remote_content( html_entity_decode( $font_url ) );
$css_content = $this->get_remote_content( html_entity_decode( $css_url ) );

if ( ! $css_content ) {
return false;
Expand All @@ -84,6 +84,9 @@ public function write_font_css( string $font_url, string $provider ): bool {
$font_urls = $matches[1];
$local_css = $css_content;

$count_fonts = 0;
$download_average = 0;

foreach ( $font_urls as $font_url ) {
$font_path = wp_parse_url( $font_url, PHP_URL_PATH );

Expand All @@ -99,6 +102,8 @@ public function write_font_css( string $font_url, string $provider ): bool {
}

if ( ! $this->filesystem->exists( $local_path ) ) {
$download_start = microtime( true );

$font_content = $this->get_remote_content( $font_url );

if ( ! $font_content ) {
Expand All @@ -107,6 +112,15 @@ public function write_font_css( string $font_url, string $provider ): bool {
}

$this->write_file( $local_path, $font_content );

$download_end = microtime( true );
$download_time = $download_end - $download_start;

$download_average += $download_time;

++$count_fonts;

Logger::debug( "Font $font_url download duration -- $download_time", [ 'Host Fonts Locally' ] );
}

$local_url = content_url( $this->get_fonts_relative_path( $font_provider_path, $font_path ) );
Expand All @@ -121,6 +135,8 @@ public function write_font_css( string $font_url, string $provider ): bool {

// Add for test purpose.
Logger::debug( "Font download and optimization duration in seconds -- $duration", [ 'Host Fonts Locally' ] );
Logger::debug( "Number of fonts downloaded for $css_url -- $count_fonts", [ 'Host Fonts Locally' ] );
Logger::debug( 'Average download time per font -- ' . ( $count_fonts ? $download_average / $count_fonts : 0 ), [ 'Host Fonts Locally' ] );

return $this->write_file( $css_filepath, $local_css );
}
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function rewrite_fonts( $html ): string {

// 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' ] );
Logger::debug( "Total execution time for Host Google Fonts Feature in seconds -- $duration. CSS files processed: $total_fonts | Total v1: $total_v1 | Total v2: $total_v2", [ 'Host Fonts Locally' ] );

return $html;
}
Expand Down

0 comments on commit 0afcb6a

Please sign in to comment.