From 0afcb6a8ba708f18ea037bc4b130488c3d0a63d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 12 Dec 2024 04:40:37 -0500 Subject: [PATCH] Closes #7170 Fonts download data measurement (PR #7173) --- inc/Engine/Media/Fonts/Filesystem.php | 28 +++++++++++++++---- .../Media/Fonts/Frontend/Controller.php | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/inc/Engine/Media/Fonts/Filesystem.php b/inc/Engine/Media/Fonts/Filesystem.php index fb03eaabb1..b8bd8ce039 100644 --- a/inc/Engine/Media/Fonts/Filesystem.php +++ b/inc/Engine/Media/Fonts/Filesystem.php @@ -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 ) ) ) { @@ -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; @@ -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 ); @@ -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 ) { @@ -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 ) ); @@ -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 ); } diff --git a/inc/Engine/Media/Fonts/Frontend/Controller.php b/inc/Engine/Media/Fonts/Frontend/Controller.php index 38bede8816..9c8818babb 100644 --- a/inc/Engine/Media/Fonts/Frontend/Controller.php +++ b/inc/Engine/Media/Fonts/Frontend/Controller.php @@ -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; }