Indicate callback count when profiling a stage

This commit is contained in:
Daniel Bachhuber 2016-10-07 16:18:04 -07:00
parent 9a977569b1
commit e403ef86e0
2 changed files with 25 additions and 2 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();
}
@ -352,11 +367,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 );