mirror of
https://hk.gh-proxy.com/https://github.com/wp-cli/profile-command.git
synced 2025-08-18 06:11:48 +08:00
Merge pull request #98 from runcommand/23-all-flag
Use `--all` to profile callbacks on all hooks
This commit is contained in:
commit
0677fffbaf
5 changed files with 24 additions and 9 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ 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>] [--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>]
|
||||
|
||||
See 'wp help profile <command>' for more information on a specific command.
|
||||
|
|
|
@ -88,7 +88,10 @@ class Command {
|
|||
* ## OPTIONS
|
||||
*
|
||||
* [<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>]
|
||||
* : 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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue