diff --git a/features/profile-hook.feature b/features/profile-hook.feature index f7ed06f..56e1726 100644 --- a/features/profile-hook.feature +++ b/features/profile-hook.feature @@ -7,8 +7,20 @@ Feature: Profile a specific hook Then STDOUT should be a table containing rows: | hook | callback_count | | plugins_loaded | 3 | + | init | 11 | + | template_redirect | 6 | 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 Given a WP install diff --git a/features/profile.feature b/features/profile.feature index b673665..829b41e 100644 --- a/features/profile.feature +++ b/features/profile.feature @@ -8,7 +8,7 @@ Feature: Basic profile usage """ usage: wp profile eval [--fields=] [--format=] or: wp profile eval-file [--fields=] [--format=] - or: wp profile hook [] [--url=] [--fields=] [--format=] + or: wp profile hook [] [--all] [--url=] [--fields=] [--format=] or: wp profile stage [] [--all] [--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 c5b941d..e0e6be5 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -88,7 +88,10 @@ class Command { * ## OPTIONS * * [] - * : 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=] * : Execute a request against a specified URL. Defaults to the home URL. @@ -111,7 +114,7 @@ class Command { */ 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->run(); diff --git a/inc/class-logger.php b/inc/class-logger.php index 3f4ecb1..963bdd6 100644 --- a/inc/class-logger.php +++ b/inc/class-logger.php @@ -61,14 +61,14 @@ class Logger { if ( ! is_null( $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++ ) { $this->query_time += $wpdb->queries[ $i ][1]; $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_misses = ! empty( $wp_object_cache->cache_misses ) ? $wp_object_cache->cache_misses : 0; $this->cache_hits = $cache_hits - $this->cache_hit_offset; diff --git a/inc/class-profiler.php b/inc/class-profiler.php index 88a46d9..a6eee86 100644 --- a/inc/class-profiler.php +++ b/inc/class-profiler.php @@ -107,7 +107,7 @@ class Profiler { } if ( 'hook' === $this->type - && $current_filter === $this->focus + && ( $current_filter === $this->focus || true === $this->focus ) && 0 === $this->filter_depth ) { $this->wrap_current_filter_callbacks( $current_filter ); } @@ -303,11 +303,11 @@ class Profiler { if ( $reflection ) { $location = $reflection->getFileName() . ':' . $reflection->getStartLine(); $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 ); - } 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 ); - } 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 ); } else if ( 0 === stripos( $location, $abspath . 'wp-admin/' ) ) { $location = str_replace( $abspath, '', $location );