Advertisement
wpdreams

Custom performance profiler

Nov 3rd, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.90 KB | None | 0 0
  1. /**
  2.  * Very simple time & performance measurment
  3.  *
  4.  * 1. Hook first function to the first available action
  5.  * 2. Hook second function to the last
  6.  * 3. Calculate the differences  
  7.  *
  8.  **/
  9.  
  10. class measureUsage {
  11.  
  12.     private $records;
  13.     private $all_records;
  14.     private $average = array(
  15.           'runtime' => 0,
  16.           'repeated_runtime' => 0,
  17.           'memory'  => 0,
  18.           'ticks'   => 0,
  19.           'queries' => 0
  20.     );
  21.     private $runtime;
  22.     private $memory;
  23.     private $ticks;
  24.     private $queries;
  25.     private $key;
  26.  
  27.     function __construct($key = false, $reset = false) {
  28.       if ($key === false || is_admin()) return false;
  29.       $this->key = $key;
  30.       if ($reset === true) {
  31.         delete_option('measure_performance');
  32.         return false;
  33.       }
  34.       declare(ticks=1);
  35.       register_tick_function(array(&$this, 'ticks'), true);
  36.       $this->all_records = get_option('measure_performance');
  37.       $this->records = &$this->all_records[$key];
  38.       $this->records = !isset($this->all_records[$key]) ? array() : $this->records;
  39.       add_action( 'registered_taxonomy', array( $this, 'start_measuring') );
  40.       add_action( 'shutdown', array( $this, 'stop_measuring') );
  41.     }
  42.    
  43.     function start_measuring() {
  44.       $this->runtime = microtime(true);    
  45.     }
  46.    
  47.     function stop_measuring() {                
  48.       $this->runtime = microtime(true) - $this->runtime;
  49.       $this->memory = memory_get_usage(true);
  50.       $this->queries = get_num_queries();
  51.       $this->save();
  52.       $this->print_results();          
  53.     }
  54.    
  55.     function ticks() {
  56.        $curr = memory_get_peak_usage(true);
  57.        $this->memory = $curr > $this->memory ? $curr : $this->memory;
  58.        $this->ticks++;
  59.     }
  60.    
  61.     function save() {
  62.       if (count($this->records) >= 20)
  63.         array_shift($this->records);
  64.       $this->records[] = array(
  65.         'runtime' => $this->runtime,
  66.         'memory'  => $this->memory,
  67.         'ticks'   => $this->ticks,
  68.         'queries' => $this->queries
  69.       );
  70.       $this->count_averages();
  71.      
  72.       // Store the average to the core key and the records as well
  73.       if ($this->key == 'core')
  74.         $this->all_records['core_avg'] = $this->average;      
  75.      
  76.       update_option('measure_performance', $this->all_records);
  77.  
  78.     }
  79.    
  80.     function count_averages() {
  81.       $count = 0;
  82.       foreach($this->records as $k=>$v) {
  83.         $count++;
  84.         $this->average['runtime'] += $v['runtime'];
  85.         if ($count > 1)
  86.           $this->average['repeated_runtime'] += $v['runtime'];
  87.         $this->average['memory'] += $v['memory'];
  88.         $this->average['ticks'] += $v['ticks'];
  89.         $this->average['queries'] += $v['queries'];
  90.       }
  91.       $this->average['runtime'] = $this->average['runtime'] / $count;
  92.       if ($count > 1)
  93.         $this->average['repeated_runtime'] = $this->average['repeated_runtime'] / ($count-1);
  94.       $this->average['memory'] = $this->average['memory'] / $count;
  95.       $this->average['ticks'] = round($this->average['ticks'] / $count);
  96.       $this->average['queries'] = round($this->average['queries'] / $count);
  97.     }
  98.    
  99.     function print_results() {
  100.        ?>
  101.        <table class='gridtable'>  
  102.         <thead>
  103.           <tr>
  104.              <th colspan=5>Measures for <strong><?php echo $this->key; ?></strong></th>
  105.           </tr>
  106.         </thead>
  107.         <thead>
  108.           <tr>
  109.              <th>Measure no.</th>
  110.              <th>Load time</th>
  111.              <th>Memory consumption</th>
  112.              <th>PHP ticks</th>
  113.              <th>Database Queries</th>
  114.           </tr>
  115.         </thead>
  116.        <?php    
  117.        $count = 0;
  118.        foreach ($this->records as $k=>$v) {
  119.         $count++;
  120.         extract($v);
  121.         $avgs = $this->all_records['core_avg'];
  122.         if ($this->key != 'core') {          
  123.           $runtime -= $avgs['runtime'];
  124.           $memory -= $avgs['memory'];
  125.           $ticks -= $avgs['ticks'];
  126.           $queries -= $avgs['queries'];
  127.         }
  128.        ?>
  129.        <tr>
  130.         <td><?php echo $count; ?>.</td>
  131.         <td><?php echo number_format($runtime, 3, '.', ''); ?>s</td>
  132.         <td><?php echo $this->mem_convert($memory); ?></td>
  133.         <td><?php echo $ticks; ?></td>
  134.         <td><?php echo $queries; ?></td>
  135.        </tr>
  136.        <?php
  137.        }  
  138.        extract($this->average);
  139.         if ($this->key != 'core') {          
  140.           $runtime -= $avgs['runtime'];
  141.           $memory -= $avgs['memory'];
  142.           $ticks -= $avgs['ticks'];
  143.           $queries -= $avgs['queries'];
  144.         }
  145.        ?>
  146.        <tr>
  147.         <td><strong>Averages</strong></td>
  148.         <td><?php echo number_format($runtime, 3, '.', ''); ?>s</td>
  149.         <td><?php echo $this->mem_convert($memory); ?></td>
  150.         <td><?php echo $ticks; ?></td>
  151.         <td><?php echo $queries; ?></td>
  152.        </tr>
  153.        <tr>
  154.         <td><strong>Core Averages</strong></td>
  155.         <td><?php echo number_format($avgs['runtime'], 3, '.', ''); ?>s</td>
  156.         <td><?php echo $this->mem_convert($avgs['memory']); ?></td>
  157.         <td><?php echo $avgs['ticks']; ?></td>
  158.         <td><?php echo $avgs['queries']; ?></td>
  159.        </tr>
  160.        <?php if ($this->key != 'core'): ?>
  161.        <tr>
  162.         <td><strong>Average plugin load</strong></td>
  163.         <td colspan=4><?php echo $runtime / $this->average['runtime'] * 100; ?>%</td>
  164.        </tr>
  165.        <tr>
  166.         <td><strong>Average plugin load on repetitive pageload</strong></td>
  167.         <td colspan=4><?php echo ($this->average['repeated_runtime'] - $avgs['runtime']) / $this->average['repeated_runtime'] * 100; ?>%</td>
  168.        </tr>
  169.        <?php endif ?>
  170.        </table>
  171.  
  172.        <?php
  173.     }
  174.    
  175.     function mem_convert($size) {
  176.         $unit=array('b','kb','mb','gb','tb','pb');
  177.         return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
  178.     }
  179. }
  180.  
  181. $o = new measureUsage('bwsrp');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement