8 * Simple profiler base class.
9 * @todo document methods (?)
12 class ProfilerSimple
extends Profiler
{
13 var $mMinimumTime = 0;
15 function __construct( $params ) {
16 global $wgRequestTime, $wgRUstart;
17 parent
::__construct( $params );
18 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
19 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
21 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
22 $elapsedreal = microtime(true) - $wgRequestTime;
24 $entry =& $this->mCollated
["-setup"];
25 if (!is_array($entry)) {
26 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
27 $this->mCollated
["-setup"] =& $entry;
29 $entry['cpu'] +
= $elapsedcpu;
30 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
31 $entry['real'] +
= $elapsedreal;
32 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
37 function setMinimum( $min ) {
38 $this->mMinimumTime
= $min;
41 function profileIn($functionname) {
42 global $wgDebugFunctionEntry;
43 if ($wgDebugFunctionEntry) {
44 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
46 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
49 function profileOut($functionname) {
50 global $wgDebugFunctionEntry;
52 if ($wgDebugFunctionEntry) {
53 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
56 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
59 $this->debug("Profiling error: $functionname\n");
61 if ($functionname == 'close') {
62 $message = "Profile section ended by close(): {$ofname}";
63 $functionname = $ofname;
64 $this->debug( "$message\n" );
65 $this->mCollated
[$message] = array(
66 'real' => 0.0, 'count' => 1);
68 elseif ($ofname != $functionname) {
69 $message = "Profiling error: in({$ofname}), out($functionname)";
70 $this->debug( "$message\n" );
71 $this->mCollated
[$message] = array(
72 'real' => 0.0, 'count' => 1);
74 $entry =& $this->mCollated
[$functionname];
75 $elapsedcpu = $this->getCpuTime() - $octime;
76 $elapsedreal = microtime(true) - $ortime;
77 if (!is_array($entry)) {
78 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
79 $this->mCollated
[$functionname] =& $entry;
81 $entry['cpu'] +
= $elapsedcpu;
82 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
83 $entry['real'] +
= $elapsedreal;
84 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
90 public function getFunctionReport() {
91 /* Implement in output subclasses */
95 public function logData() {
96 /* Implement in subclasses */
99 function getCpuTime($ru=null) {
100 if ( function_exists( 'getrusage' ) ) {
104 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
105 $ru['ru_stime.tv_usec']) * 1e-6);
111 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
112 function getTime($time=null) {
114 return microtime(true);
116 list($a,$b)=explode(" ",$time);
117 return (float)($a+
$b);