mirror of
https://hk.gh-proxy.com/https://github.com/wp-cli/profile-command.git
synced 2025-08-21 06:38:51 +08:00
Merge pull request #26 from runcommand/2-count-remote-requests
Log request count and time
This commit is contained in:
commit
80cf90c427
2 changed files with 45 additions and 0 deletions
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue