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 __construct() {
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
["-setup"] =& $entry;
30 $entry['cpu'] +
= $elapsedcpu;
31 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
32 $entry['real'] +
= $elapsedreal;
33 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
38 function setMinimum( $min ) {
39 $this->mMinimumTime
= $min;
42 function setProfileID( $id ) {
43 $this->mProfileID
= $id;
46 function getProfileID() {
47 if ( $this->mProfileID
=== false ) {
50 return $this->mProfileID
;
54 function profileIn($functionname) {
55 global $wgDebugFunctionEntry;
56 if ($wgDebugFunctionEntry) {
57 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
59 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
62 function profileOut($functionname) {
63 global $wgDebugFunctionEntry;
65 if ($wgDebugFunctionEntry) {
66 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
69 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
72 $this->debug("Profiling error: $functionname\n");
74 if ($functionname == 'close') {
75 $message = "Profile section ended by close(): {$ofname}";
76 $functionname = $ofname;
77 $this->debug( "$message\n" );
79 elseif ($ofname != $functionname) {
80 $message = "Profiling error: in({$ofname}), out($functionname)";
81 $this->debug( "$message\n" );
83 $entry =& $this->mCollated
[$functionname];
84 $elapsedcpu = $this->getCpuTime() - $octime;
85 $elapsedreal = microtime(true) - $ortime;
86 if (!is_array($entry)) {
87 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
88 $this->mCollated
[$functionname] =& $entry;
90 $entry['cpu'] +
= $elapsedcpu;
91 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
92 $entry['real'] +
= $elapsedreal;
93 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
99 function getFunctionReport() {
100 /* Implement in output subclasses */
103 function getCpuTime($ru=null) {
104 if ( function_exists( 'getrusage' ) ) {
107 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
108 $ru['ru_stime.tv_usec']) * 1e-6);
114 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
115 function getTime($time=null) {
117 return microtime(true);
118 list($a,$b)=explode(" ",$time);
119 return (float)($a+
$b);
122 function debug( $s ) {
123 if (function_exists( 'wfDebug' ) ) {