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

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