Add session accessor functions to WebRequest
[mediawiki.git] / includes / ProfilerSimple.php
blob349a7cac1f42929a333b01ef45fe6ff2a463cadf
1 <?php
2 /**
3 * @file
4 * @ingroup Profiler
5 */
7 require_once(dirname(__FILE__).'/Profiler.php');
9 /**
10 * Simple profiler base class.
11 * @todo document methods (?)
12 * @ingroup Profiler
14 class ProfilerSimple extends Profiler {
15 var $mMinimumTime = 0;
16 var $mProfileID = false;
18 function __construct() {
19 global $wgRequestTime, $wgRUstart;
20 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
21 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
23 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
24 $elapsedreal = microtime(true) - $wgRequestTime;
26 $entry =& $this->mCollated["-setup"];
27 if (!is_array($entry)) {
28 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
29 $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" );
79 $this->mCollated[$message] = array(
80 'real' => 0.0, 'count' => 1);
82 elseif ($ofname != $functionname) {
83 $message = "Profiling error: in({$ofname}), out($functionname)";
84 $this->debug( "$message\n" );
85 $this->mCollated[$message] = array(
86 'real' => 0.0, 'count' => 1);
88 $entry =& $this->mCollated[$functionname];
89 $elapsedcpu = $this->getCpuTime() - $octime;
90 $elapsedreal = microtime(true) - $ortime;
91 if (!is_array($entry)) {
92 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
93 $this->mCollated[$functionname] =& $entry;
95 $entry['cpu'] += $elapsedcpu;
96 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
97 $entry['real'] += $elapsedreal;
98 $entry['real_sq'] += $elapsedreal*$elapsedreal;
99 $entry['count']++;
104 function getFunctionReport() {
105 /* Implement in output subclasses */
108 function getCpuTime($ru=null) {
109 if ( function_exists( 'getrusage' ) ) {
110 if ( $ru == null )
111 $ru = getrusage();
112 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
113 $ru['ru_stime.tv_usec']) * 1e-6);
114 } else {
115 return 0;
119 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
120 function getTime($time=null) {
121 if ($time==null)
122 return microtime(true);
123 list($a,$b)=explode(" ",$time);
124 return (float)($a+$b);