3 require_once(dirname(__FILE__
).'/Profiler.php');
6 * Simple profiler base class.
7 * @todo document methods (?)
9 class ProfilerSimple
extends Profiler
{
10 var $mMinimumTime = 0;
11 var $mProfileID = false;
13 function __construct() {
14 global $wgRequestTime,$wgRUstart;
15 if (!empty($wgRequestTime) && !empty($wgRUstart)) {
16 $this->mWorkStack
[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
18 $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
19 $elapsedreal = microtime(true) - $wgRequestTime;
21 $entry =& $this->mCollated
["-setup"];
22 if (!is_array($entry)) {
23 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
24 $this->mCollated
["-setup"] =& $entry;
26 $entry['cpu'] +
= $elapsedcpu;
27 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
28 $entry['real'] +
= $elapsedreal;
29 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
34 function setMinimum( $min ) {
35 $this->mMinimumTime
= $min;
38 function setProfileID( $id ) {
39 $this->mProfileID
= $id;
42 function getProfileID() {
43 if ( $this->mProfileID
=== false ) {
46 return $this->mProfileID
;
50 function profileIn($functionname) {
51 global $wgDebugFunctionEntry;
52 if ($wgDebugFunctionEntry) {
53 $this->debug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
55 $this->mWorkStack
[] = array($functionname, count( $this->mWorkStack
), microtime(true), $this->getCpuTime());
58 function profileOut($functionname) {
59 global $wgDebugFunctionEntry;
61 if ($wgDebugFunctionEntry) {
62 $this->debug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
65 list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack
);
68 $this->debug("Profiling error: $functionname\n");
70 if ($functionname == 'close') {
71 $message = "Profile section ended by close(): {$ofname}";
72 $functionname = $ofname;
73 $this->debug( "$message\n" );
75 elseif ($ofname != $functionname) {
76 $message = "Profiling error: in({$ofname}), out($functionname)";
77 $this->debug( "$message\n" );
79 $entry =& $this->mCollated
[$functionname];
80 $elapsedcpu = $this->getCpuTime() - $octime;
81 $elapsedreal = microtime(true) - $ortime;
82 if (!is_array($entry)) {
83 $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
84 $this->mCollated
[$functionname] =& $entry;
86 $entry['cpu'] +
= $elapsedcpu;
87 $entry['cpu_sq'] +
= $elapsedcpu*$elapsedcpu;
88 $entry['real'] +
= $elapsedreal;
89 $entry['real_sq'] +
= $elapsedreal*$elapsedreal;
95 function getFunctionReport() {
96 /* Implement in output subclasses */
99 function getCpuTime($ru=null) {
100 if ( function_exists( 'getrusage' ) ) {
103 return ($ru['ru_utime.tv_sec'] +
$ru['ru_stime.tv_sec'] +
($ru['ru_utime.tv_usec'] +
104 $ru['ru_stime.tv_usec']) * 1e-6);
110 /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
111 function getTime($time=null) {
113 return microtime(true);
114 list($a,$b)=explode(" ",$time);
115 return (float)($a+
$b);
118 function debug( $s ) {
119 if (function_exists( 'wfDebug' ) ) {