7 if ( !class_exists( 'Profiler' ) ) {
8 require_once(dirname(__FILE__
).'/Profiler.php');
12 * Simple profiler base class.
13 * @todo document methods (?)
16 class ProfilerSimple
extends Profiler
{
17 var $mMinimumTime = 0;
18 var $mProfileID = false;
19 var $mTemplated = false;
21 function __construct() {
22 global $wgRequestTime, $wgRUstart;
23 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
24 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
26 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
27 $elapsedreal = microtime(true) - $wgRequestTime;
29 $entry =& $this->mCollated
["-setup"];
30 if (!is_array($entry)) {
31 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
32 $this->mCollated
["-setup"] =& $entry;
34 $entry['cpu'] +
= $elapsedcpu;
35 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
36 $entry['real'] +
= $elapsedreal;
37 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
42 function setMinimum( $min ) {
43 $this->mMinimumTime
= $min;
46 function setProfileID( $id ) {
47 $this->mProfileID
= $id;
50 function getProfileID() {
51 if ( $this->mProfileID
=== false ) {
54 return $this->mProfileID
;
59 * Mark this call as templated or not
62 function setTemplated( $t ) {
63 $this->mTemplated
= $t;
66 function profileIn($functionname) {
67 global $wgDebugFunctionEntry;
68 if ($wgDebugFunctionEntry) {
69 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
71 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
74 function profileOut($functionname) {
75 global $wgDebugFunctionEntry;
77 if ($wgDebugFunctionEntry) {
78 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
81 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
84 $this->debug("Profiling error: $functionname\n");
86 if ($functionname == 'close') {
87 $message = "Profile section ended by close(): {$ofname}";
88 $functionname = $ofname;
89 $this->debug( "$message\n" );
90 $this->mCollated
[$message] = array(
91 'real' => 0.0, 'count' => 1);
93 elseif ($ofname != $functionname) {
94 $message = "Profiling error: in({$ofname}), out($functionname)";
95 $this->debug( "$message\n" );
96 $this->mCollated
[$message] = array(
97 'real' => 0.0, 'count' => 1);
99 $entry =& $this->mCollated
[$functionname];
100 $elapsedcpu = $this->getCpuTime() - $octime;
101 $elapsedreal = microtime(true) - $ortime;
102 if (!is_array($entry)) {
103 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
104 $this->mCollated
[$functionname] =& $entry;
106 $entry['cpu'] +
= $elapsedcpu;
107 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
108 $entry['real'] +
= $elapsedreal;
109 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
115 function getFunctionReport() {
116 /* Implement in output subclasses */
119 function getCpuTime($ru=null) {
120 if ( function_exists( 'getrusage' ) ) {
123 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
124 $ru['ru_stime.tv_usec']) * 1e-6);
130 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
131 function getTime($time=null) {
133 return microtime(true);
134 list($a,$b)=explode(" ",$time);
135 return (float)($a+
$b);