8 * Simple profiler base class.
9 * @todo document methods (?)
12 class ProfilerSimple
extends Profiler
{
13 var $mMinimumTime = 0;
14 var $mProfileID = false;
16 function __construct() {
17 global $wgRequestTime, $wgRUstart;
18 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
19 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
21 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
22 $elapsedreal = microtime(true) - $wgRequestTime;
24 $entry =& $this->mCollated
["-setup"];
25 if (!is_array($entry)) {
26 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
27 $this->mCollated
["-setup"] =& $entry;
29 $entry['cpu'] +
= $elapsedcpu;
30 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
31 $entry['real'] +
= $elapsedreal;
32 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
37 function setMinimum( $min ) {
38 $this->mMinimumTime
= $min;
41 function setProfileID( $id ) {
42 $this->mProfileID
= $id;
45 function getProfileID() {
46 if ( $this->mProfileID
=== false ) {
49 return $this->mProfileID
;
53 function profileIn($functionname) {
54 global $wgDebugFunctionEntry;
55 if ($wgDebugFunctionEntry) {
56 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
58 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
61 function profileOut($functionname) {
62 global $wgDebugFunctionEntry;
64 if ($wgDebugFunctionEntry) {
65 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
68 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
71 $this->debug("Profiling error: $functionname\n");
73 if ($functionname == 'close') {
74 $message = "Profile section ended by close(): {$ofname}";
75 $functionname = $ofname;
76 $this->debug( "$message\n" );
77 $this->mCollated
[$message] = array(
78 'real' => 0.0, 'count' => 1);
80 elseif ($ofname != $functionname) {
81 $message = "Profiling error: in({$ofname}), out($functionname)";
82 $this->debug( "$message\n" );
83 $this->mCollated
[$message] = array(
84 'real' => 0.0, 'count' => 1);
86 $entry =& $this->mCollated
[$functionname];
87 $elapsedcpu = $this->getCpuTime() - $octime;
88 $elapsedreal = microtime(true) - $ortime;
89 if (!is_array($entry)) {
90 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
91 $this->mCollated
[$functionname] =& $entry;
93 $entry['cpu'] +
= $elapsedcpu;
94 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
95 $entry['real'] +
= $elapsedreal;
96 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
102 function getFunctionReport() {
103 /* Implement in output subclasses */
106 function getCpuTime($ru=null) {
107 if ( function_exists( 'getrusage' ) ) {
111 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
112 $ru['ru_stime.tv_usec']) * 1e-6);
118 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
119 function getTime($time=null) {
121 return microtime(true);
123 list($a,$b)=explode(" ",$time);
124 return (float)($a+
$b);