(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / ProfilerSimple.php
blob5989061dcdba7ede9d03b443842bf8f6ca5e452c
1 <?php
2 /**
3 * @file
4 * @ingroup Profiler
5 */
7 if ( !class_exists( 'Profiler' ) ) {
8 require_once(dirname(__FILE__).'/Profiler.php');
11 /**
12 * Simple profiler base class.
13 * @todo document methods (?)
14 * @ingroup Profiler
16 class ProfilerSimple extends Profiler {
17 var $mMinimumTime = 0;
18 var $mProfileID = false;
20 function __construct() {
21 global $wgRequestTime, $wgRUstart;
22 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
23 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
25 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
26 $elapsedreal = microtime(true) - $wgRequestTime;
28 $entry =& $this->mCollated["-setup"];
29 if (!is_array($entry)) {
30 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
31 $this->mCollated["-setup"] =& $entry;
33 $entry['cpu'] += $elapsedcpu;
34 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
35 $entry['real'] += $elapsedreal;
36 $entry['real_sq'] += $elapsedreal*$elapsedreal;
37 $entry['count']++;
41 function setMinimum( $min ) {
42 $this->mMinimumTime = $min;
45 function setProfileID( $id ) {
46 $this->mProfileID = $id;
49 function getProfileID() {
50 if ( $this->mProfileID === false ) {
51 return wfWikiID();
52 } else {
53 return $this->mProfileID;
57 function profileIn($functionname) {
58 global $wgDebugFunctionEntry;
59 if ($wgDebugFunctionEntry) {
60 $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
62 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
65 function profileOut($functionname) {
66 global $wgDebugFunctionEntry;
68 if ($wgDebugFunctionEntry) {
69 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
72 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
74 if (!$ofname) {
75 $this->debug("Profiling error: $functionname\n");
76 } else {
77 if ($functionname == 'close') {
78 $message = "Profile section ended by close(): {$ofname}";
79 $functionname = $ofname;
80 $this->debug( "$message\n" );
81 $this->mCollated[$message] = array(
82 'real' => 0.0, 'count' => 1);
84 elseif ($ofname != $functionname) {
85 $message = "Profiling error: in({$ofname}), out($functionname)";
86 $this->debug( "$message\n" );
87 $this->mCollated[$message] = array(
88 'real' => 0.0, 'count' => 1);
90 $entry =& $this->mCollated[$functionname];
91 $elapsedcpu = $this->getCpuTime() - $octime;
92 $elapsedreal = microtime(true) - $ortime;
93 if (!is_array($entry)) {
94 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
95 $this->mCollated[$functionname] =& $entry;
97 $entry['cpu'] += $elapsedcpu;
98 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
99 $entry['real'] += $elapsedreal;
100 $entry['real_sq'] += $elapsedreal*$elapsedreal;
101 $entry['count']++;
106 function getFunctionReport() {
107 /* Implement in output subclasses */
110 function getCpuTime($ru=null) {
111 if ( function_exists( 'getrusage' ) ) {
112 if ( $ru == null )
113 $ru = getrusage();
114 return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
115 $ru['ru_stime.tv_usec']) * 1e-6);
116 } else {
117 return 0;
121 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
122 function getTime($time=null) {
123 if ($time==null)
124 return microtime(true);
125 list($a,$b)=explode(" ",$time);
126 return (float)($a+$b);