Benchmark
[ class tree: Benchmark ] [ index: Benchmark ] [ all elements ]

Source for file Timer.php

Documentation is available at Timer.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: Benchmark                                                      |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2001-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: Timer.php,v 1.11 2004/12/22 08:06:10 sebastian Exp $
  16. //
  17.  
  18. require_once 'PEAR.php';
  19.  
  20. /**
  21.  * Provides timing and profiling information.
  22.  *
  23.  * Example 1: Automatic profiling start, stop, and output.
  24.  *
  25.  * <code>
  26.  * <?php
  27.  * require_once 'Benchmark/Timer.php';
  28.  *
  29.  * $timer = new Benchmark_Timer(TRUE);
  30.  * $timer->setMarker('Marker 1');
  31.  * ?>
  32.  * </code>
  33.  *
  34.  * Example 2: Manual profiling start, stop, and output.
  35.  *
  36.  * <code>
  37.  * <?php
  38.  * require_once 'Benchmark/Timer.php';
  39.  *
  40.  * $timer = new Benchmark_Timer();
  41.  * $timer->start();
  42.  * $timer->setMarker('Marker 1');
  43.  * $timer->stop();
  44.  *
  45.  * $profiling = $timer->getProfiling();
  46.  * print $profiling->getOutput();
  47.  * ?>
  48.  * </code>
  49.  *
  50.  * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
  51.  * @author    Ludovico Magnocavallo <ludo@sumatrasolutions.com>
  52.  * @copyright Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  53.  * @license   http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  54.  * @category  Benchmarking
  55.  * @package   Benchmark
  56.  */
  57. class Benchmark_Timer extends PEAR {
  58.     /**
  59.      * Contains the markers.
  60.      *
  61.      * @var    array 
  62.      * @access private
  63.      */
  64.     var $markers = array();
  65.  
  66.     /**
  67.      * Auto-start and stop timer.
  68.      *
  69.      * @var    boolean 
  70.      * @access private
  71.      */
  72.     var $auto = FALSE;
  73.  
  74.     /**
  75.      * Max marker name length for non-html output.
  76.      *
  77.      * @var    integer 
  78.      * @access private
  79.      */
  80.     var $maxStringLength = 0;
  81.  
  82.     /**
  83.      * Constructor.
  84.      *
  85.      * @param  boolean $auto 
  86.      * @access public
  87.      */
  88.     function Benchmark_Timer($auto = FALSE{
  89.         $this->auto $auto;
  90.  
  91.         if ($this->auto{
  92.             $this->start();
  93.         }
  94.  
  95.         $this->PEAR();
  96.     }
  97.  
  98.     /**
  99.      * Destructor.
  100.      *
  101.      * @access private
  102.      */
  103.     function _Benchmark_Timer({
  104.         if ($this->auto{
  105.             $this->stop();
  106.             $this->display();
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Set "Start" marker.
  112.      *
  113.      * @see    setMarker(), stop()
  114.      * @access public
  115.      */
  116.     function start({
  117.         $this->setMarker('Start');
  118.     }
  119.  
  120.     /**
  121.      * Set "Stop" marker.
  122.      *
  123.      * @see    setMarker(), start()
  124.      * @access public
  125.      */
  126.     function stop({
  127.         $this->setMarker('Stop');
  128.     }
  129.  
  130.     /**
  131.      * Set marker.
  132.      *
  133.      * @param  string  $name Name of the marker to be set.
  134.      * @see    start(), stop()
  135.      * @access public
  136.      */
  137.     function setMarker($name{
  138.         $this->markers[$name$this->_getMicrotime();
  139.     }
  140.  
  141.     /**
  142.      * Returns the time elapsed betweens two markers.
  143.      *
  144.      * @param  string  $start        start marker, defaults to "Start"
  145.      * @param  string  $end          end marker, defaults to "Stop"
  146.      * @return double  $time_elapsed time elapsed between $start and $end
  147.      * @access public
  148.      */
  149.     function timeElapsed($start 'Start'$end 'Stop'{
  150.         if ($end == 'Stop' && !isset($this->markers['Stop'])) {
  151.             $this->markers['Stop'$this->_getMicrotime();
  152.         }
  153.  
  154.         if (extension_loaded('bcmath')) {
  155.             return bcsub($this->markers[$end]$this->markers[$start]6);
  156.         else {
  157.             return $this->markers[$end$this->markers[$start];
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * Returns profiling information.
  163.      *
  164.      * $profiling[x]['name']  = name of marker x
  165.      * $profiling[x]['time']  = time index of marker x
  166.      * $profiling[x]['diff']  = execution time from marker x-1 to this marker x
  167.      * $profiling[x]['total'] = total execution time up to marker x
  168.      *
  169.      * @return array 
  170.      * @access public
  171.      */
  172.     function getProfiling({
  173.         $i $total $temp = 0;
  174.         $result = array();
  175.  
  176.         foreach ($this->markers as $marker => $time{
  177.             if (extension_loaded('bcmath')) {
  178.                 $diff  = bcsub($time$temp6);
  179.                 $total = bcadd($total$diff6);
  180.             else {
  181.                 $diff  $time $temp;
  182.                 $total $total $diff;
  183.             }
  184.  
  185.             $result[$i]['name']  $marker;
  186.             $result[$i]['time']  $time;
  187.             $result[$i]['diff']  $diff;
  188.             $result[$i]['total'$total;
  189.  
  190.             $this->maxStringLength (strlen($marker$this->maxStringLength strlen($marker+ 1 : $this->maxStringLength);
  191.  
  192.             $temp $time;
  193.             $i++;
  194.         }
  195.  
  196.         $result[0]['diff''-';
  197.         $this->maxStringLength (strlen('total'$this->maxStringLength strlen('total'$this->maxStringLength);
  198.         $this->maxStringLength += 4;
  199.  
  200.         return $result;
  201.     }
  202.  
  203.     /**
  204.      * Return formatted profiling information.
  205.      *
  206.      * @return string 
  207.      * @see    getProfiling()
  208.      * @access public
  209.      */
  210.     function getOutput()
  211.     {
  212.         if (function_exists('version_compare'&&
  213.             version_compare(phpversion()'4.1''ge'))
  214.         {
  215.             $http = isset($_SERVER['SERVER_PROTOCOL']);
  216.         else {
  217.             global $HTTP_SERVER_VARS;
  218.             $http = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']);
  219.         }
  220.  
  221.         $total  $this->TimeElapsed();
  222.         $result $this->getProfiling();
  223.         $dashes '';
  224.  
  225.         if ($http{
  226.             $out '<table border="1">'."\n";
  227.             $out .= '<tr><td>&nbsp;</td><td align="center"><b>time index</b></td><td align="center"><b>ex time</b></td><td align="center"><b>%</b></td></tr>'."\n";
  228.         else {
  229.             $dashes $out str_pad("\n"($this->maxStringLength + 52)'-'STR_PAD_LEFT);
  230.             $out .= str_pad('marker'$this->maxStringLength);
  231.             $out .= str_pad("time index"22);
  232.             $out .= str_pad("ex time"22);
  233.             $out .= "perct\n";
  234.             $out .= $dashes;
  235.         }
  236.  
  237.         foreach ($result as $k => $v{
  238.             $perc (($v['diff'* 100$total);
  239.  
  240.             if ($http{
  241.                 $out .= "<tr><td><b>" $v['name'"</b></td><td>" $v['time'"</td><td>" $v['diff'"</td><td align=\"right\">" number_format($perc2'.'''"%</td></tr>\n";
  242.             else {
  243.                 $out .= str_pad($v['name']$this->maxStringLength' ');
  244.                 $out .= str_pad($v['time']22);
  245.                 $out .= str_pad($v['diff']22);
  246.                 $out .= str_pad(number_format($perc2'.'''"%\n"8' 'STR_PAD_LEFT);
  247.             }
  248.  
  249.             $out .= $dashes;
  250.         }
  251.  
  252.         if ($http{
  253.             $out .= "<tr style='background: silver;'><td><b>total</b></td><td>-</td><td>${total}</td><td>100.00%</td></tr>\n";
  254.             $out .= "</table>\n";
  255.         else {
  256.             $out .= str_pad('total'$this->maxStringLength);
  257.             $out .= str_pad('-'22);
  258.             $out .= str_pad($total22);
  259.             $out .= "100.00%\n";
  260.             $out .= $dashes;
  261.         }
  262.  
  263.         return $out;
  264.     }
  265.  
  266.     /**
  267.      * Prints the information returned by getOutput().
  268.      *
  269.      * @see    getOutput()
  270.      * @access public
  271.      */
  272.     function display({
  273.         print $this->getOutput();
  274.     }
  275.  
  276.     /**
  277.      * Wrapper for microtime().
  278.      *
  279.      * @return float 
  280.      * @access private
  281.      * @since  1.3.0
  282.      */
  283.     function _getMicrotime({
  284.         $microtime explode(' 'microtime());
  285.         return $microtime[1substr($microtime[0]1);
  286.     }
  287. }
  288. ?>

Documentation generated on Mon, 11 Mar 2019 14:00:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.