diff --git a/inc/class-command.php b/inc/class-command.php index 8c0495c..21a279b 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -85,6 +85,8 @@ class Command { define( 'SAVEQUERIES', true ); } WP_CLI::add_wp_hook( 'all', array( $this, 'wp_hook_begin' ) ); + WP_CLI::add_wp_hook( 'pre_http_request', array( $this, 'wp_request_begin' ) ); + WP_CLI::add_wp_hook( 'http_api_debug', array( $this, 'wp_request_end' ) ); try { $this->load_wordpress_with_template(); } catch( \Exception $e ) { @@ -121,6 +123,8 @@ class Command { 'query_time', 'hook_count', 'hook_time', + 'request_count', + 'request_time', ); $data = $this->scope_log; } @@ -243,6 +247,26 @@ class Command { return $filter_value; } + /** + * Profiling request time for any active Loggers + */ + public function wp_request_begin( $filter_value = null ) { + foreach( Logger::$active_loggers as $logger ) { + $logger->start_request_timer(); + } + return $filter_value; + } + + /** + * Profiling request time for any active Loggers + */ + public function wp_request_end( $filter_value = null ) { + foreach( Logger::$active_loggers as $logger ) { + $logger->stop_request_timer(); + } + return $filter_value; + } + /** * Runs through the entirety of the WP bootstrap process */ diff --git a/inc/class-logger.php b/inc/class-logger.php index 6c12f9c..e1ae115 100644 --- a/inc/class-logger.php +++ b/inc/class-logger.php @@ -9,11 +9,14 @@ class Logger { public $query_time = 0; public $hook_count = 0; public $hook_time = 0; + public $request_count = 0; + public $request_time = 0; private $start_time = null; private $query_offset = null; private $hook_start_time = null; private $hook_depth = 0; + private $request_start_time = null; public static $active_loggers = array(); @@ -83,4 +86,22 @@ class Logger { } } + /** + * Start this logger's request timer + */ + public function start_request_timer() { + $this->request_count++; + $this->request_start_time = microtime( true ); + } + + /** + * Stop this logger's request timer + */ + public function stop_request_timer() { + if ( ! is_null( $this->request_start_time ) ) { + $this->request_time += microtime( true ) - $this->request_start_time; + } + $this->request_start_time = null; + } + }