8 * Simple profiler base class.
9 * @todo document methods (?)
12 class ProfilerSimple
extends Profiler
{
13 var $mMinimumTime = 0;
15 function __construct() {
16 global $wgRequestTime, $wgRUstart;
17 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
18 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
20 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
21 $elapsedreal = microtime(true) - $wgRequestTime;
23 $entry =& $this->mCollated
["-setup"];
24 if (!is_array($entry)) {
25 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
26 $this->mCollated
["-setup"] =& $entry;
28 $entry['cpu'] +
= $elapsedcpu;
29 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
30 $entry['real'] +
= $elapsedreal;
31 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
36 function setMinimum( $min ) {
37 $this->mMinimumTime
= $min;
40 function profileIn($functionname) {
41 global $wgDebugFunctionEntry;
42 if ($wgDebugFunctionEntry) {
43 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
45 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
48 function profileOut($functionname) {
49 global $wgDebugFunctionEntry;
51 if ($wgDebugFunctionEntry) {
52 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
55 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
58 $this->debug("Profiling error: $functionname\n");
60 if ($functionname == 'close') {
61 $message = "Profile section ended by close(): {$ofname}";
62 $functionname = $ofname;
63 $this->debug( "$message\n" );
64 $this->mCollated
[$message] = array(
65 'real' => 0.0, 'count' => 1);
67 elseif ($ofname != $functionname) {
68 $message = "Profiling error: in({$ofname}), out($functionname)";
69 $this->debug( "$message\n" );
70 $this->mCollated
[$message] = array(
71 'real' => 0.0, 'count' => 1);
73 $entry =& $this->mCollated
[$functionname];
74 $elapsedcpu = $this->getCpuTime() - $octime;
75 $elapsedreal = microtime(true) - $ortime;
76 if (!is_array($entry)) {
77 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
78 $this->mCollated
[$functionname] =& $entry;
80 $entry['cpu'] +
= $elapsedcpu;
81 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
82 $entry['real'] +
= $elapsedreal;
83 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
89 public function getFunctionReport() {
90 /* Implement in output subclasses */
94 public function logData() {
95 /* Implement in subclasses */
98 function getCpuTime($ru=null) {
99 if ( function_exists( 'getrusage' ) ) {
103 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
104 $ru['ru_stime.tv_usec']) * 1e-6);
110 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
111 function getTime($time=null) {
113 return microtime(true);
115 list($a,$b)=explode(" ",$time);
116 return (float)($a+
$b);