Localisation updates for core and extension messages from translatewiki.net
[mediawiki.git] / includes / profiler / ProfilerSimple.php
blob055a0ea0f77e7e1ca7476bd271630ea47fcbc588
1 <?php
2 /**
3 * @file
4 * @ingroup Profiler
5 */
7 /**
8 * Simple profiler base class.
9 * @todo document methods (?)
10 * @ingroup Profiler
12 class ProfilerSimple extends Profiler {
13 var $mMinimumTime = 0;
15 var $zeroEntry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
16 var $errorEntry;
18 function __construct( $params ) {
19 global $wgRequestTime, $wgRUstart;
20 parent::__construct( $params );
22 $this->errorEntry = $this->zeroEntry;
23 $this->errorEntry['count'] = 1;
25 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
26 # Remove the -total entry from parent::__construct
27 $this->mWorkStack = array();
29 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
31 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
32 $elapsedreal = microtime(true) - $wgRequestTime;
34 $entry =& $this->mCollated["-setup"];
35 if (!is_array($entry)) {
36 $entry = $this->zeroEntry;
37 $this->mCollated["-setup"] =& $entry;
39 $entry['cpu'] += $elapsedcpu;
40 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
41 $entry['real'] += $elapsedreal;
42 $entry['real_sq'] += $elapsedreal*$elapsedreal;
43 $entry['count']++;
47 function setMinimum( $min ) {
48 $this->mMinimumTime = $min;
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" );
75 $this->mCollated[$message] = $this->errorEntry;
77 elseif ($ofname != $functionname) {
78 $message = "Profiling error: in({$ofname}), out($functionname)";
79 $this->debug( "$message\n" );
80 $this->mCollated[$message] = $this->errorEntry;
82 $entry =& $this->mCollated[$functionname];
83 $elapsedcpu = $this->getCpuTime() - $octime;
84 $elapsedreal = microtime(true) - $ortime;
85 if (!is_array($entry)) {
86 $entry = $this->zeroEntry;
87 $this->mCollated[$functionname] =& $entry;
89 $entry['cpu'] += $elapsedcpu;
90 $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
91 $entry['real'] += $elapsedreal;
92 $entry['real_sq'] += $elapsedreal*$elapsedreal;
93 $entry['count']++;
98 public function getFunctionReport() {
99 /* Implement in output subclasses */
100 return '';
103 public function logData() {
104 /* Implement in subclasses */
107 function getCpuTime($ru=null) {
108 if ( function_exists( 'getrusage' ) ) {
109 if ( $ru == null ) {
110 $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;