Didn't really mean to delete SpecialContributions.php back there, but now that
[mediawiki.git] / includes / ProfilerSimple.php
blobe69bfc475b15b84a28d5e880c1997d3d34b684b5
1 <?php
2 /**
3 * Simple profiler base class
4 * @package MediaWiki
5 */
7 /**
8 * @todo document
9 * @package MediaWiki
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["-setup"] =& $entry;
31 $entry['cpu'] += $elapsedcpu;
32 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
33 $entry['real'] += $elapsedreal;
34 $entry['real_sq'] += $elapsedreal*$elapsedreal;
35 $entry['count']++;
39 function setMinimum( $min ) {
40 $this->mMinimumTime = $min;
43 function setProfileID( $id ) {
44 $this->mProfileID = $id;
47 function getProfileID() {
48 if ( $this->mProfileID === false ) {
49 return wfWikiID();
50 } else {
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);
72 if (!$ofname) {
73 $this->debug("Profiling error: $functionname\n");
74 } else {
75 if ($functionname == 'close') {
76 $message = "Profile section ended by close(): {$ofname}";
77 $functionname = $ofname;
78 $this->debug( "$message\n" );
80 elseif ($ofname != $functionname) {
81 $message = "Profiling error: in({$ofname}), out($functionname)";
82 $this->debug( "$message\n" );
84 $entry =& $this->mCollated[$functionname];
85 $elapsedcpu = $this->getCpuTime() - $octime;
86 $elapsedreal = microtime(true) - $ortime;
87 if (!is_array($entry)) {
88 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
89 $this->mCollated[$functionname] =& $entry;
92 $entry['cpu'] += $elapsedcpu;
93 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
94 $entry['real'] += $elapsedreal;
95 $entry['real_sq'] += $elapsedreal*$elapsedreal;
96 $entry['count']++;
101 function getFunctionReport() {
102 /* Implement in output subclasses */
105 function getCpuTime($ru=null) {
106 if ( function_exists( 'getrusage' ) ) {
107 if ( $ru == null )
108 $ru = getrusage();
109 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
110 $ru['ru_stime.tv_usec']) * 1e-6);
111 } else {
112 return 0;
116 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
117 function getTime($time=null) {
118 if ($time==null)
119 return microtime(true);
120 list($a,$b)=explode(" ",$time);
121 return (float)($a+$b);
124 function debug( $s ) {
125 if (function_exists( 'wfDebug' ) ) {
126 wfDebug( $s );