Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
10
 * @link          http://cakephp.org CakePHP(tm) Project
11
 * @since         DebugKit 0.1
12
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
13
 */
14
 
15
App::uses('Debugger', 'Utility');
16
 
17
/**
18
 * Contains methods for Profiling and creating timers.
19
 *
20
 */
21
class DebugTimer {
22
 
23
/**
24
 * Internal timers array
25
 *
26
 * @var array
27
 */
28
	protected static $_timers = array();
29
 
30
/**
31
 * Start an benchmarking timer.
32
 *
33
 * @param string $name The name of the timer to start.
34
 * @param string $message A message for your timer
35
 * @return boolean Always true
36
 */
37
	public static function start($name = null, $message = null) {
38
		$start = microtime(true);
39
 
40
		if (!$name) {
41
			$named = false;
42
			$calledFrom = debug_backtrace();
43
			$_name = $name = Debugger::trimpath($calledFrom[0]['file']) . ' line ' . $calledFrom[0]['line'];
44
		} else {
45
			$named = true;
46
		}
47
 
48
		if (!$message) {
49
			$message = $name;
50
		}
51
 
52
		$_name = $name;
53
		$i = 1;
54
		while (isset(self::$_timers[$name])) {
55
			$i++;
56
			$name = $_name . ' #' . $i;
57
		}
58
 
59
		if ($i > 1) {
60
			$message .= ' #' . $i;
61
		}
62
 
63
		self::$_timers[$name] = array(
64
			'start' => $start,
65
			'message' => $message,
66
			'named' => $named
67
		);
68
		return true;
69
	}
70
 
71
/**
72
 * Stop a benchmarking timer.
73
 *
74
 * $name should be the same as the $name used in startTimer().
75
 *
76
 * @param string $name The name of the timer to end.
77
 * @return boolean true if timer was ended, false if timer was not started.
78
 */
79
	public static function stop($name = null) {
80
		$end = microtime(true);
81
		if (!$name) {
82
			$names = array_reverse(array_keys(self::$_timers));
83
			foreach ($names as $name) {
84
				if (!empty(self::$_timers[$name]['end'])) {
85
					continue;
86
				}
87
				if (empty(self::$_timers[$name]['named'])) {
88
					break;
89
				}
90
			}
91
		} else {
92
			$i = 1;
93
			$_name = $name;
94
			while (isset(self::$_timers[$name])) {
95
				if (empty(self::$_timers[$name]['end'])) {
96
					break;
97
				}
98
				$i++;
99
				$name = $_name . ' #' . $i;
100
			}
101
		}
102
		if (!isset(self::$_timers[$name])) {
103
			return false;
104
		}
105
		self::$_timers[$name]['end'] = $end;
106
		return true;
107
	}
108
 
109
/**
110
 * Get all timers that have been started and stopped.
111
 * Calculates elapsed time for each timer. If clear is true, will delete existing timers
112
 *
113
 * @param boolean $clear false
114
 * @return array
115
 */
116
	public static function getAll($clear = false) {
117
		$start = self::requestStartTime();
118
		$now = microtime(true);
119
 
120
		$times = array();
121
		if (!empty(self::$_timers)) {
122
			$firstTimer = reset(self::$_timers);
123
			$_end = $firstTimer['start'];
124
		} else {
125
			$_end = $now;
126
		}
127
		$times['Core Processing (Derived from $_SERVER["REQUEST_TIME"])'] = array(
128
			'message' => __d('debug_kit', 'Core Processing (Derived from $_SERVER["REQUEST_TIME"])'),
129
			'start' => 0,
130
			'end' => $_end - $start,
131
			'time' => round($_end - $start, 6),
132
			'named' => null
133
		);
134
		foreach (self::$_timers as $name => $timer) {
135
			if (!isset($timer['end'])) {
136
				$timer['end'] = $now;
137
			}
138
			$times[$name] = array_merge($timer, array(
139
				'start' => $timer['start'] - $start,
140
				'end' => $timer['end'] - $start,
141
				'time' => self::elapsedTime($name)
142
			));
143
		}
144
		if ($clear) {
145
			self::$_timers = array();
146
		}
147
		return $times;
148
	}
149
 
150
/**
151
 * Clear all existing timers
152
 *
153
 * @return boolean true
154
 */
155
	public static function clear() {
156
		self::$_timers = array();
157
		return true;
158
	}
159
 
160
/**
161
 * Get the difference in time between the timer start and timer end.
162
 *
163
 * @param $name string the name of the timer you want elapsed time for.
164
 * @param $precision int the number of decimal places to return, defaults to 5.
165
 * @return float number of seconds elapsed for timer name, 0 on missing key
166
 */
167
	public static function elapsedTime($name = 'default', $precision = 5) {
168
		if (!isset(self::$_timers[$name]['start']) || !isset(self::$_timers[$name]['end'])) {
169
			return 0;
170
		}
171
		return round(self::$_timers[$name]['end'] - self::$_timers[$name]['start'], $precision);
172
	}
173
 
174
/**
175
 * Get the total execution time until this point
176
 *
177
 * @return float elapsed time in seconds since script start.
178
 */
179
	public static function requestTime() {
180
		$start = self::requestStartTime();
181
		$now = microtime(true);
182
		return ($now - $start);
183
	}
184
 
185
/**
186
 * get the time the current request started.
187
 *
188
 * @return float time of request start
189
 */
190
	public static function requestStartTime() {
191
		if (defined('TIME_START')) {
192
			$startTime = TIME_START;
193
		} elseif (isset($GLOBALS['TIME_START'])) {
194
			$startTime = $GLOBALS['TIME_START'];
195
		} else {
196
			$startTime = env('REQUEST_TIME');
197
		}
198
		return $startTime;
199
	}
200
 
201
}