home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Profiler.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  10.9 KB  |  371 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PEAR :: Benchmark                                                    |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 Matthias Englert <Matthias.Englert@gmx.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: Profiler.php,v 1.6 2003/04/24 15:33:53 matthias Exp $
  16. //
  17.  
  18. /**
  19.  * Benchmark::Profiler
  20.  *
  21.  * Purpose:
  22.  *
  23.  *     Timing Script Execution, Generating Profiling Information
  24.  *
  25.  * Example with automatic profiling start, stop, and output:
  26.  *
  27.  *     $profiler =& new Benchmark_Profiler(true);
  28.  *     function myFunction() {
  29.  *         $profiler->enterSection('myFunction');
  30.  *         //do something
  31.  *         $profiler->leaveSection('myFunction');
  32.  *         return;
  33.  *     }
  34.  *     //do something
  35.  *     myFunction();
  36.  *     //do more
  37.  *
  38.  *
  39.  * Example without automatic profiling:
  40.  *
  41.  *     $profiler =& new Benchmark_Profiler();
  42.  *     function myFunction() {
  43.  *         $profiler->enterSection('myFunction');
  44.  *         //do something
  45.  *         $profiler->leaveSection('myFunction');
  46.  *         return;
  47.  *     }
  48.  *     $profiler->start();
  49.  *     //do something
  50.  *     myFunction();
  51.  *     //do more
  52.  *     $profiler->stop();
  53.  *     $profiler->display();
  54.  *
  55.  * @author   Matthias Englert <Matthias.Englert@gmx.de>
  56.  * @version  $Revision: 1.6 $
  57.  * @access   public
  58.  */
  59.  
  60. require_once 'PEAR.php';
  61.  
  62. class Benchmark_Profiler extends PEAR {
  63.  
  64.    /**
  65.      * Contains the total ex. time of each section
  66.      *
  67.      * @var    array
  68.      * @access private
  69.      */
  70.     var $_sections = array();
  71.  
  72.     /**
  73.      * Calling stack
  74.      *
  75.      * @var    array
  76.      * @access private
  77.      */
  78.     var $_stack = array();
  79.  
  80.     /**
  81.      * Notes how often a section was entered
  82.      *
  83.      * @var    array
  84.      * @access private
  85.      */
  86.     var $_num_calls = array();
  87.  
  88.     /**
  89.      * Notes for each section how much time is spend in sub-sections
  90.      *
  91.      * @var    array
  92.      * @access private
  93.      */
  94.     var $_sub_sections_time = array();
  95.  
  96.     /**
  97.      * Notes for each section how often it calls which section
  98.      *
  99.      * @var    array
  100.      * @access private
  101.      */
  102.     var $_calls = array();
  103.  
  104.     /**
  105.      * Notes for each section how often it was called by which section
  106.      *
  107.      * @var    array
  108.      * @access private
  109.      */
  110.     var $_callers = array();
  111.  
  112.     /**
  113.      * Auto-starts and stops profiler
  114.      *
  115.      * @var    boolean
  116.      * @access private
  117.      */
  118.     var $_auto = false;
  119.  
  120.     /**
  121.      * Max marker name length for non-html output
  122.      *
  123.      * @var    integer
  124.      * @access private
  125.      */
  126.     var $_strlen_max = 0;
  127.  
  128.     /**
  129.      * Constructor, starts profiling recording
  130.      *
  131.      * @access public
  132.      */
  133.     function Benchmark_Profiler($auto = false) {
  134.         $this->PEAR();
  135.         if ($auto) {
  136.             $this->auto = $auto;
  137.             $this->start();
  138.         }
  139.     }
  140.  
  141.     /**
  142.      * Destructor, stops profiling recording
  143.      *
  144.      * @access private
  145.      */
  146.     function _Benchmark_Profiler() {
  147.         if (isset($this->auto)) {
  148.             $this->stop();
  149.             $this->display();
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Returns profiling informations for a given section.
  155.      *
  156.      * @access public
  157.      */
  158.     function getSectionInformations($section = 'Global') {
  159.         if (isset($this->_sections[$section])) {
  160.  
  161.             $calls = array();
  162.             if (isset($this->_calls[$section])) {
  163.                 $calls = $this->_calls[$section];
  164.             }        
  165.             
  166.             $callers = array();
  167.             if (isset($this->_callers[$section])) {
  168.                 $callers = $this->_callers[$section];
  169.             }
  170.     
  171.             $informations = array();
  172.             $informations['time'] = $this->_sections[$section];
  173.             $informations['percentage'] = number_format(100 * $this->_sections[$section] / $this->_sections['Global'], 2, '.', '');
  174.             $informations['calls'] = $calls;
  175.             $informations['num_calls'] = $this->_num_calls[$section];
  176.             $informations['callers'] = $callers;
  177.         if (isset($this->_sub_sections_time[$section])) {
  178.                 $informations['netto_time'] = $this->_sections[$section] - $this->_sub_sections_time[$section];
  179.         } else {
  180.                 $informations['netto_time'] = $this->_sections[$section];
  181.         }
  182.  
  183.             return $informations;
  184.         } else {
  185.             $this->raiseError("The section '$section' does not exists.\n", null, PEAR_ERROR_TRIGGER, E_USER_WARNING);        
  186.         }
  187.     }    
  188.  
  189.     /**
  190.      * Returns profiling informations for all sections.
  191.      *
  192.      * @access public
  193.      */
  194.     function getAllSectionsInformations() {
  195.         $informations = array();
  196.         foreach($this->_sections as $section => $time) {
  197.             $informations[$section] = $this->getSectionInformations($section);
  198.         }
  199.  
  200.         return $informations;
  201.     }    
  202.     
  203.     /**
  204.      * Returns formatted profiling information.
  205.      *
  206.      * @see    display()
  207.      * @access private
  208.      */
  209.     function _getOutput() {
  210.         if (function_exists('version_compare') &&
  211.             version_compare(phpversion(), '4.1', 'ge')) {
  212.             $http = isset($_SERVER['SERVER_PROTOCOL']);
  213.         } else {
  214.             global $HTTP_SERVER_VARS;
  215.             $http = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']);
  216.         }
  217.         if ($http) {
  218.             $out = "<table border=1>\n";
  219.             $out .=
  220.                 '<tr><td> </td><td align="center"><b>total ex. time</b></td>'.
  221.                 '<td align="center"><b>netto ex. time</b></td>'.
  222.                 '<td align="center"><b>#calls</b></td><td align="center"><b>%</b></td>'.
  223.                 '<td align="center"><b>calls</b></td><td align="center"><b>callers</b></td></tr>'.
  224.                 "\n";
  225.         } else {
  226.             $dashes = $out =
  227.                 str_pad("\n", ($this->_strlen_max + 52), '-',
  228.                         STR_PAD_LEFT);
  229.             $out .= str_pad('section', $this->_strlen_max);
  230.             $out .= str_pad("total ex time", 22);
  231.             $out .= str_pad("netto ex time", 22);
  232.             $out .= str_pad("#calls", 22);
  233.             $out .= "perct\n";
  234.             $out .= $dashes;
  235.         }
  236.         
  237.         $informations = $this->getAllSectionsInformations();        
  238.         foreach($informations as $name => $values) {
  239.             $percentage = $values['percentage'];
  240.             $calls_str = "";
  241.             foreach($values['calls'] as $key => $val) {
  242.                 if ($calls_str) {
  243.                     $calls_str .= ", ";
  244.                 }
  245.                 $calls_str .= "$key ($val)";
  246.             }
  247.             $callers_str = "";
  248.             foreach($values['callers'] as $key => $val) {
  249.                 if ($callers_str) {
  250.                     $callers_str .= ", ";
  251.                 }
  252.                 $callers_str .= "$key ($val)";
  253.             }
  254.             if ($http) {
  255.                 $out .=
  256.                     "<tr><td><b>$name</b></td><td>{$values['time']}</td><td>{$values['netto_time']}</td><td>{$values['num_calls']}</td>".
  257.                     "<td align=\"right\">{$values['percentage']}%</td>\n";
  258.                 $out .= "<td>$calls_str</td><td>$callers_str</td></tr>";
  259.             } else {
  260.                 $out .= str_pad($name, $this->_strlen_max, ' ');
  261.                 $out .= str_pad($values['time'], 22);
  262.                 $out .= str_pad($values['netto_time'], 22);
  263.                 $out .= str_pad($values['num_calls'], 22);
  264.                 $out .=
  265.                 str_pad($values['percentage']."%\n", 8, ' ',
  266.                             STR_PAD_LEFT);
  267.             }
  268.         }
  269.         $out .= "</table>";
  270.         return $out;        
  271.     }    
  272.  
  273.     /**
  274.      * Returns formatted profiling information.
  275.      *
  276.      * @access public
  277.      */
  278.     function display() {
  279.         echo $this->_getOutput();
  280.     }
  281.  
  282.     /**
  283.      * Enters "Global" section.
  284.      *
  285.      * @see    enterSection(), stop()
  286.      * @access public
  287.      */
  288.     function start() {
  289.         $this->enterSection('Global');
  290.     }
  291.  
  292.     /**
  293.      * Leaves "Global" section.
  294.      *
  295.      * @see    leaveSection(), start()
  296.      * @access public
  297.      */
  298.     function stop() {
  299.         $this->leaveSection('Global');
  300.     }
  301.  
  302.     /**
  303.      * Enters code section.
  304.      *
  305.      * @param  string  name of the code section
  306.      * @see    start(), leaveSection()
  307.      * @access public
  308.      */
  309.     function enterSection($name) {
  310.         if (count($this->_stack)) {
  311.             if (isset($this->_callers[$name][$this->_stack[count($this->_stack) - 1]["name"]])) {
  312.                 $this->_callers[$name][$this->_stack[count($this->_stack) - 1]["name"]]++;
  313.             } else {
  314.                 $this->_callers[$name][$this->_stack[count($this->_stack) - 1]["name"]] = 1;
  315.             }
  316.         
  317.             if (isset($this->_calls[$this->_stack[count($this->_stack) - 1]["name"]][$name])) {
  318.                 $this->_calls[$this->_stack[count($this->_stack) - 1]["name"]][$name]++;
  319.             } else {
  320.                 $this->_calls[$this->_stack[count($this->_stack) - 1]["name"]][$name] = 1;
  321.             }
  322.         }
  323.         
  324.         if (isset($this->_num_calls[$name])) {
  325.             $this->_num_calls[$name]++;
  326.         } else {
  327.             $this->_num_calls[$name] = 1;
  328.         }       
  329.  
  330.         $microtime = explode(" ", microtime());
  331.         $microtime = $microtime[1].substr($microtime[0], 1);
  332.         array_push($this->_stack,
  333.                    array("name" => $name, "time" => $microtime));
  334.     }
  335.  
  336.     /**
  337.      * Leaves code section.
  338.      *
  339.      * @param  string  name of the marker to be set
  340.      * @see     stop(), enterSection()
  341.      * @access public
  342.      */
  343.     function leaveSection($name) {
  344.         $microtime = explode(" ", microtime());
  345.         $microtime = $microtime[1].substr($microtime[0], 1);
  346.         $x = array_pop($this->_stack);
  347.         if ($x["name"] != $name) {
  348.             $this->raiseError("reached end of section $name but expecting end of ".
  349.                                $x["name"]."\n",null,PEAR_ERROR_DIE);
  350.         }
  351.  
  352.         if (isset($this->_sections[$name])) {
  353.             $this->_sections[$name] += $microtime - $x["time"];
  354.         } else {
  355.             $this->_sections[$name] = $microtime - $x["time"];
  356.         }
  357.     
  358.     $parent = array_pop($this->_stack);
  359.     if (isset($parent)) {
  360.             if (isset($this->_sub_sections_time[$parent['name']])) {
  361.                 $this->_sub_sections_time[$parent['name']] += $microtime - $x['time'];
  362.             } else {
  363.                 $this->_sub_sections_time[$parent['name']] = $microtime - $x['time'];
  364.             }
  365.         array_push($this->_stack, $parent);
  366.     }
  367.     }
  368.  
  369. }
  370.  
  371. ?>