2016-08-26 07:42:05 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace runcommand\Profile;
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
|
|
|
|
public $execution_time = 0;
|
2016-08-26 08:46:22 -07:00
|
|
|
public $queries = array(
|
|
|
|
'count' => 0,
|
|
|
|
'time' => 0,
|
|
|
|
);
|
2016-08-26 09:00:11 -07:00
|
|
|
public $cache = array(
|
|
|
|
'ratio' => 0,
|
|
|
|
'hits' => 0,
|
|
|
|
'misses' => 0,
|
|
|
|
);
|
2016-08-26 08:46:22 -07:00
|
|
|
public $hooks = array(
|
|
|
|
'count' => 0,
|
|
|
|
'time' => 0,
|
|
|
|
);
|
|
|
|
public $requests = array(
|
|
|
|
'count' => 0,
|
|
|
|
'time' => 0,
|
|
|
|
);
|
2016-08-26 07:42:05 -07:00
|
|
|
|
|
|
|
private $start_time = null;
|
|
|
|
private $query_offset = null;
|
2016-08-26 09:00:11 -07:00
|
|
|
private $cache_hit_offset = null;
|
|
|
|
private $cache_miss_offset = null;
|
2016-08-26 07:42:05 -07:00
|
|
|
private $hook_start_time = null;
|
2016-08-26 08:08:51 -07:00
|
|
|
private $hook_depth = 0;
|
2016-08-26 08:26:42 -07:00
|
|
|
private $request_start_time = null;
|
2016-08-26 07:42:05 -07:00
|
|
|
|
|
|
|
public static $active_loggers = array();
|
|
|
|
|
|
|
|
public function __construct( $type, $name ) {
|
|
|
|
$this->$type = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start this logger
|
|
|
|
*/
|
|
|
|
public function start() {
|
2016-08-26 09:00:11 -07:00
|
|
|
global $wpdb, $wp_object_cache;
|
2016-08-26 07:42:05 -07:00
|
|
|
$this->start_time = microtime( true );
|
|
|
|
$this->query_offset = ! empty( $wpdb->queries ) ? count( $wpdb->queries ) : 0;
|
|
|
|
if ( false === ( $key = array_search( $this, self::$active_loggers ) ) ) {
|
|
|
|
self::$active_loggers[] = $this;
|
|
|
|
}
|
2016-08-26 09:00:11 -07:00
|
|
|
$this->cache_hit_offset = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0;
|
|
|
|
$this->cache_miss_offset = ! empty( $wp_object_cache->cache_misses ) ? $wp_object_cache->cache_misses : 0;
|
2016-08-26 07:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop this logger
|
|
|
|
*/
|
|
|
|
public function stop() {
|
2016-08-26 09:00:11 -07:00
|
|
|
global $wpdb, $wp_object_cache;
|
2016-08-26 07:42:05 -07:00
|
|
|
|
|
|
|
if ( ! is_null( $this->start_time ) ) {
|
|
|
|
$this->execution_time += microtime( true ) - $this->start_time;
|
|
|
|
}
|
|
|
|
if ( ! is_null( $this->query_offset ) ) {
|
|
|
|
for ( $i = $this->query_offset; $i < count( $wpdb->queries ); $i++ ) {
|
2016-08-26 08:46:22 -07:00
|
|
|
$this->queries['time'] += $wpdb->queries[ $i ][1];
|
|
|
|
$this->queries['count']++;
|
2016-08-26 07:42:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 09:00:11 -07:00
|
|
|
if ( ! is_null( $this->cache_hit_offset ) && ! is_null( $this->cache_miss_offset ) ) {
|
|
|
|
$cache_hits = ! empty( $wp_object_cache->cache_hits ) ? $wp_object_cache->cache_hits : 0;
|
|
|
|
$cache_misses = ! empty( $wp_object_cache->cache_misses ) ? $wp_object_cache->cache_misses : 0;
|
|
|
|
$cache_total = $cache_hits + $cache_misses;
|
|
|
|
$this->cache['hits'] = $cache_hits - $this->cache_hit_offset;
|
|
|
|
$this->cache['misses'] = $cache_misses - $this->cache_miss_offset;
|
|
|
|
if ( $cache_total ) {
|
|
|
|
$this->cache['ratio'] = $cache_hits / $cache_total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 07:42:05 -07:00
|
|
|
$this->start_time = null;
|
|
|
|
$this->query_offset = null;
|
2016-08-26 09:00:11 -07:00
|
|
|
$this->cache_hit_offset = null;
|
|
|
|
$this->cache_miss_offset = null;
|
2016-08-26 07:42:05 -07:00
|
|
|
if ( false !== ( $key = array_search( $this, self::$active_loggers ) ) ) {
|
|
|
|
unset( self::$active_loggers[ $key ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start this logger's hook timer
|
|
|
|
*/
|
|
|
|
public function start_hook_timer() {
|
2016-08-26 08:08:51 -07:00
|
|
|
// Timer already running means a subhook has been called
|
|
|
|
if ( ! is_null( $this->hook_start_time ) ) {
|
|
|
|
$this->hook_depth++;
|
|
|
|
} else {
|
2016-08-26 08:46:22 -07:00
|
|
|
$this->hooks['count']++;
|
2016-08-26 08:08:51 -07:00
|
|
|
$this->hook_start_time = microtime( true );
|
|
|
|
}
|
2016-08-26 07:42:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop this logger's hook timer
|
|
|
|
*/
|
|
|
|
public function stop_hook_timer() {
|
2016-08-26 08:08:51 -07:00
|
|
|
if ( $this->hook_depth ) {
|
|
|
|
$this->hook_depth--;
|
|
|
|
} else {
|
|
|
|
if ( ! is_null( $this->hook_start_time ) ) {
|
2016-08-26 08:46:22 -07:00
|
|
|
$this->hooks['time'] += microtime( true ) - $this->hook_start_time;
|
2016-08-26 08:08:51 -07:00
|
|
|
}
|
|
|
|
$this->hook_start_time = null;
|
2016-08-26 07:42:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:26:42 -07:00
|
|
|
/**
|
|
|
|
* Start this logger's request timer
|
|
|
|
*/
|
|
|
|
public function start_request_timer() {
|
2016-08-26 08:46:22 -07:00
|
|
|
$this->requests['count']++;
|
2016-08-26 08:26:42 -07:00
|
|
|
$this->request_start_time = microtime( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop this logger's request timer
|
|
|
|
*/
|
|
|
|
public function stop_request_timer() {
|
|
|
|
if ( ! is_null( $this->request_start_time ) ) {
|
2016-08-26 08:46:22 -07:00
|
|
|
$this->requests['time'] += microtime( true ) - $this->request_start_time;
|
2016-08-26 08:26:42 -07:00
|
|
|
}
|
|
|
|
$this->request_start_time = null;
|
|
|
|
}
|
|
|
|
|
2016-08-26 07:42:05 -07:00
|
|
|
}
|