Merge "Use ResourceLoader::makeComment to embed page title in wiki modules"
[lhc/web/wiklou.git] / includes / profiler / ProfilerSimpleTrace.php
1 <?php
2 /**
3 * Profiler showing execution trace.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 */
23
24 /**
25 * Execution trace profiler
26 * @todo document methods (?)
27 * @ingroup Profiler
28 */
29 class ProfilerSimpleTrace extends Profiler {
30 protected $trace = "Beginning trace: \n";
31 protected $memory = 0;
32
33 protected function collateOnly() {
34 return true;
35 }
36
37 public function profileIn( $functionname ) {
38 parent::profileIn( $functionname );
39
40 $this->trace .= " " . sprintf( "%6.1f", $this->memoryDiff() ) .
41 str_repeat( " ", count( $this->mWorkStack ) ) . " > " . $functionname . "\n";
42 }
43
44 public function profileOut( $functionname ) {
45 $item = end( $this->mWorkStack );
46
47 parent::profileOut( $functionname );
48
49 if ( !$item ) {
50 $this->trace .= "Profiling error: $functionname\n";
51 } else {
52 list( $ofname, /* $ocount */, $ortime ) = $item;
53 if ( $functionname == 'close' ) {
54 $message = "Profile section ended by close(): {$ofname}";
55 $functionname = $ofname;
56 $this->trace .= $message . "\n";
57 } elseif ( $ofname != $functionname ) {
58 $this->trace .= "Profiling error: in({$ofname}), out($functionname)";
59 }
60 $elapsedreal = $this->getTime() - $ortime;
61 $this->trace .= sprintf( "%03.6f %6.1f", $elapsedreal, $this->memoryDiff() ) .
62 str_repeat( " ", count( $this->mWorkStack ) + 1 ) . " < " . $functionname . "\n";
63 }
64 }
65
66 protected function memoryDiff() {
67 $diff = memory_get_usage() - $this->memory;
68 $this->memory = memory_get_usage();
69 return $diff / 1024;
70 }
71
72 public function logData() {
73 if ( $this->mTemplated ) {
74 if ( PHP_SAPI === 'cli' ) {
75 print "<!-- \n {$this->trace} \n -->";
76 } elseif ( $this->getContentType() === 'text/html' ) {
77 print "<!-- \n {$this->trace} \n -->";
78 } elseif ( $this->getContentType() === 'text/javascript' ) {
79 print "\n/*\n {$this->trace}\n*/";
80 } elseif ( $this->getContentType() === 'text/css' ) {
81 print "\n/*\n {$this->trace}\n*/";
82 }
83 }
84 }
85 }