Adding extra charsets for ActionMailer unit tests, if you're looking to parse incomin...
[akelos.git] / lib / AkProfiler.php
blobf516fc5ac83fe03e8ec7dc17b3031633f706bb44
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;
27 }else{
28 $pid = getmypid();
29 exec("ps -o rss -p $pid", $output);
30 return $output[1] *1024;
32 return false;
36 class AkProfiler
38 var $_timeStart;
39 var $report = '';
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();
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).' %';
70 $memory = ($timer[2]-$initial_memory)/1024;
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>";
79 $prev_time = $timer[0];
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";
86 function saveReport()
88 if($this->report == ''){
89 $this->renderReport();
91 Ak::file_put_contents('profiler_results.txt',$this->report);
94 function showReport()
96 if($this->report == ''){
97 $this->renderReport();
99 echo $this->report;
100 $this->saveReport();