7 require_once(dirname(__FILE__
).'/Profiler.php');
10 * Simple profiler base class.
11 * @todo document methods (?)
14 class ProfilerSimple
extends Profiler
{
15 var $mMinimumTime = 0;
16 var $mProfileID = false;
18 function __construct() {
19 global $wgRequestTime, $wgRUstart;
20 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
21 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
23 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
24 $elapsedreal = microtime(true) - $wgRequestTime;
26 $entry =& $this->mCollated
["-setup"];
27 if (!is_array($entry)) {
28 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
29 $this->mCollated
["-setup"] =& $entry;
31 $entry['cpu'] +
= $elapsedcpu;
32 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
33 $entry['real'] +
= $elapsedreal;
34 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
39 function setMinimum( $min ) {
40 $this->mMinimumTime
= $min;
43 function setProfileID( $id ) {
44 $this->mProfileID
= $id;
47 function getProfileID() {
48 if ( $this->mProfileID
=== false ) {
51 return $this->mProfileID
;
55 function profileIn($functionname) {
56 global $wgDebugFunctionEntry;
57 if ($wgDebugFunctionEntry) {
58 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
60 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
63 function profileOut($functionname) {
64 global $wgDebugFunctionEntry;
66 if ($wgDebugFunctionEntry) {
67 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
70 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
73 $this->debug("Profiling error: $functionname\n");
75 if ($functionname == 'close') {
76 $message = "Profile section ended by close(): {$ofname}";
77 $functionname = $ofname;
78 $this->debug( "$message\n" );
79 $this->mCollated
[$message] = array(
80 'real' => 0.0, 'count' => 1);
82 elseif ($ofname != $functionname) {
83 $message = "Profiling error: in({$ofname}), out($functionname)";
84 $this->debug( "$message\n" );
85 $this->mCollated
[$message] = array(
86 'real' => 0.0, 'count' => 1);
88 $entry =& $this->mCollated
[$functionname];
89 $elapsedcpu = $this->getCpuTime() - $octime;
90 $elapsedreal = microtime(true) - $ortime;
91 if (!is_array($entry)) {
92 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
93 $this->mCollated
[$functionname] =& $entry;
95 $entry['cpu'] +
= $elapsedcpu;
96 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
97 $entry['real'] +
= $elapsedreal;
98 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
104 function getFunctionReport() {
105 /* Implement in output subclasses */
108 function getCpuTime($ru=null) {
109 if ( function_exists( 'getrusage' ) ) {
112 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
113 $ru['ru_stime.tv_usec']) * 1e-6);
119 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
120 function getTime($time=null) {
122 return microtime(true);
123 list($a,$b)=explode(" ",$time);
124 return (float)($a+
$b);