home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Benchmark / Timer.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  10.0 KB  |  322 lines

  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.2 2007/05/24 05:23:20 anant 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 © 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.      * Close method. Stop timer and display output.
  103.      *
  104.      * @access public
  105.      */
  106.     function close() {
  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.         $end = isset($this->markers[$end]) ? $this->markers[$end] : 0;
  157.         $start = isset($this->markers[$start]) ? $this->markers[$start] : 0;
  158.  
  159.         if (extension_loaded('bcmath')) {
  160.             return bcsub($end, $start, 6);
  161.         } else {
  162.             return $end - $start;
  163.         }
  164.     }
  165.  
  166.     /**
  167.      * Returns profiling information.
  168.      *
  169.      * $profiling[x]['name']  = name of marker x
  170.      * $profiling[x]['time']  = time index of marker x
  171.      * $profiling[x]['diff']  = execution time from marker x-1 to this marker x
  172.      * $profiling[x]['total'] = total execution time up to marker x
  173.      *
  174.      * @return array
  175.      * @access public
  176.      */
  177.     function getProfiling() {
  178.         $i = $total = 0;
  179.         $result = array();
  180.         $temp = reset($this->markers);
  181.         $this->maxStringLength = 0;
  182.  
  183.         foreach ($this->markers as $marker => $time) {
  184.             if (extension_loaded('bcmath')) {
  185.                 $diff  = bcsub($time, $temp, 6);
  186.                 $total = bcadd($total, $diff, 6);
  187.             } else {
  188.                 $diff  = $time - $temp;
  189.                 $total = $total + $diff;
  190.             }
  191.  
  192.             $result[$i]['name']  = $marker;
  193.             $result[$i]['time']  = $time;
  194.             $result[$i]['diff']  = $diff;
  195.             $result[$i]['total'] = $total;
  196.  
  197.             $this->maxStringLength = (strlen($marker) > $this->maxStringLength ? strlen($marker) + 1 : $this->maxStringLength);
  198.  
  199.             $temp = $time;
  200.             $i++;
  201.         }
  202.  
  203.         $result[0]['diff'] = '-';
  204.         $result[0]['total'] = '-';
  205.         $this->maxStringLength = (strlen('total') > $this->maxStringLength ? strlen('total') : $this->maxStringLength);
  206.         $this->maxStringLength += 2;
  207.  
  208.         return $result;
  209.     }
  210.  
  211.     /**
  212.      * Return formatted profiling information.
  213.      *
  214.      * @param  boolean  $showTotal   Optionnaly includes total in output, default no
  215.      * @param  string  $format   output format (auto, plain or html), default auto
  216.      * @return string
  217.      * @see    getProfiling()
  218.      * @access public
  219.      */
  220.     function getOutput($showTotal = FALSE, $format = 'auto') {
  221.         if ($format == 'auto') {
  222.             if (function_exists('version_compare') &&
  223.                 version_compare(phpversion(), '4.1', 'ge'))
  224.             {
  225.                 $format = isset($_SERVER['SERVER_PROTOCOL']) ? 'html' : 'plain';
  226.             } else {
  227.                 global $HTTP_SERVER_VARS;
  228.                 $format = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']) ? 'html' : 'plain';
  229.         }
  230.         }
  231.  
  232.         $total  = $this->TimeElapsed();
  233.         $result = $this->getProfiling();
  234.         $dashes = '';
  235.  
  236.         if ($format == 'html') {
  237.             $out = '<table border="1">'."\n";
  238.             $out .= '<tr><td> </td><td align="center"><b>time index</b></td><td align="center"><b>ex time</b></td><td align="center"><b>%</b></td>'.
  239.             ($showTotal ?
  240.               '<td align="center"><b>elapsed</b></td><td align="center"><b>%</b></td>'
  241.                : '')."</tr>\n";
  242.         } else {
  243.             $dashes = $out = str_pad("\n",
  244.                 $this->maxStringLength + ($showTotal ? 70 : 45), '-', STR_PAD_LEFT);
  245.             $out .= str_pad('marker', $this->maxStringLength) .
  246.                     str_pad("time index", 22) .
  247.                     str_pad("ex time", 16) .
  248.                     str_pad("perct ", 8) .
  249.                     ($showTotal ? ' '.str_pad("elapsed", 16)."perct" : '')."\n" .
  250.                     $dashes;
  251.         }
  252.  
  253.         foreach ($result as $k => $v) {
  254.             $perc = (($v['diff'] * 100) / $total);
  255.             $tperc = (($v['total'] * 100) / $total);
  256.  
  257.             if ($format == 'html') {
  258.                 $out .= "<tr><td><b>" . $v['name'] .
  259.                        "</b></td><td>" . $v['time'] .
  260.                        "</td><td>" . $v['diff'] .
  261.                        "</td><td align=\"right\">" . number_format($perc, 2, '.', '') .
  262.                        "%</td>".
  263.                        ($showTotal ?
  264.                             "<td>" . $v['total'] .
  265.                             "</td><td align=\"right\">" .
  266.                             number_format($tperc, 2, '.', '') .
  267.                             "%</td>" : '').
  268.                        "</tr>\n";
  269.             } else {
  270.                 $out .= str_pad($v['name'], $this->maxStringLength, ' ') .
  271.                         str_pad($v['time'], 22) .
  272.                         str_pad($v['diff'], 14) .
  273.                         str_pad(number_format($perc, 2, '.', '')."%",8, ' ', STR_PAD_LEFT) .
  274.                         ($showTotal ? '   '.
  275.                             str_pad($v['total'], 14) .
  276.                             str_pad(number_format($tperc, 2, '.', '')."%",
  277.                                              8, ' ', STR_PAD_LEFT) : '').
  278.                         "\n";
  279.             }
  280.  
  281.             $out .= $dashes;
  282.         }
  283.  
  284.         if ($format == 'html') {
  285.             $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";
  286.             $out .= "</table>\n";
  287.         } else {
  288.             $out .= str_pad('total', $this->maxStringLength);
  289.             $out .= str_pad('-', 22);
  290.             $out .= str_pad($total, 15);
  291.             $out .= "100.00%\n";
  292.             $out .= $dashes;
  293.         }
  294.  
  295.         return $out;
  296.     }
  297.  
  298.     /**
  299.      * Prints the information returned by getOutput().
  300.      *
  301.      * @param  boolean  $showTotal   Optionnaly includes total in output, default no
  302.      * @param  string  $format   output format (auto, plain or html), default auto
  303.      * @see    getOutput()
  304.      * @access public
  305.      */
  306.     function display($showTotal = FALSE, $format = 'auto') {
  307.         print $this->getOutput($showTotal, $format);
  308.     }
  309.  
  310.     /**
  311.      * Wrapper for microtime().
  312.      *
  313.      * @return float
  314.      * @access private
  315.      * @since  1.3.0
  316.      */
  317.     function _getMicrotime() {
  318.         $microtime = explode(' ', microtime());
  319.         return $microtime[1] . substr($microtime[0], 1);
  320.     }
  321. }
  322.