diff --git a/README.md b/README.md index 5a3f20f..bd43977 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ This package implements the following commands: Profile each stage of the WordPress load process (bootstrap, main_query, template). ~~~ -wp profile stage [] [--all] [--url=] [--fields=] [--format=] +wp profile stage [] [--all] [--spotlight] [--url=] [--fields=] [--format=] ~~~ **OPTIONS** @@ -102,6 +102,9 @@ wp profile stage [] [--all] [--url=] [--fields=] [--format=< [--all] Expand upon all stages. + [--spotlight] + Filter out logs with zero-ish values from the set. + [--url=] Execute a request against a specified URL. Defaults to the home URL. @@ -126,7 +129,7 @@ wp profile stage [] [--all] [--url=] [--fields=] [--format=< Profile key metrics for WordPress hooks (actions and filters). ~~~ -wp profile hook [] [--all] [--url=] [--fields=] [--format=] +wp profile hook [] [--all] [--spotlight] [--url=] [--fields=] [--format=] ~~~ In order to profile callbacks on a specific hook, the action or filter @@ -140,6 +143,9 @@ will need to execute during the course of the request. [--all] Profile callbacks for all WordPress hooks. + [--spotlight] + Filter out logs with zero-ish values from the set. + [--url=] Execute a request against a specified URL. Defaults to the home URL. diff --git a/features/profile-stage.feature b/features/profile-stage.feature index 9580420..e0f8e57 100644 --- a/features/profile-stage.feature +++ b/features/profile-stage.feature @@ -124,3 +124,20 @@ Feature: Profile the template render stage Then STDOUT should be a table containing rows: | hook | callback_count | | plugins_loaded | 3 | + + Scenario: Use spotlight mode to filter out the zero-ish values + Given a WP install + + When I run `wp profile stage bootstrap --fields=hook` + Then STDOUT should be a table containing rows: + | hook | + | init | + | wp_loaded:before | + | wp_loaded | + | wp_loaded:after | + + When I run `wp profile stage bootstrap --fields=hook --spotlight` + Then STDOUT should be a table containing rows: + | hook | + | init | + | wp_loaded:after | diff --git a/features/profile.feature b/features/profile.feature index 829b41e..e6e22b9 100644 --- a/features/profile.feature +++ b/features/profile.feature @@ -8,8 +8,8 @@ Feature: Basic profile usage """ usage: wp profile eval [--fields=] [--format=] or: wp profile eval-file [--fields=] [--format=] - or: wp profile hook [] [--all] [--url=] [--fields=] [--format=] - or: wp profile stage [] [--all] [--url=] [--fields=] [--format=] + or: wp profile hook [] [--all] [--spotlight] [--url=] [--fields=] [--format=] + or: wp profile stage [] [--all] [--spotlight] [--url=] [--fields=] [--format=] See 'wp help profile ' for more information on a specific command. """ diff --git a/inc/class-command.php b/inc/class-command.php index ba084c2..b3a361e 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -18,6 +18,9 @@ class Command { * [--all] * : Expand upon all stages. * + * [--spotlight] + * : Filter out logs with zero-ish values from the set. + * * [--url=] * : Execute a request against a specified URL. Defaults to the home URL. * @@ -51,9 +54,11 @@ class Command { $profiler->run(); if ( $focus ) { - $fields = array( + $base = array( 'hook', 'callback_count', + ); + $metrics = array( 'time', 'query_time', 'query_count', @@ -64,8 +69,10 @@ class Command { 'request_count', ); } else { - $fields = array( + $base = array( 'stage', + ); + $metrics = array( 'time', 'query_time', 'query_count', @@ -78,8 +85,13 @@ class Command { 'request_count', ); } + $fields = array_merge( $base, $metrics ); $formatter = new Formatter( $assoc_args, $fields ); - $formatter->display_items( $profiler->get_loggers() ); + $loggers = $profiler->get_loggers(); + if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) { + $loggers = self::shine_spotlight( $loggers, $metrics ); + } + $formatter->display_items( $loggers ); } /** @@ -96,6 +108,9 @@ class Command { * [--all] * : Profile callbacks for all WordPress hooks. * + * [--spotlight] + * : Filter out logs with zero-ish values from the set. + * * [--url=] * : Execute a request against a specified URL. Defaults to the home URL. * @@ -146,7 +161,11 @@ class Command { ); $fields = array_merge( $base, $metrics ); $formatter = new Formatter( $assoc_args, $fields ); - $formatter->display_items( $profiler->get_loggers() ); + $loggers = $profiler->get_loggers(); + if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) { + $loggers = self::shine_spotlight( $loggers, $metrics ); + } + $formatter->display_items( $loggers ); } /** @@ -269,4 +288,46 @@ class Command { include( $file ); } + /** + * Filter loggers with zero-ish values. + * + * @param array $loggers + * @param array $metrics + * @return array + */ + private static function shine_spotlight( $loggers, $metrics ) { + + foreach( $loggers as $k => $logger ) { + $non_zero = false; + foreach( $metrics as $metric ) { + switch ( $metric ) { + // 100% cache ratio is fine by us + case 'cache_ratio': + case 'cache_hits': + case 'cache_misses': + if ( $logger->cache_ratio && '100%' !== $logger->cache_ratio ) { + $non_zero = true; + } + break; + case 'time': + case 'query_time': + if ( $logger->$metric > 0.01 ) { + $non_zero = true; + } + break; + default: + if ( $logger->$metric ) { + $non_zero = true; + } + break; + } + } + if ( ! $non_zero ) { + unset( $loggers[ $k ] ); + } + } + + return $loggers; + } + }