summaryrefslogtreecommitdiff
path: root/includes/profiler/ProfilerSimpleTrace.php
blob: 2a4449483c577602a61bc761cfc8d8f0fd0a09e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
 * Profiler showing execution trace.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Profiler
 */

/**
 * Execution trace profiler
 * @todo document methods (?)
 * @ingroup Profiler
 */
class ProfilerSimpleTrace extends ProfilerStandard {
	protected $trace = "Beginning trace: \n";
	protected $memory = 0;

	protected function collateOnly() {
		return true;
	}

	public function profileIn( $functionname ) {
		parent::profileIn( $functionname );

		$this->trace .= "         " . sprintf( "%6.1f", $this->memoryDiff() ) .
			str_repeat( " ", count( $this->mWorkStack ) ) . " > " . $functionname . "\n";
	}

	public function profileOut( $functionname ) {
		$item = end( $this->mWorkStack );

		parent::profileOut( $functionname );

		if ( !$item ) {
			$this->trace .= "Profiling error: $functionname\n";
		} else {
			list( $ofname, /* $ocount */, $ortime ) = $item;
			if ( $functionname == 'close' ) {
				$message = "Profile section ended by close(): {$ofname}";
				$functionname = $ofname;
				$this->trace .= $message . "\n";
			} elseif ( $ofname != $functionname ) {
				$this->trace .= "Profiling error: in({$ofname}), out($functionname)";
			}
			$elapsedreal = $this->getTime() - $ortime;
			$this->trace .= sprintf( "%03.6f %6.1f", $elapsedreal, $this->memoryDiff() ) .
				str_repeat( " ", count( $this->mWorkStack ) + 1 ) . " < " . $functionname . "\n";
		}
	}

	protected function memoryDiff() {
		$diff = memory_get_usage() - $this->memory;
		$this->memory = memory_get_usage();
		return $diff / 1024;
	}

	public function logData() {
		if ( $this->mTemplated ) {
			if ( PHP_SAPI === 'cli' ) {
				print "<!-- \n {$this->trace} \n -->";
			} elseif ( $this->getContentType() === 'text/html' ) {
				print "<!-- \n {$this->trace} \n -->";
			} elseif ( $this->getContentType() === 'text/javascript' ) {
				print "\n/*\n {$this->trace}\n*/";
			} elseif ( $this->getContentType() === 'text/css' ) {
				print "\n/*\n {$this->trace}\n*/";
			}
		}
	}
}