Follow-up c7df7ade694d38cddc8ec358fe0453cffd6ef379 (r3532):
[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 $trace = "";
14 var $memory = 0;
15
16 function addInitialStack() {
17 global $wgRequestTime, $wgRUstart;
18 if ( !empty( $wgRequestTime ) && !empty( $wgRUstart ) ) {
19 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, $this->getCpuTime( $wgRUstart ) );
20 }
21 $this->trace .= "Beginning trace: \n";
22 }
23
24 function profileIn($functionname) {
25 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
26 $this->trace .= " " . sprintf("%6.1f",$this->memoryDiff()) .
27 str_repeat( " ", count($this->mWorkStack)) . " > " . $functionname . "\n";
28 }
29
30 function profileOut($functionname) {
31 global $wgDebugFunctionEntry;
32
33 if ( $wgDebugFunctionEntry ) {
34 $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
35 }
36
37 list( $ofname, /* $ocount */ , $ortime ) = array_pop( $this->mWorkStack );
38
39 if ( !$ofname ) {
40 $this->trace .= "Profiling error: $functionname\n";
41 } else {
42 if ( $functionname == 'close' ) {
43 $message = "Profile section ended by close(): {$ofname}";
44 $functionname = $ofname;
45 $this->trace .= $message . "\n";
46 }
47 elseif ( $ofname != $functionname ) {
48 $this->trace .= "Profiling error: in({$ofname}), out($functionname)";
49 }
50 $elapsedreal = microtime( true ) - $ortime;
51 $this->trace .= sprintf( "%03.6f %6.1f", $elapsedreal, $this->memoryDiff() ) .
52 str_repeat(" ", count( $this->mWorkStack ) + 1 ) . " < " . $functionname . "\n";
53 }
54 }
55
56 function memoryDiff() {
57 $diff = memory_get_usage() - $this->memory;
58 $this->memory = memory_get_usage();
59 return $diff / 1024;
60 }
61
62 function logData() {
63 print "<!-- \n {$this->trace} \n -->";
64 }
65 }