3 * Simple profiler base class
9 require_once(dirname(__FILE__
).'/Profiler.php');
11 class ProfilerSimple
extends Profiler
{
12 var $mMinimumTime = 0;
13 var $mProfileID = false;
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 setProfileID( $id ) {
41 $this->mProfileID
= $id;
44 function getProfileID() {
45 if ( $this->mProfileID
=== false ) {
48 return $this->mProfileID
;
52 function profileIn($functionname) {
53 global $wgDebugFunctionEntry;
54 if ($wgDebugFunctionEntry) {
55 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
57 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
60 function profileOut($functionname) {
61 global $wgDebugFunctionEntry;
63 if ($wgDebugFunctionEntry) {
64 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
67 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
70 $this->debug("Profiling error: $functionname\n");
72 if ($functionname == 'close') {
73 $message = "Profile section ended by close(): {$ofname}";
74 $functionname = $ofname;
75 $this->debug( "$message\n" );
77 elseif ($ofname != $functionname) {
78 $message = "Profiling error: in({$ofname}), out($functionname)";
79 $this->debug( "$message\n" );
81 $entry =& $this->mCollated
[$functionname];
82 $elapsedcpu = $this->getCpuTime() - $octime;
83 $elapsedreal = microtime(true) - $ortime;
84 if (!is_array($entry)) {
85 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
86 $this->mCollated
[$functionname] =& $entry;
88 $entry['cpu'] +
= $elapsedcpu;
89 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
90 $entry['real'] +
= $elapsedreal;
91 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
97 function getFunctionReport() {
98 /* Implement in output subclasses */
101 function getCpuTime($ru=null) {
102 if ( function_exists( 'getrusage' ) ) {
105 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
106 $ru['ru_stime.tv_usec']) * 1e-6);
112 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
113 function getTime($time=null) {
115 return microtime(true);
116 list($a,$b)=explode(" ",$time);
117 return (float)($a+
$b);
120 function debug( $s ) {
121 if (function_exists( 'wfDebug' ) ) {