3 * Simple profiler base class
11 require_once(dirname(__FILE__
).'/Profiler.php');
13 class ProfilerSimple
extends Profiler
{
14 var $mMinimumTime = 0;
15 var $mProfileID = false;
17 function ProfilerSimple() {
18 global $wgRequestTime,$wgRUstart;
19 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
20 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
22 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
23 $elapsedreal = microtime(true) - $wgRequestTime;
25 $entry =& $this->mCollated
["-setup"];
26 if (!is_array($entry)) {
27 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
28 $this->mCollated
[$functionname] =& $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 $memory = memory_get_usage();
66 global $wgDebugFunctionEntry;
68 if ($wgDebugFunctionEntry) {
69 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
72 list($ofname,$ocount,$ortime,$octime) = array_pop($this->mWorkStack
);
75 $this->debug("Profiling error: $functionname\n");
77 if ($functionname == 'close') {
78 $message = "Profile section ended by close(): {$ofname}";
79 $functionname = $ofname;
80 $this->debug( "$message\n" );
82 elseif ($ofname != $functionname) {
83 $message = "Profiling error: in({$ofname}), out($functionname)";
84 $this->debug( "$message\n" );
86 $entry =& $this->mCollated
[$functionname];
87 $elapsedcpu = $this->getCpuTime() - $octime;
88 $elapsedreal = microtime(true) - $ortime;
89 if (!is_array($entry)) {
90 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
91 $this->mCollated
[$functionname] =& $entry;
94 $entry['cpu'] +
= $elapsedcpu;
95 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
96 $entry['real'] +
= $elapsedreal;
97 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
103 function getFunctionReport() {
104 /* Implement in output subclasses */
107 function getCpuTime($ru=null) {
108 if ( function_exists( 'getrusage' ) ) {
111 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
112 $ru['ru_stime.tv_usec']) * 1e-6);
118 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
119 function getTime($time=null) {
121 return microtime(true);
122 list($a,$b)=explode(" ",$time);
123 return (float)($a+
$b);
126 function debug( $s ) {
127 if (function_exists( 'wfDebug' ) ) {