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.13 2005/05/24 13:42:06 toggg 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.  * $timer->display(); // to output html formated
  46.  * // AND/OR :
  47.  * $profiling = $timer->getProfiling(); // get the profiler info as an associative array
  48.  * ?>
  49.  * </code>
  50.  *
  51.  * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
  52.  * @author    Ludovico Magnocavallo <ludo@sumatrasolutions.com>
  53.  * @copyright Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  54.  * @license   http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  55.  * @category  Benchmarking
  56.  * @package   Benchmark
  57.  */
  58. class Benchmark_Timer extends PEAR {
  59.     /**
  60.      * Contains the markers.
  61.      *
  62.      * @var    array 
  63.      * @access private
  64.      */
  65.     var $markers = array();
  66.  
  67.     /**
  68.      * Auto-start and stop timer.
  69.      *
  70.      * @var    boolean 
  71.      * @access private
  72.      */
  73.     var $auto = FALSE;
  74.  
  75.     /**
  76.      * Max marker name length for non-html output.
  77.      *
  78.      * @var    integer 
  79.      * @access private
  80.      */
  81.     var $maxStringLength = 0;
  82.  
  83.     /**
  84.      * Constructor.
  85.      *
  86.      * @param  boolean $auto 
  87.      * @access public
  88.      */
  89.     function Benchmark_Timer($auto = FALSE{
  90.         $this->auto $auto;
  91.  
  92.         if ($this->auto{
  93.             $this->start();
  94.         }
  95.  
  96.         $this->PEAR();
  97.     }
  98.  
  99.     /**
  100.      * Destructor.
  101.      *
  102.      * @access private
  103.      */
  104.     function _Benchmark_Timer({
  105.         if ($this->auto{
  106.             $this->stop();
  107.             $this->display();
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * Set "Start" marker.
  113.      *
  114.      * @see    setMarker(), stop()
  115.      * @access public
  116.      */
  117.     function start({
  118.         $this->setMarker('Start');
  119.     }
  120.  
  121.     /**
  122.      * Set "Stop" marker.
  123.      *
  124.      * @see    setMarker(), start()
  125.      * @access public
  126.      */
  127.     function stop({
  128.         $this->setMarker('Stop');
  129.     }
  130.  
  131.     /**
  132.      * Set marker.
  133.      *
  134.      * @param  string  $name Name of the marker to be set.
  135.      * @see    start(), stop()
  136.      * @access public
  137.      */
  138.     function setMarker($name{
  139.         $this->markers[$name$this->_getMicrotime();
  140.     }
  141.  
  142.     /**
  143.      * Returns the time elapsed betweens two markers.
  144.      *
  145.      * @param  string  $start        start marker, defaults to "Start"
  146.      * @param  string  $end          end marker, defaults to "Stop"
  147.      * @return double  $time_elapsed time elapsed between $start and $end
  148.      * @access public
  149.      */
  150.     function timeElapsed($start 'Start'$end 'Stop'{
  151.         if ($end == 'Stop' && !isset($this->markers['Stop'])) {
  152.             $this->markers['Stop'$this->_getMicrotime();
  153.         }
  154.  
  155.         if (extension_loaded('bcmath')) {
  156.             return bcsub($this->markers[$end]$this->markers[$start]6);
  157.         else {
  158.             return $this->markers[$end$this->markers[$start];
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Returns profiling information.
  164.      *
  165.      * $profiling[x]['name']  = name of marker x
  166.      * $profiling[x]['time']  = time index of marker x
  167.      * $profiling[x]['diff']  = execution time from marker x-1 to this marker x
  168.      * $profiling[x]['total'] = total execution time up to marker x
  169.      *
  170.      * @return array 
  171.      * @access public
  172.      */
  173.     function getProfiling({
  174.         $i $total = 0;
  175.         $result = array();
  176.         $temp reset($this->markers);
  177.         $this->maxStringLength = 0;
  178.  
  179.         foreach ($this->markers as $marker => $time{
  180.             if (extension_loaded('bcmath')) {
  181.                 $diff  = bcsub($time$temp6);
  182.                 $total = bcadd($total$diff6);
  183.             else {
  184.                 $diff  $time $temp;
  185.                 $total $total $diff;
  186.             }
  187.  
  188.             $result[$i]['name']  $marker;
  189.             $result[$i]['time']  $time;
  190.             $result[$i]['diff']  $diff;
  191.             $result[$i]['total'$total;
  192.  
  193.             $this->maxStringLength (strlen($marker$this->maxStringLength strlen($marker+ 1 : $this->maxStringLength);
  194.  
  195.             $temp $time;
  196.             $i++;
  197.         }
  198.  
  199.         $result[0]['diff''-';
  200.         $result[0]['total''-';
  201.         $this->maxStringLength (strlen('total'$this->maxStringLength strlen('total'$this->maxStringLength);
  202.         $this->maxStringLength += 2;
  203.  
  204.         return $result;
  205.     }
  206.  
  207.     /**
  208.      * Return formatted profiling information.
  209.      *
  210.      * @param  boolean  $showTotal   Optionnaly includes total in output, default no
  211.      * @return string 
  212.      * @see    getProfiling()
  213.      * @access public
  214.      */
  215.     function getOutput($showTotal = FALSE)
  216.     {
  217.         if (function_exists('version_compare'&&
  218.             version_compare(phpversion()'4.1''ge'))
  219.         {
  220.             $http = isset($_SERVER['SERVER_PROTOCOL']);
  221.         else {
  222.             global $HTTP_SERVER_VARS;
  223.             $http = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']);
  224.         }
  225.  
  226.         $total  $this->TimeElapsed();
  227.         $result $this->getProfiling();
  228.         $dashes '';
  229.  
  230.         if ($http{
  231.             $out '<table border="1">'."\n";
  232.             $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>'.
  233.             ($showTotal ?
  234.               '<td align="center"><b>elapsed</b></td><td align="center"><b>%</b></td>'
  235.                : '')."</tr>\n";
  236.         else {
  237.             $dashes $out str_pad("\n",
  238.                 $this->maxStringLength ($showTotal ? 70 : 45)'-'STR_PAD_LEFT);
  239.             $out .= str_pad('marker'$this->maxStringLength.
  240.                     str_pad("time index"22.
  241.                     str_pad("ex time"16.
  242.                     str_pad("perct "8.
  243.                     ($showTotal ' '.str_pad("elapsed"16)."perct" '')."\n" .
  244.                     $dashes;
  245.         }
  246.  
  247.         foreach ($result as $k => $v{
  248.             $perc (($v['diff'* 100$total);
  249.             $tperc (($v['total'* 100$total);
  250.  
  251.             if ($http{
  252.                 $out .= "<tr><td><b>" $v['name'.
  253.                        "</b></td><td>" $v['time'.
  254.                        "</td><td>" $v['diff'.
  255.                        "</td><td align=\"right\">" number_format($perc2'.'''.
  256.                        "%</td>".
  257.                        ($showTotal ?
  258.                             "<td>" $v['total'.
  259.                             "</td><td align=\"right\">" .
  260.                             number_format($tperc2'.'''.
  261.                             "%</td>" '').
  262.                        "</tr>\n";
  263.             else {
  264.                 $out .= str_pad($v['name']$this->maxStringLength' '.
  265.                         str_pad($v['time']22.
  266.                         str_pad($v['diff']14.
  267.                         str_pad(number_format($perc2'.''')."%",8' 'STR_PAD_LEFT.
  268.                         ($showTotal '   '.
  269.                             str_pad($v['total']14.
  270.                             str_pad(number_format($tperc2'.''')."%",
  271.                                              8' 'STR_PAD_LEFT'').
  272.                         "\n";
  273.             }
  274.  
  275.             $out .= $dashes;
  276.         }
  277.  
  278.         if ($http{
  279.             $out .= "<tr style='background: silver;'><td><b>total</b></td><td>-</td><td>${total}</td><td>100.00%</td>".($showTotal "<td>-</td><td>-</td>" "")."</tr>\n";
  280.             $out .= "</table>\n";
  281.         else {
  282.             $out .= str_pad('total'$this->maxStringLength);
  283.             $out .= str_pad('-'22);
  284.             $out .= str_pad($total15);
  285.             $out .= "100.00%\n";
  286.             $out .= $dashes;
  287.         }
  288.  
  289.         return $out;
  290.     }
  291.  
  292.     /**
  293.      * Prints the information returned by getOutput().
  294.      *
  295.      * @param  boolean  $showTotal   Optionnaly includes total in output, default no
  296.      * @see    getOutput()
  297.      * @access public
  298.      */
  299.     function display($showTotal = FALSE{
  300.         print $this->getOutput($showTotal);
  301.     }
  302.  
  303.     /**
  304.      * Wrapper for microtime().
  305.      *
  306.      * @return float 
  307.      * @access private
  308.      * @since  1.3.0
  309.      */
  310.     function _getMicrotime({
  311.         $microtime explode(' 'microtime());
  312.         return $microtime[1substr($microtime[0]1);
  313.     }
  314. }
  315. ?>

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