*/ public static function normalize( $raw ) { $raw = is_array( $raw ) ? $raw : array(); $origin = isset( $raw['origin'] ) && in_array( $raw['origin'], self::ORIGINS, true ) ? $raw['origin'] : 'frontend'; $server_ms = self::number( isset( $raw['server_ms'] ) ? $raw['server_ms'] : 0 ); $queries = isset( $raw['queries'] ) && is_array( $raw['queries'] ) ? $raw['queries'] : array(); $result = array( 'origin' => $origin, 'url' => self::clean_url( isset( $raw['url'] ) ? $raw['url'] : '' ), 'time' => (int) self::number( isset( $raw['time'] ) ? $raw['time'] : 0 ), 'server_ms' => $server_ms, 'ttfb_ms' => self::number( isset( $raw['ttfb_ms'] ) ? $raw['ttfb_ms'] : 0 ), 'memory_peak' => (int) self::number( isset( $raw['memory_peak'] ) ? $raw['memory_peak'] : 0 ), 'memory_limit' => (int) self::number( isset( $raw['memory_limit'] ) ? $raw['memory_limit'] : 0 ), 'template' => self::text( isset( $raw['template'] ) ? $raw['template'] : '', 200 ), 'dashboard_ms' => self::number( isset( $raw['dashboard_ms'] ) ? $raw['dashboard_ms'] : 0 ), 'queries' => array( 'count' => (int) self::number( isset( $queries['count'] ) ? $queries['count'] : 0 ), 'total_ms' => self::number( isset( $queries['total_ms'] ) ? $queries['total_ms'] : 0 ), 'slowest' => self::query_list( isset( $queries['slowest'] ) ? $queries['slowest'] : array(), 'ms' ), 'duplicates' => self::query_list( isset( $queries['duplicates'] ) ? $queries['duplicates'] : array(), 'count' ), 'by_source' => self::query_sources( isset( $queries['by_source'] ) ? $queries['by_source'] : array() ), ), 'sources' => self::sources( isset( $raw['sources'] ) ? $raw['sources'] : array(), $server_ms ), 'phases' => self::phases( isset( $raw['phases'] ) ? $raw['phases'] : array() ), 'external' => self::external( isset( $raw['external'] ) ? $raw['external'] : array() ), 'headers' => self::headers( isset( $raw['headers'] ) ? $raw['headers'] : array() ), 'html' => self::html( isset( $raw['html'] ) ? $raw['html'] : array() ), 'heartbeat' => array( 'calls' => (int) self::number( isset( $raw['heartbeat']['calls'] ) ? $raw['heartbeat']['calls'] : 0 ), 'minutes' => (int) self::number( isset( $raw['heartbeat']['minutes'] ) ? $raw['heartbeat']['minutes'] : 0 ), ), 'notes' => self::notes( isset( $raw['notes'] ) ? $raw['notes'] : array() ), ); return $result; } /** * Median mehrerer Messungen. Zahlen und Quellen werden je Wert gemittelt, * alles andere (Abfragelisten, Kopfzeilen, Phasen) stammt von der Messung, * deren Serverzeit dem Median am naechsten liegt. * * @param mixed $measurements Liste von Messungen. * @return array */ public static function median( $measurements ) { $list = array(); if ( is_array( $measurements ) ) { foreach ( $measurements as $item ) { $list[] = self::normalize( $item ); } } if ( array() === $list ) { return self::normalize( array() ); } if ( 1 === count( $list ) ) { return $list[0]; } $server = self::column_median( $list, array( 'server_ms' ) ); $representative = $list[0]; $best = abs( $list[0]['server_ms'] - $server ); foreach ( $list as $item ) { $distance = abs( $item['server_ms'] - $server ); if ( $distance < $best ) { $best = $distance; $representative = $item; } } $result = $representative; $result['server_ms'] = $server; $result['ttfb_ms'] = self::column_median( $list, array( 'ttfb_ms' ) ); $result['memory_peak'] = (int) round( self::column_median( $list, array( 'memory_peak' ) ) ); $result['queries']['count'] = (int) round( self::column_median( $list, array( 'queries', 'count' ) ) ); $result['queries']['total_ms'] = self::column_median( $list, array( 'queries', 'total_ms' ) ); $result['sources'] = self::median_sources( $list, $server ); $result['notes'] = $representative['notes']; return $result; } /** * Vergleich zweier Messungen. Fuer alle Kennzahlen gilt: weniger ist besser. * * @param mixed $before Messung davor. * @param mixed $after Messung danach. * @param float $tolerance Relative Toleranz, innerhalb der "same" gilt. * @return array */ public static function compare( $before, $after, $tolerance = 0.05 ) { $before = self::normalize( $before ); $after = self::normalize( $after ); $metrics = array( 'server_ms' => array( 'server_ms' ), 'ttfb_ms' => array( 'ttfb_ms' ), 'memory_peak' => array( 'memory_peak' ), 'query_count' => array( 'queries', 'count' ), 'query_ms' => array( 'queries', 'total_ms' ), ); $result = array(); foreach ( $metrics as $name => $path ) { $old = (float) self::dig( $before, $path ); $new = (float) self::dig( $after, $path ); $diff = round( $new - $old, 2 ); if ( $old > 0 ) { $percent = round( ( $new - $old ) / $old * 100, 1 ); $trend = abs( $new - $old ) / $old <= $tolerance ? 'same' : ( $new < $old ? 'better' : 'worse' ); } else { $percent = null; $trend = $new > 0 ? 'worse' : 'same'; } $result[ $name ] = array( 'before' => $old, 'after' => $new, 'diff' => $diff, 'percent' => $percent, 'trend' => $trend, ); } return $result; } /** * Entfernt Werte aus der Abfragezeichenfolge; die Namen bleiben stehen. * Adresse ohne Schema und Host (nur Pfad), damit Berichte portabel sind. */ public static function clean_url( $url ) { if ( ! is_string( $url ) || '' === $url ) { return ''; } $path = '?' === $url[0] ? '/' : (string) strtok( $url, '?#' ); $parts = function_exists( 'wp_parse_url' ) ? wp_parse_url( $path ) : parse_url( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url if ( is_array( $parts ) && isset( $parts['path'] ) ) { $path = $parts['path']; } elseif ( preg_match( '#^[a-z][a-z0-9+.-]*://[^/]*$#i', $path ) ) { $path = '/'; } $query = ''; $pos = strpos( $url, '?' ); if ( false !== $pos ) { $raw = (string) strtok( substr( $url, $pos + 1 ), '#' ); $names = array(); foreach ( explode( '&', $raw ) as $pair ) { $name = (string) strtok( $pair, '=' ); if ( '' !== $name ) { $names[] = rawurlencode( rawurldecode( $name ) ); } } if ( $names ) { $query = '?' . implode( '&', array_map( static function ( $name ) { return $name . '='; }, array_slice( $names, 0, 10 ) ) ); } } return self::text( $path . $query, 300 ); } /* ---------------------------------------------------------------- */ private static function number( $value ) { if ( ! is_numeric( $value ) ) { return 0.0; } $number = (float) $value; if ( is_nan( $number ) || is_infinite( $number ) || $number < 0 ) { return 0.0; } return round( $number, 2 ); } private static function text( $value, $max ) { if ( ! is_scalar( $value ) ) { return ''; } $value = (string) $value; // Steuerzeichen entfernen, Laenge begrenzen. $value = (string) preg_replace( '/[\x00-\x1F\x7F]/', '', $value ); return strlen( $value ) > $max ? substr( $value, 0, $max ) : $value; } /** * @return list */ private static function sources( $raw, $server_ms ) { if ( ! is_array( $raw ) ) { return array(); } $types = array( 'plugin', 'mu-plugin', 'theme', 'core', 'other' ); $list = array(); foreach ( $raw as $item ) { if ( ! is_array( $item ) ) { continue; } $type = isset( $item['type'] ) && in_array( $item['type'], $types, true ) ? $item['type'] : 'other'; $name = self::text( isset( $item['name'] ) ? $item['name'] : '', 120 ); $ms = self::number( isset( $item['ms'] ) ? $item['ms'] : 0 ); if ( $ms <= 0 ) { continue; } $key = '' === $name ? $type : $type . ':' . $name; if ( isset( $list[ $key ] ) ) { $list[ $key ]['ms'] = round( $list[ $key ]['ms'] + $ms, 2 ); continue; } $list[ $key ] = array( 'type' => $type, 'name' => $name, 'key' => $key, 'label' => '' === $name ? $type : $name, 'ms' => $ms, 'share' => 0.0, ); } return self::apply_shares( array_values( $list ), $server_ms ); } /** * Sortiert nach Zeit absteigend und berechnet die Anteile. Ueberlappen * sich Zeiten (verschachtelte Haken), werden die Anteile so skaliert, * dass sie zusammen hoechstens 100 % ergeben. * * @param list> $sources * @return list> */ private static function apply_shares( array $sources, $server_ms ) { usort( $sources, static function ( $a, $b ) { if ( $a['ms'] === $b['ms'] ) { return strcmp( $a['key'], $b['key'] ); } return $a['ms'] < $b['ms'] ? 1 : -1; } ); $sources = array_slice( $sources, 0, self::MAX_SOURCES ); $sum = 0.0; foreach ( $sources as $source ) { $sum += $source['ms']; } $denominator = max( (float) $server_ms, $sum ); foreach ( $sources as $index => $source ) { $sources[ $index ]['share'] = $denominator > 0 ? round( $source['ms'] / $denominator, 4 ) : 0.0; } return $sources; } /** * @param list> $list Normalisierte Messungen. * @return list> */ private static function median_sources( array $list, $server_ms ) { $per_key = array(); foreach ( $list as $item ) { foreach ( $item['sources'] as $source ) { $per_key[ $source['key'] ]['meta'] = $source; } } $count = count( $list ); $sources = array(); foreach ( $per_key as $key => $data ) { $values = array(); foreach ( $list as $item ) { $found = 0.0; foreach ( $item['sources'] as $source ) { if ( $source['key'] === $key ) { $found = $source['ms']; break; } } $values[] = $found; } $ms = self::middle( $values ); if ( $ms > 0 && $count > 0 ) { $entry = $data['meta']; $entry['ms'] = $ms; $sources[] = $entry; } } return self::apply_shares( $sources, $server_ms ); } /** * Signale aus der Antwort (siehe Html_Signals) und die Kopfzeilen von * Beispiel-Dateien. Nur bekannte Felder, mit Obergrenzen. * * @return array */ private static function html( $raw ) { $raw = is_array( $raw ) ? $raw : array(); $hosts = array(); if ( isset( $raw['external_hosts'] ) && is_array( $raw['external_hosts'] ) ) { foreach ( $raw['external_hosts'] as $host => $count ) { $host = strtolower( self::text( $host, 120 ) ); if ( '' !== $host && count( $hosts ) < 20 ) { $hosts[ $host ] = (int) self::number( $count ); } } arsort( $hosts ); } $samples = array(); if ( isset( $raw['sample_assets'] ) && is_array( $raw['sample_assets'] ) ) { foreach ( array_slice( $raw['sample_assets'], 0, 5 ) as $asset ) { if ( is_array( $asset ) && ! empty( $asset['url'] ) ) { $samples[] = array( 'url' => self::clean_url( $asset['url'] ), 'type' => in_array( isset( $asset['type'] ) ? $asset['type'] : '', array( 'css', 'js', 'img' ), true ) ? $asset['type'] : 'css', ); } } } $asset_headers = array(); if ( isset( $raw['asset_headers'] ) && is_array( $raw['asset_headers'] ) ) { foreach ( array_slice( $raw['asset_headers'], 0, 5 ) as $asset ) { if ( is_array( $asset ) ) { $asset_headers[] = array( 'url' => self::clean_url( isset( $asset['url'] ) ? $asset['url'] : '' ), 'type' => in_array( isset( $asset['type'] ) ? $asset['type'] : '', array( 'css', 'js', 'img' ), true ) ? $asset['type'] : 'css', 'status' => (int) self::number( isset( $asset['status'] ) ? $asset['status'] : 0 ), 'cache_control' => self::text( isset( $asset['cache_control'] ) ? $asset['cache_control'] : '', 200 ), 'expires' => self::text( isset( $asset['expires'] ) ? $asset['expires'] : '', 100 ), 'date' => self::text( isset( $asset['date'] ) ? $asset['date'] : '', 100 ), ); } } } return array( 'is_html' => ! empty( $raw['is_html'] ), 'bytes' => (int) self::number( isset( $raw['bytes'] ) ? $raw['bytes'] : 0 ), 'emoji_tags' => (int) self::number( isset( $raw['emoji_tags'] ) ? $raw['emoji_tags'] : 0 ), 'version_urls' => (int) self::number( isset( $raw['version_urls'] ) ? $raw['version_urls'] : 0 ), 'asset_count' => (int) self::number( isset( $raw['asset_count'] ) ? $raw['asset_count'] : 0 ), 'blocking_scripts' => (int) self::number( isset( $raw['blocking_scripts'] ) ? $raw['blocking_scripts'] : 0 ), 'blocking_handles' => self::handles( isset( $raw['blocking_handles'] ) ? $raw['blocking_handles'] : array() ), 'external_hosts' => $hosts, 'hinted_hosts' => self::host_list( isset( $raw['hinted_hosts'] ) ? $raw['hinted_hosts'] : array() ), 'sample_assets' => $samples, 'asset_headers' => $asset_headers, ); } /** * Hostnamen (klein geschrieben, nur Buchstaben, Ziffern, Punkt, Bindestrich). * * @return list */ private static function host_list( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $hosts = array(); foreach ( $raw as $host ) { $host = is_string( $host ) ? strtolower( $host ) : ''; if ( preg_match( '/^[a-z0-9.\-]{1,120}$/', $host ) && ! in_array( $host, $hosts, true ) ) { $hosts[] = $host; } } return array_slice( $hosts, 0, 30 ); } /** * Skript-Handles (nur Buchstaben, Ziffern, Punkt, Bindestrich, Unterstrich). * * @return list */ private static function handles( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $handles = array(); foreach ( $raw as $handle ) { if ( is_string( $handle ) && preg_match( '/^[A-Za-z0-9_.\-]{1,80}$/', $handle ) && ! in_array( $handle, $handles, true ) ) { $handles[] = $handle; } } return array_slice( $handles, 0, 20 ); } /** * Abfragen je Quelle (Anzahl und Zeit), die meisten zuerst. * * @return list */ private static function query_sources( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $types = array( 'plugin', 'mu-plugin', 'theme', 'core', 'other' ); $list = array(); foreach ( $raw as $item ) { if ( ! is_array( $item ) ) { continue; } $type = isset( $item['type'] ) && in_array( $item['type'], $types, true ) ? $item['type'] : 'other'; $name = self::text( isset( $item['name'] ) ? $item['name'] : '', 120 ); $count = (int) self::number( isset( $item['count'] ) ? $item['count'] : 0 ); if ( $count <= 0 ) { continue; } $key = '' === $name ? $type : $type . ':' . $name; if ( isset( $list[ $key ] ) ) { $list[ $key ]['count'] += $count; $list[ $key ]['ms'] = round( $list[ $key ]['ms'] + self::number( isset( $item['ms'] ) ? $item['ms'] : 0 ), 2 ); continue; } $list[ $key ] = array( 'type' => $type, 'name' => $name, 'key' => $key, 'label' => '' === $name ? $type : $name, 'count' => $count, 'ms' => self::number( isset( $item['ms'] ) ? $item['ms'] : 0 ), ); } $list = array_values( $list ); usort( $list, static function ( $a, $b ) { return $a['count'] === $b['count'] ? strcmp( $a['key'], $b['key'] ) : ( $a['count'] < $b['count'] ? 1 : -1 ); } ); return array_slice( $list, 0, 15 ); } /** * @return list */ private static function query_list( $raw, $sort_by ) { if ( ! is_array( $raw ) ) { return array(); } $list = array(); foreach ( $raw as $item ) { if ( ! is_array( $item ) ) { continue; } $sql = isset( $item['sql'] ) && is_string( $item['sql'] ) ? $item['sql'] : ''; // Sicherheitsnetz: Werte werden auch hier noch einmal entfernt. if ( class_exists( 'Wp360_Speed_Optimizer_Doctor_Sql_Sanitizer' ) ) { $sql = Wp360_Speed_Optimizer_Doctor_Sql_Sanitizer::sanitize( $sql ); } else { $sql = ''; } if ( '' === $sql ) { continue; } $list[] = array( 'sql' => $sql, 'ms' => self::number( isset( $item['ms'] ) ? $item['ms'] : 0 ), 'count' => (int) self::number( isset( $item['count'] ) ? $item['count'] : 1 ), 'source' => self::text( isset( $item['source'] ) ? $item['source'] : '', 160 ), ); } usort( $list, static function ( $a, $b ) use ( $sort_by ) { if ( $a[ $sort_by ] === $b[ $sort_by ] ) { return 0; } return $a[ $sort_by ] < $b[ $sort_by ] ? 1 : -1; } ); return array_slice( $list, 0, self::MAX_QUERIES ); } /** * @return array */ private static function phases( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $phases = array(); foreach ( $raw as $name => $ms ) { $name = self::text( $name, 60 ); if ( '' === $name ) { continue; } $phases[ $name ] = self::number( $ms ); if ( count( $phases ) >= self::MAX_PHASES ) { break; } } return $phases; } /** * @return list */ private static function external( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $list = array(); foreach ( $raw as $item ) { if ( ! is_array( $item ) ) { continue; } $host = strtolower( self::text( isset( $item['host'] ) ? $item['host'] : '', 120 ) ); if ( '' === $host ) { continue; } $list[] = array( 'host' => $host, 'ms' => self::number( isset( $item['ms'] ) ? $item['ms'] : 0 ), 'source' => self::text( isset( $item['source'] ) ? $item['source'] : '', 160 ), ); } usort( $list, static function ( $a, $b ) { return $a['ms'] === $b['ms'] ? 0 : ( $a['ms'] < $b['ms'] ? 1 : -1 ); } ); return array_slice( $list, 0, self::MAX_EXTERNAL ); } /** * Kopfzeilen mit kleingeschriebenen Namen. Cookie-Kopfzeilen werden nie * uebernommen (koennten Sitzungswerte enthalten). * * @return array */ private static function headers( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $headers = array(); foreach ( $raw as $name => $value ) { $name = strtolower( self::text( $name, 60 ) ); // Nur Kopfzeilen, die der Ratgeber braucht (keine Zugangs- oder Debug-Daten). if ( ! in_array( $name, self::HEADER_ALLOWLIST, true ) ) { continue; } if ( is_array( $value ) ) { $value = implode( ', ', array_filter( $value, 'is_scalar' ) ); } $headers[ $name ] = self::text( $value, 300 ); if ( count( $headers ) >= self::MAX_HEADERS ) { break; } } return $headers; } /** * @return list */ private static function notes( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $notes = array(); foreach ( $raw as $note ) { $note = self::text( $note, 60 ); if ( '' !== $note && ! in_array( $note, $notes, true ) ) { $notes[] = $note; } } return array_slice( $notes, 0, 10 ); } /** * @param list> $list * @param list $path */ private static function column_median( array $list, array $path ) { $values = array(); foreach ( $list as $item ) { $values[] = (float) self::dig( $item, $path ); } return self::middle( $values ); } /** * @param array $data * @param list $path */ private static function dig( array $data, array $path ) { foreach ( $path as $key ) { if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) { return 0; } $data = $data[ $key ]; } return $data; } /** * @param list $values */ private static function middle( array $values ) { if ( array() === $values ) { return 0.0; } sort( $values ); $count = count( $values ); $mid = intdiv( $count, 2 ); return round( 1 === $count % 2 ? $values[ $mid ] : ( $values[ $mid - 1 ] + $values[ $mid ] ) / 2, 2 ); } }