Merge pull request #28 from runcommand/15-cache-hit-miss

Capture cache hit / miss ratio
This commit is contained in:
Daniel Bachhuber 2016-08-26 09:07:07 -07:00 committed by GitHub
commit 5c14cba9a0
2 changed files with 26 additions and 2 deletions

View file

@ -120,6 +120,7 @@ class Command {
'scope',
'execution_time',
'queries',
'cache',
'hooks',
'requests',
);

View file

@ -9,6 +9,11 @@ class Logger {
'count' => 0,
'time' => 0,
);
public $cache = array(
'ratio' => 0,
'hits' => 0,
'misses' => 0,
);
public $hooks = array(
'count' => 0,
'time' => 0,
@ -20,6 +25,8 @@ class Logger {
private $start_time = null;
private $query_offset = null;
private $cache_hit_offset = null;
private $cache_miss_offset = null;
private $hook_start_time = null;
private $hook_depth = 0;
private $request_start_time = null;
@ -34,19 +41,21 @@ class Logger {
* Start this logger
*/
public function start() {
global $wpdb;
global $wpdb, $wp_object_cache;
$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;
}
$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;
}
/**
* Stop this logger
*/
public function stop() {
global $wpdb;
global $wpdb, $wp_object_cache;
if ( ! is_null( $this->start_time ) ) {
$this->execution_time += microtime( true ) - $this->start_time;
@ -58,8 +67,22 @@ class Logger {
}
}
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 ) {
$ratio = ( $cache_hits / $cache_total ) * 100;
$this->cache['ratio'] = round( $ratio, 2 ) . '%';
}
}
$this->start_time = null;
$this->query_offset = null;
$this->cache_hit_offset = null;
$this->cache_miss_offset = null;
if ( false !== ( $key = array_search( $this, self::$active_loggers ) ) ) {
unset( self::$active_loggers[ $key ] );
}