More profiler cleanup:
[lhc/web/wiklou.git] / includes / profiler / ProfilerSimpleTrace.php
1 <?php
2 /**
3 * @file
4 * @ingroup Profiler
5 */
6
7 /**
8 * Execution trace
9 * @todo document methods (?)
10 * @ingroup Profiler
11 */
12 class ProfilerSimpleTrace extends ProfilerSimple {
13 var $mMinimumTime = 0;
14 var $mProfileID = false;
15 var $trace = "";
16 var $memory = 0;
17
18 function __construct() {
19 global $wgRequestTime, $wgRUstart;
20 if ( !empty( $wgRequestTime ) && !empty( $wgRUstart ) ) {
21 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, $this->getCpuTime( $wgRUstart ) );
22 }
23 $this->trace .= "Beginning trace: \n";
24 }
25
26 function profileIn($functionname) {
27 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
28 $this->trace .= " " . sprintf("%6.1f",$this->memoryDiff()) .
29 str_repeat( " ", count($this->mWorkStack)) . " > " . $functionname . "\n";
30 }
31
32 function profileOut($functionname) {
33 global $wgDebugFunctionEntry;
34
35 if ( $wgDebugFunctionEntry ) {
36 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
37 }
38
39 list( $ofname, /* $ocount */ , $ortime, $octime ) = array_pop( $this->mWorkStack );
40
41 if ( !$ofname ) {
42 $this->trace .= "Profiling error: $functionname\n";
43 } else {
44 if ( $functionname == 'close' ) {
45 $message = "Profile section ended by close(): {$ofname}";
46 $functionname = $ofname;
47 $this->trace .= $message . "\n";
48 }
49 elseif ( $ofname != $functionname ) {
50 $this->trace .= "Profiling error: in({$ofname}), out($functionname)";
51 }
52 $elapsedreal = microtime( true ) - $ortime;
53 $this->trace .= sprintf( "%03.6f %6.1f", $elapsedreal, $this->memoryDiff() ) .
54 str_repeat(" ", count( $this->mWorkStack ) + 1 ) . " < " . $functionname . "\n";
55 }
56 }
57
58 function memoryDiff() {
59 $diff = memory_get_usage() - $this->memory;
60 $this->memory = memory_get_usage();
61 return $diff / 1024;
62 }
63
64 function getOutput() {
65 print "<!-- \n {$this->trace} \n -->";
66 }
67 }