Use --all to profile callbacks on all hooks

This commit is contained in:
Daniel Bachhuber 2016-10-08 16:18:48 -07:00
parent 53b8253b1f
commit 178e0454b6
5 changed files with 24 additions and 9 deletions

View file

@ -7,8 +7,20 @@ Feature: Profile a specific hook
Then STDOUT should be a table containing rows: Then STDOUT should be a table containing rows:
| hook | callback_count | | hook | callback_count |
| plugins_loaded | 3 | | plugins_loaded | 3 |
| init | 11 |
| template_redirect | 6 |
And STDERR should be empty And STDERR should be empty
Scenario: Profile all callbacks when --all flag is used
Given a WP install
When I run `wp profile hook --all --fields=callback,cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| callback | cache_hits | cache_misses |
| sanitize_comment_cookies() | 0 | 0 |
| smilies_init() | 2 | 0 |
| feed_links() | 8 | 0 |
Scenario: Profile a hook before the template is loaded Scenario: Profile a hook before the template is loaded
Given a WP install Given a WP install

View file

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

View file

@ -88,7 +88,10 @@ class Command {
* ## OPTIONS * ## OPTIONS
* *
* [<hook>] * [<hook>]
* : Drill into key metrics for a specific WordPress hook (action or filter). * : Drill into key metrics of callbacks on a specific WordPress hook.
*
* [--all]
* : Profile callbacks for all WordPress hooks.
* *
* [--url=<url>] * [--url=<url>]
* : Execute a request against a specified URL. Defaults to the home URL. * : Execute a request against a specified URL. Defaults to the home URL.
@ -111,7 +114,7 @@ class Command {
*/ */
public function hook( $args, $assoc_args ) { public function hook( $args, $assoc_args ) {
$focus = isset( $args[0] ) ? $args[0] : null; $focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
$profiler = new Profiler( 'hook', $focus ); $profiler = new Profiler( 'hook', $focus );
$profiler->run(); $profiler->run();

View file

@ -61,14 +61,14 @@ class Logger {
if ( ! is_null( $this->start_time ) ) { if ( ! is_null( $this->start_time ) ) {
$this->time += microtime( true ) - $this->start_time; $this->time += microtime( true ) - $this->start_time;
} }
if ( ! is_null( $this->query_offset ) ) { if ( ! is_null( $this->query_offset ) && isset( $wpdb ) ) {
for ( $i = $this->query_offset; $i < count( $wpdb->queries ); $i++ ) { for ( $i = $this->query_offset; $i < count( $wpdb->queries ); $i++ ) {
$this->query_time += $wpdb->queries[ $i ][1]; $this->query_time += $wpdb->queries[ $i ][1];
$this->query_count++; $this->query_count++;
} }
} }
if ( ! is_null( $this->cache_hit_offset ) && ! is_null( $this->cache_miss_offset ) ) { if ( ! is_null( $this->cache_hit_offset ) && ! is_null( $this->cache_miss_offset ) && isset( $wp_object_cache ) ) {
$cache_hits = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0; $cache_hits = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0;
$cache_misses = ! empty( $wp_object_cache->cache_misses ) ? $wp_object_cache->cache_misses : 0; $cache_misses = ! empty( $wp_object_cache->cache_misses ) ? $wp_object_cache->cache_misses : 0;
$this->cache_hits = $cache_hits - $this->cache_hit_offset; $this->cache_hits = $cache_hits - $this->cache_hit_offset;

View file

@ -107,7 +107,7 @@ class Profiler {
} }
if ( 'hook' === $this->type if ( 'hook' === $this->type
&& $current_filter === $this->focus && ( $current_filter === $this->focus || true === $this->focus )
&& 0 === $this->filter_depth ) { && 0 === $this->filter_depth ) {
$this->wrap_current_filter_callbacks( $current_filter ); $this->wrap_current_filter_callbacks( $current_filter );
} }
@ -303,11 +303,11 @@ class Profiler {
if ( $reflection ) { if ( $reflection ) {
$location = $reflection->getFileName() . ':' . $reflection->getStartLine(); $location = $reflection->getFileName() . ':' . $reflection->getStartLine();
$abspath = rtrim( realpath( ABSPATH ), '/' ) . '/'; $abspath = rtrim( realpath( ABSPATH ), '/' ) . '/';
if ( 0 === stripos( $location, WP_PLUGIN_DIR ) ) { if ( defined( 'WP_PLUGIN_DIR' ) && 0 === stripos( $location, WP_PLUGIN_DIR ) ) {
$location = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $location ); $location = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $location );
} else if ( 0 === stripos( $location, WPMU_PLUGIN_DIR ) ) { } else if ( defined( 'WPMU_PLUGIN_DIR' ) && 0 === stripos( $location, WPMU_PLUGIN_DIR ) ) {
$location = str_replace( trailingslashit( dirname( WPMU_PLUGIN_DIR ) ), '', $location ); $location = str_replace( trailingslashit( dirname( WPMU_PLUGIN_DIR ) ), '', $location );
} else if ( 0 === stripos( $location, get_theme_root() ) ) { } else if ( function_exists( 'get_theme_root' ) && 0 === stripos( $location, get_theme_root() ) ) {
$location = str_replace( trailingslashit( get_theme_root() ), '', $location ); $location = str_replace( trailingslashit( get_theme_root() ), '', $location );
} else if ( 0 === stripos( $location, $abspath . 'wp-admin/' ) ) { } else if ( 0 === stripos( $location, $abspath . 'wp-admin/' ) ) {
$location = str_replace( $abspath, '', $location ); $location = str_replace( $abspath, '', $location );