8 * Simple profiler base class.
9 * @todo document methods (?)
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);
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;
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
);
69 $this->debug("Profiling error: $functionname\n");
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;
98 public function getFunctionReport() {
99 /* Implement in output subclasses */
103 public function logData() {
104 /* Implement in subclasses */
107 function getCpuTime($ru=null) {
108 if ( function_exists( '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);