Fixes unit test after changes in [549].
[akelos.git] / lib / AkProfiler.php
blob68c1cfd3e407d02ee92ccca75fc197de7e364b00
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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 // +----------------------------------------------------------------------+
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>
19 if(!function_exists('memory_get_usage')){
20 function memory_get_usage()
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;
28 return false;
32 class AkProfiler
34 var $_timeStart;
35 var $report = '';
36 var $_timer = array();
38 function init($message='Initializing profiler')
40 $this->_timeStart = $this->getMicrotime();
41 $this->setFlag($message);
44 function getMicrotime()
46 return array_sum(explode(' ',microtime()));
49 function setFlag($flag)
51 $memory = AK_PROFILER_GET_MEMORY ? memory_get_usage() : 1;
52 $this->_timer[] = array($this->getMicrotime(), $flag, $memory);
55 function renderReport()
57 $this->setFlag('end');
58 $end_time = $this->getMicrotime();
59 $report = array();
60 $this->report = '';
61 $prev_time = $this->_timeStart;
62 foreach($this->_timer as $k=>$timer ){
63 $initial_memory = !isset($initial_memory) ? $timer[2] : $initial_memory;
64 $average = number_format(100*(($timer[0]-$prev_time)/($end_time-$this->_timeStart)),4).' %';
66 $memory = ($timer[2]-$initial_memory)/1024;
68 $report[] =
69 "<li>$average (".($k+1).") {$timer[1]}\t".
70 number_format($timer[0]-$this->_timeStart,6)."\t".
71 number_format(($timer[0] - $prev_time),6)."\t".
72 "$average\t".
73 "{$memory} KB (".number_format($timer[2]/1024,2)." KB)\n</li>";
75 $prev_time = $timer[0];
77 natsort($report);
78 $report = array_reverse($report);
79 $this->report .= "flag\tstarted\telapsed\taverage\n\n\nTotal time: <ul>".join("\n",$report).number_format($end_time-$this->_timeStart,6)."</ul>\n";
82 function saveReport()
84 if($this->report == ''){
85 $this->renderReport();
87 Ak::file_put_contents('profiler_results.txt',$this->report);
90 function showReport()
92 if($this->report == ''){
93 $this->renderReport();
95 echo $this->report;
96 $this->saveReport();