Merge pull request #93 from runcommand/23-callback-count

Indicate callback count when profiling a stage
This commit is contained in:
Daniel Bachhuber 2016-10-08 06:01:56 -07:00 committed by GitHub
commit d58d4b0b36
2 changed files with 28 additions and 3 deletions

View file

@ -116,3 +116,11 @@ Feature: Profile the template render stage
"""
Error: Invalid stage. Must be one of bootstrap, main_query, template, or use --all.
"""
Scenario: Identify callback_count for each hook
Given a WP install
When I run `wp profile stage bootstrap --fields=hook,callback_count`
Then STDOUT should be a table containing rows:
| hook | callback_count |
| plugins_loaded | 3 |

View file

@ -63,6 +63,7 @@ class Command {
if ( $this->focus_stage ) {
$fields = array(
'hook',
'callback_count',
'time',
'query_time',
'query_count',
@ -271,7 +272,21 @@ class Command {
if ( isset( $this->loggers[ $pseudo_hook ] ) ) {
$this->loggers[ $pseudo_hook ]->stop();
}
$this->loggers[ $current_filter ] = new Logger( array( 'hook' => $current_filter ) );
$callback_count = 0;
if ( isset( $wp_filter[ $current_filter ] ) && is_a( $wp_filter[ $current_filter ], 'WP_Hook' ) ) {
if ( is_array( $wp_filter[ $current_filter ]->callbacks ) ) {
foreach( $wp_filter[ $current_filter ]->callbacks as $priority => $callbacks ) {
$callback_count += count( $callbacks );
}
}
} else {
if ( isset( $wp_filter[ $current_filter ] ) && is_array( $wp_filter[ $current_filter ] ) ) {
foreach( $wp_filter[ $current_filter ] as $priority => $callbacks ) {
$callback_count += count( $callbacks );
}
}
}
$this->loggers[ $current_filter ] = new Logger( array( 'hook' => $current_filter, 'callback_count' => $callback_count ) );
$this->loggers[ $current_filter ]->start();
}
@ -289,7 +304,9 @@ class Command {
$this->filter_depth = 1;
}
WP_CLI::add_wp_hook( $current_filter, array( $this, 'wp_hook_end' ), 9999 );
if ( 'shutdown' !== $this->focus_hook ) {
WP_CLI::add_wp_hook( $current_filter, array( $this, 'wp_hook_end' ), 9999 );
}
}
/**
@ -352,11 +369,11 @@ class Command {
$logger->stop_hook_timer();
}
$current_filter = current_filter();
if ( $this->focus_hook && $current_filter === $this->focus_hook ) {
$this->filter_depth = 0;
}
$current_filter = current_filter();
if ( in_array( $current_filter, $this->stage_hooks ) ) {
$this->loggers[ $current_filter ]->stop();
$key = array_search( $current_filter, $this->stage_hooks );