[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkProfiler.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActiveSupport
  13   * @subpackage Reporting
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  if(!function_exists('memory_get_usage')){
  20      function memory_get_usage()
  21      {
  22          if ( substr(PHP_OS,0,3) == 'WIN') {
  23              $tmp = explode(',"'.getmypid().'",',`TASKLIST /FO "CSV"`);
  24              $tmp = explode("\n",$tmp[1]);
  25              $tmp = explode('"',trim($tmp[0],'"KB '));
  26              return intval(str_replace(array('.',','),array(''),$tmp[count($tmp)-1]))*1024;
  27          }else{
  28              $pid = getmypid();
  29              exec("ps -o rss -p $pid", $output);
  30              return $output[1] *1024;
  31          }
  32          return false;
  33      }
  34  }
  35  
  36  class AkProfiler
  37  {
  38      var $_timeStart;
  39      var $report = '';
  40      var $_timer = array();
  41  
  42      function init($message='Initializing profiler')
  43      {
  44          $this->_timeStart = $this->getMicrotime();
  45          $this->setFlag($message);
  46      }
  47  
  48      function getMicrotime()
  49      {
  50          return array_sum(explode(' ',microtime()));
  51      }
  52  
  53      function setFlag($flag)
  54      {
  55          $memory = AK_PROFILER_GET_MEMORY ? memory_get_usage() : 1;
  56          $this->_timer[] = array($this->getMicrotime(), $flag, $memory);
  57      }
  58  
  59      function renderReport()
  60      {
  61          $this->setFlag('end');
  62          $end_time = $this->getMicrotime();
  63          $report = array();
  64          $this->report = '';
  65          $prev_time = $this->_timeStart;
  66          foreach($this->_timer as $k=>$timer ){
  67              $initial_memory = !isset($initial_memory) ? $timer[2] : $initial_memory;
  68              $average = number_format(100*(($timer[0]-$prev_time)/($end_time-$this->_timeStart)),4).' %';
  69  
  70              $memory = ($timer[2]-$initial_memory)/1024;
  71  
  72              $report[] =
  73              "<li>$average (".($k+1).") {$timer[1]}\t".
  74              number_format($timer[0]-$this->_timeStart,6)."\t".
  75              number_format(($timer[0] - $prev_time),6)."\t".
  76              "$average\t".
  77              "{$memory} KB (".number_format($timer[2]/1024,2)." KB)\n</li>";
  78  
  79              $prev_time = $timer[0];
  80          }
  81          natsort($report);
  82          $report = array_reverse($report);
  83          $this->report .= "flag\tstarted\telapsed\taverage\n\n\nTotal time: <ul>".join("\n",$report).number_format($end_time-$this->_timeStart,6)."</ul>\n";
  84      }
  85  
  86      function saveReport()
  87      {
  88          if($this->report == ''){
  89              $this->renderReport();
  90          }
  91          Ak::file_put_contents('profiler_results.txt',$this->report);
  92      }
  93  
  94      function showReport()
  95      {
  96          if($this->report == ''){
  97              $this->renderReport();
  98          }
  99          echo $this->report;
 100          $this->saveReport();
 101      }
 102  
 103  }
 104  
 105  
 106  
 107  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6