Merge "Add release notes entry for wgRelevantArticleId"
[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 ProfilerStandard {
30 protected $trace = "Beginning trace: \n";
31 protected $memory = 0;
32
33 public function profileIn( $functionname ) {
34 parent::profileIn( $functionname );
35
36 $this->trace .= " " . sprintf( "%6.1f", $this->memoryDiff() ) .
37 str_repeat( " ", count( $this->workStack ) ) . " > " . $functionname . "\n";
38 }
39
40 public function profileOut( $functionname ) {
41 $item = end( $this->workStack );
42
43 parent::profileOut( $functionname );
44
45 if ( !$item ) {
46 $this->trace .= "Profiling error: $functionname\n";
47 } else {
48 list( $ofname, /* $ocount */, $ortime ) = $item;
49 if ( $functionname == 'close' ) {
50 $message = "Profile section ended by close(): {$ofname}";
51 $functionname = $ofname;
52 $this->trace .= $message . "\n";
53 } elseif ( $ofname != $functionname ) {
54 $this->trace .= "Profiling error: in({$ofname}), out($functionname)";
55 }
56 $elapsedreal = $this->getTime() - $ortime;
57 $this->trace .= sprintf( "%03.6f %6.1f", $elapsedreal, $this->memoryDiff() ) .
58 str_repeat( " ", count( $this->workStack ) + 1 ) . " < " . $functionname . "\n";
59 }
60 }
61
62 protected function memoryDiff() {
63 $diff = memory_get_usage() - $this->memory;
64 $this->memory = memory_get_usage();
65 return $diff / 1024;
66 }
67
68 public function logData() {
69 if ( $this->templated ) {
70 $contentType = $this->getContentType();
71 if ( PHP_SAPI === 'cli' ) {
72 print "<!-- \n {$this->trace} \n -->";
73 } elseif ( $contentType === 'text/html' ) {
74 print "<!-- \n {$this->trace} \n -->";
75 } elseif ( $contentType === 'text/javascript' ) {
76 print "\n/*\n {$this->trace}\n*/";
77 } elseif ( $contentType === 'text/css' ) {
78 print "\n/*\n {$this->trace}\n*/";
79 }
80 }
81 }
82 }