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 // +----------------------------------------------------------------------+
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;
29 exec("ps -o rss -p $pid", $output);
30 return $output[1] *1024;
40 var $_timer = array();
42 function init($message='Initializing profiler')
44 $this->_timeStart
= $this->getMicrotime();
45 $this->setFlag($message);
48 function getMicrotime()
50 return array_sum(explode(' ',microtime()));
53 function setFlag($flag)
55 $memory = AK_PROFILER_GET_MEMORY ?
memory_get_usage() : 1;
56 $this->_timer
[] = array($this->getMicrotime(), $flag, $memory);
59 function renderReport()
61 $this->setFlag('end');
62 $end_time = $this->getMicrotime();
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).' %';
70 $memory = ($timer[2]-$initial_memory)/1024;
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".
77 "{$memory} KB (".number_format($timer[2]/1024,2)." KB)\n</li>";
79 $prev_time = $timer[0];
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";
88 if($this->report
== ''){
89 $this->renderReport();
91 Ak
::file_put_contents('profiler_results.txt',$this->report
);
96 if($this->report
== ''){
97 $this->renderReport();