Merge pull request #74 from runcommand/fix-profile-early-hooks

Fix profiling of early hooks
This commit is contained in:
Daniel Bachhuber 2016-09-23 06:42:16 -07:00 committed by GitHub
commit c669b51a29
2 changed files with 27 additions and 4 deletions

View file

@ -1,5 +1,13 @@
Feature: Profile a specific hook Feature: Profile a specific hook
Scenario: Profile a hook before the template is loaded
Given a WP install
When I run `wp profile --hook=plugins_loaded --fields=callback`
Then STDOUT should be a table containing rows:
| callback |
And STDERR should be empty
Scenario: Profile a hook without any callbacks Scenario: Profile a hook without any callbacks
Given a WP install Given a WP install

View file

@ -14,6 +14,8 @@ class Command {
private $current_filter_callbacks = array(); private $current_filter_callbacks = array();
private $focus_query_offset = 0; private $focus_query_offset = 0;
private static $exception_message = "Need to bail, because can't restore the hooks";
/** /**
* Quickly identify what's slow with WordPress. * Quickly identify what's slow with WordPress.
* *
@ -141,7 +143,7 @@ class Command {
$this->current_filter_callbacks = $wp_filter[ $current_filter ]; $this->current_filter_callbacks = $wp_filter[ $current_filter ];
unset( $wp_filter[ $current_filter ] ); unset( $wp_filter[ $current_filter ] );
call_user_func_array( array( $this, 'do_action' ), func_get_args() ); call_user_func_array( array( $this, 'do_action' ), func_get_args() );
throw new \Exception( "Need to bail, because can't restore the hooks" ); throw new \Exception( self::$exception_message );
} }
WP_CLI::add_wp_hook( $current_filter, array( $this, 'wp_hook_end' ), 999 ); WP_CLI::add_wp_hook( $current_filter, array( $this, 'wp_hook_end' ), 999 );
@ -269,7 +271,11 @@ class Command {
try { try {
WP_CLI::get_runner()->load_wordpress(); WP_CLI::get_runner()->load_wordpress();
} catch( \Exception $e ) { } catch( \Exception $e ) {
// pass through // If this was thrown by our do_action implementation, then we need to bail
if ( self::$exception_message === $e->getMessage() ) {
return;
}
// Otherwise, pass through.
} }
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) { if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
$this->loggers['wp_profile_last_hook']->stop(); $this->loggers['wp_profile_last_hook']->stop();
@ -295,7 +301,11 @@ class Command {
try { try {
wp(); wp();
} catch( \Exception $e ) { } catch( \Exception $e ) {
// pass through // If this was thrown by our do_action implementation, then we need to bail
if ( self::$exception_message === $e->getMessage() ) {
return;
}
// Otherwise, pass through.
} }
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) { if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
$this->loggers['wp_profile_last_hook']->stop(); $this->loggers['wp_profile_last_hook']->stop();
@ -330,7 +340,12 @@ class Command {
try { try {
require_once( ABSPATH . WPINC . '/template-loader.php' ); require_once( ABSPATH . WPINC . '/template-loader.php' );
} catch( \Exception $e ) { } catch( \Exception $e ) {
// pass through // If this was thrown by our do_action implementation, then we need to bail
if ( self::$exception_message === $e->getMessage() ) {
ob_get_clean();
return;
}
// Otherwise, pass through.
} }
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) { if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
$this->loggers['wp_profile_last_hook']->stop(); $this->loggers['wp_profile_last_hook']->stop();