File: SpotTiming.php

package info (click to toggle)
spotweb 20130826%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,132 kB
  • ctags: 11,281
  • sloc: php: 31,367; xml: 1,009; sh: 148; makefile: 83
file content (51 lines) | stat: -rwxr-xr-x 1,634 bytes parent folder | download | duplicates (2)
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
<?php

class SpotTiming {
	static private $_disabled = true;
	static private $_timings = array();
	static private $_inflight = array();
	static private $_curlevel = 0;
	
	static function enable() {
		self::$_disabled = false;
	} # enable
	
	static function disable() {
		self::$_disabled = true;
	} #disable
	
	static function start($name) {
		if (self::$_disabled) return;
		
		self::$_curlevel++;
		self::$_inflight[$name] = array('start' => microtime(true));
	} # start
	
	static function stop($name, $extra = '') {
		if (self::$_disabled) return;

		self::$_inflight[$name]['stop'] = microtime(true);
		self::$_inflight[$name]['extra'] = $extra;
		self::$_inflight[$name]['level'] = self::$_curlevel;
		self::$_curlevel--;

		self::$_timings[] = array_merge(self::$_inflight[$name], array('name' => $name));
		unset(self::$_inflight[$name]);
	} # stop
	
	static function display() {
		if (self::$_disabled) return;
		
		echo '<table style="border: 1px solid black; border-collapse: collapse;" border=1><tr><th>Name</th><th>Time</th><th>Extra</th></tr>';
		
		foreach(array_reverse(self::$_timings) as $values) {
			try {
				echo '<tr><td>' . str_pad('', $values['level'], '.') . $values['name'] . '</td><td>' . ($values['stop'] - $values['start']) . '</td><td>' . serialize($values['extra']) . '</td></tr>' . PHP_EOL;
			} catch(Exception $x) {
				echo '<tr><td>' . str_pad('', $values['level'], '.') . $values['name'] . '</td><td>' . ($values['stop'] - $values['start']) . '</td><td> [Unserializable data]</td></tr>' . PHP_EOL;
			} # catch
		} # foreach
	
		echo '</table><br><br><br><br>';
	} # display()
} # class SpotTiming