Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Very simple time & performance measurment
- *
- * 1. Hook first function to the first available action
- * 2. Hook second function to the last
- * 3. Calculate the differences
- *
- **/
- class measureUsage {
- private $records;
- private $all_records;
- private $average = array(
- 'runtime' => 0,
- 'repeated_runtime' => 0,
- 'memory' => 0,
- 'ticks' => 0,
- 'queries' => 0
- );
- private $runtime;
- private $memory;
- private $ticks;
- private $queries;
- private $key;
- function __construct($key = false, $reset = false) {
- if ($key === false || is_admin()) return false;
- $this->key = $key;
- if ($reset === true) {
- delete_option('measure_performance');
- return false;
- }
- declare(ticks=1);
- register_tick_function(array(&$this, 'ticks'), true);
- $this->all_records = get_option('measure_performance');
- $this->records = &$this->all_records[$key];
- $this->records = !isset($this->all_records[$key]) ? array() : $this->records;
- add_action( 'registered_taxonomy', array( $this, 'start_measuring') );
- add_action( 'shutdown', array( $this, 'stop_measuring') );
- }
- function start_measuring() {
- $this->runtime = microtime(true);
- }
- function stop_measuring() {
- $this->runtime = microtime(true) - $this->runtime;
- $this->memory = memory_get_usage(true);
- $this->queries = get_num_queries();
- $this->save();
- $this->print_results();
- }
- function ticks() {
- $curr = memory_get_peak_usage(true);
- $this->memory = $curr > $this->memory ? $curr : $this->memory;
- $this->ticks++;
- }
- function save() {
- if (count($this->records) >= 20)
- array_shift($this->records);
- $this->records[] = array(
- 'runtime' => $this->runtime,
- 'memory' => $this->memory,
- 'ticks' => $this->ticks,
- 'queries' => $this->queries
- );
- $this->count_averages();
- // Store the average to the core key and the records as well
- if ($this->key == 'core')
- $this->all_records['core_avg'] = $this->average;
- update_option('measure_performance', $this->all_records);
- }
- function count_averages() {
- $count = 0;
- foreach($this->records as $k=>$v) {
- $count++;
- $this->average['runtime'] += $v['runtime'];
- if ($count > 1)
- $this->average['repeated_runtime'] += $v['runtime'];
- $this->average['memory'] += $v['memory'];
- $this->average['ticks'] += $v['ticks'];
- $this->average['queries'] += $v['queries'];
- }
- $this->average['runtime'] = $this->average['runtime'] / $count;
- if ($count > 1)
- $this->average['repeated_runtime'] = $this->average['repeated_runtime'] / ($count-1);
- $this->average['memory'] = $this->average['memory'] / $count;
- $this->average['ticks'] = round($this->average['ticks'] / $count);
- $this->average['queries'] = round($this->average['queries'] / $count);
- }
- function print_results() {
- ?>
- <table class='gridtable'>
- <thead>
- <tr>
- <th colspan=5>Measures for <strong><?php echo $this->key; ?></strong></th>
- </tr>
- </thead>
- <thead>
- <tr>
- <th>Measure no.</th>
- <th>Load time</th>
- <th>Memory consumption</th>
- <th>PHP ticks</th>
- <th>Database Queries</th>
- </tr>
- </thead>
- <?php
- $count = 0;
- foreach ($this->records as $k=>$v) {
- $count++;
- extract($v);
- $avgs = $this->all_records['core_avg'];
- if ($this->key != 'core') {
- $runtime -= $avgs['runtime'];
- $memory -= $avgs['memory'];
- $ticks -= $avgs['ticks'];
- $queries -= $avgs['queries'];
- }
- ?>
- <tr>
- <td><?php echo $count; ?>.</td>
- <td><?php echo number_format($runtime, 3, '.', ''); ?>s</td>
- <td><?php echo $this->mem_convert($memory); ?></td>
- <td><?php echo $ticks; ?></td>
- <td><?php echo $queries; ?></td>
- </tr>
- <?php
- }
- extract($this->average);
- if ($this->key != 'core') {
- $runtime -= $avgs['runtime'];
- $memory -= $avgs['memory'];
- $ticks -= $avgs['ticks'];
- $queries -= $avgs['queries'];
- }
- ?>
- <tr>
- <td><strong>Averages</strong></td>
- <td><?php echo number_format($runtime, 3, '.', ''); ?>s</td>
- <td><?php echo $this->mem_convert($memory); ?></td>
- <td><?php echo $ticks; ?></td>
- <td><?php echo $queries; ?></td>
- </tr>
- <tr>
- <td><strong>Core Averages</strong></td>
- <td><?php echo number_format($avgs['runtime'], 3, '.', ''); ?>s</td>
- <td><?php echo $this->mem_convert($avgs['memory']); ?></td>
- <td><?php echo $avgs['ticks']; ?></td>
- <td><?php echo $avgs['queries']; ?></td>
- </tr>
- <?php if ($this->key != 'core'): ?>
- <tr>
- <td><strong>Average plugin load</strong></td>
- <td colspan=4><?php echo $runtime / $this->average['runtime'] * 100; ?>%</td>
- </tr>
- <tr>
- <td><strong>Average plugin load on repetitive pageload</strong></td>
- <td colspan=4><?php echo ($this->average['repeated_runtime'] - $avgs['runtime']) / $this->average['repeated_runtime'] * 100; ?>%</td>
- </tr>
- <?php endif ?>
- </table>
- <?php
- }
- function mem_convert($size) {
- $unit=array('b','kb','mb','gb','tb','pb');
- return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
- }
- }
- $o = new measureUsage('bwsrp');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement