* Fixed E_NOTICE..
[mediawiki.git] / includes / ProfilerSimple.php
blobb07f2517fb03028bb07deee4ae28ff29aa81c0e3
1 <?php
3 require_once(dirname(__FILE__).'/Profiler.php');
5 /**
6 * Simple profiler base class.
7 * @todo document methods (?)
8 * @addtogroup Profiler
9 */
10 class ProfilerSimple extends Profiler {
11 var $mMinimumTime = 0;
12 var $mProfileID = false;
14 function __construct() {
15 global $wgRequestTime,$wgRUstart;
16 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
17 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
19 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
20 $elapsedreal = microtime(true) - $wgRequestTime;
22 $entry =& $this->mCollated["-setup"];
23 if (!is_array($entry)) {
24 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
25 $this->mCollated["-setup"] =& $entry;
27 $entry['cpu'] += $elapsedcpu;
28 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
29 $entry['real'] += $elapsedreal;
30 $entry['real_sq'] += $elapsedreal*$elapsedreal;
31 $entry['count']++;
35 function setMinimum( $min ) {
36 $this->mMinimumTime = $min;
39 function setProfileID( $id ) {
40 $this->mProfileID = $id;
43 function getProfileID() {
44 if ( $this->mProfileID === false ) {
45 return wfWikiID();
46 } else {
47 return $this->mProfileID;
51 function profileIn($functionname) {
52 global $wgDebugFunctionEntry;
53 if ($wgDebugFunctionEntry) {
54 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
56 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
59 function profileOut($functionname) {
60 global $wgDebugFunctionEntry;
62 if ($wgDebugFunctionEntry) {
63 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
66 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
68 if (!$ofname) {
69 $this->debug("Profiling error: $functionname\n");
70 } else {
71 if ($functionname == 'close') {
72 $message = "Profile section ended by close(): {$ofname}";
73 $functionname = $ofname;
74 $this->debug( "$message\n" );
76 elseif ($ofname != $functionname) {
77 $message = "Profiling error: in({$ofname}), out($functionname)";
78 $this->debug( "$message\n" );
80 $entry =& $this->mCollated[$functionname];
81 $elapsedcpu = $this->getCpuTime() - $octime;
82 $elapsedreal = microtime(true) - $ortime;
83 if (!is_array($entry)) {
84 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
85 $this->mCollated[$functionname] =& $entry;
87 $entry['cpu'] += $elapsedcpu;
88 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
89 $entry['real'] += $elapsedreal;
90 $entry['real_sq'] += $elapsedreal*$elapsedreal;
91 $entry['count']++;
96 function getFunctionReport() {
97 /* Implement in output subclasses */
100 function getCpuTime($ru=null) {
101 if ( function_exists( 'getrusage' ) ) {
102 if ( $ru == null )
103 $ru = getrusage();
104 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
105 $ru['ru_stime.tv_usec']) * 1e-6);
106 } else {
107 return 0;
111 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
112 function getTime($time=null) {
113 if ($time==null)
114 return microtime(true);
115 list($a,$b)=explode(" ",$time);
116 return (float)($a+$b);
119 function debug( $s ) {
120 if (function_exists( 'wfDebug' ) ) {
121 wfDebug( $s );