Merge pull request #108 from runcommand/13-spotlight

Use `--spotlight` to filter zero-ish results from the set
This commit is contained in:
Daniel Bachhuber 2016-10-26 06:10:28 -07:00 committed by GitHub
commit 6beadf3aa9
4 changed files with 92 additions and 8 deletions

View file

@ -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 [<stage>] [--all] [--url=<url>] [--fields=<fields>] [--format=<format>]
wp profile stage [<stage>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>]
~~~
**OPTIONS**
@ -102,6 +102,9 @@ wp profile stage [<stage>] [--all] [--url=<url>] [--fields=<fields>] [--format=<
[--all]
Expand upon all stages.
[--spotlight]
Filter out logs with zero-ish values from the set.
[--url=<url>]
Execute a request against a specified URL. Defaults to the home URL.
@ -126,7 +129,7 @@ wp profile stage [<stage>] [--all] [--url=<url>] [--fields=<fields>] [--format=<
Profile key metrics for WordPress hooks (actions and filters).
~~~
wp profile hook [<hook>] [--all] [--url=<url>] [--fields=<fields>] [--format=<format>]
wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<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=<url>]
Execute a request against a specified URL. Defaults to the home URL.

View file

@ -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 |

View file

@ -8,8 +8,8 @@ Feature: Basic profile usage
"""
usage: wp profile eval <php-code> [--fields=<fields>] [--format=<format>]
or: wp profile eval-file <file> [--fields=<fields>] [--format=<format>]
or: wp profile hook [<hook>] [--all] [--url=<url>] [--fields=<fields>] [--format=<format>]
or: wp profile stage [<stage>] [--all] [--url=<url>] [--fields=<fields>] [--format=<format>]
or: wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>]
or: wp profile stage [<stage>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>]
See 'wp help profile <command>' for more information on a specific command.
"""

View file

@ -18,6 +18,9 @@ class Command {
* [--all]
* : Expand upon all stages.
*
* [--spotlight]
* : Filter out logs with zero-ish values from the set.
*
* [--url=<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=<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;
}
}