1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Profiler
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Utility class to assist in the process of benchmarking the execution
14 * of sections of code to understand where time is being spent.
15 *
16 * @since 11.1
17 */
18 class JProfiler
19 {
20 /**
21 * @var integer The start time.
22 * @since 12.1
23 */
24 protected $start = 0;
25
26 /**
27 * @var string The prefix to use in the output
28 * @since 12.1
29 */
30 protected $prefix = '';
31
32 /**
33 * @var array The buffer of profiling messages.
34 * @since 12.1
35 */
36 protected $buffer = null;
37
38 /**
39 * @var array The profiling messages.
40 * @since 12.1
41 */
42 protected $marks = null;
43
44 /**
45 * @var float The previous time marker
46 * @since 12.1
47 */
48 protected $previousTime = 0.0;
49
50 /**
51 * @var float The previous memory marker
52 * @since 12.1
53 */
54 protected $previousMem = 0.0;
55
56 /**
57 * @var array JProfiler instances container.
58 * @since 11.3
59 */
60 protected static $instances = array();
61
62 /**
63 * Constructor
64 *
65 * @param string $prefix Prefix for mark messages
66 *
67 * @since 11.1
68 */
69 public function __construct($prefix = '')
70 {
71 $this->start = microtime(1);
72 $this->prefix = $prefix;
73 $this->marks = array();
74 $this->buffer = array();
75 }
76
77 /**
78 * Returns the global Profiler object, only creating it
79 * if it doesn't already exist.
80 *
81 * @param string $prefix Prefix used to distinguish profiler objects.
82 *
83 * @return JProfiler The Profiler object.
84 *
85 * @since 11.1
86 */
87 public static function getInstance($prefix = '')
88 {
89 if (empty(self::$instances[$prefix]))
90 {
91 self::$instances[$prefix] = new JProfiler($prefix);
92 }
93
94 return self::$instances[$prefix];
95 }
96
97 /**
98 * Output a time mark
99 *
100 * @param string $label A label for the time mark
101 *
102 * @return string
103 *
104 * @since 11.1
105 */
106 public function mark($label)
107 {
108 $current = microtime(1) - $this->start;
109 $currentMem = memory_get_usage() / 1048576;
110
111 $m = (object) array(
112 'prefix' => $this->prefix,
113 'time' => ($current > $this->previousTime ? '+' : '-') . (($current - $this->previousTime) * 1000),
114 'totalTime' => ($current * 1000),
115 'memory' => ($currentMem > $this->previousMem ? '+' : '-') . ($currentMem - $this->previousMem),
116 'totalMemory' => $currentMem,
117 'label' => $label,
118 );
119 $this->marks[] = $m;
120
121 $mark = sprintf(
122 '%s %.3f seconds (%.3f); %0.2f MB (%0.3f) - %s',
123 $m->prefix,
124 $m->totalTime / 1000,
125 $m->time / 1000,
126 $m->totalMemory,
127 $m->memory,
128 $m->label
129 );
130 $this->buffer[] = $mark;
131
132 $this->previousTime = $current;
133 $this->previousMem = $currentMem;
134
135 return $mark;
136 }
137
138 /**
139 * Get the current time.
140 *
141 * @return float The current time
142 *
143 * @since 11.1
144 * @deprecated 12.3 (Platform) & 4.0 (CMS) - Use PHP's microtime(1)
145 */
146 public static function getmicrotime()
147 {
148 list ($usec, $sec) = explode(' ', microtime());
149
150 return (float) $usec + (float) $sec;
151 }
152
153 /**
154 * Get information about current memory usage.
155 *
156 * @return integer The memory usage
157 *
158 * @link PHP_MANUAL#memory_get_usage
159 * @since 11.1
160 * @deprecated 12.3 (Platform) & 4.0 (CMS) - Use PHP's native memory_get_usage()
161 */
162 public function getMemory()
163 {
164 return memory_get_usage();
165 }
166
167 /**
168 * Get all profiler marks.
169 *
170 * Returns an array of all marks created since the Profiler object
171 * was instantiated. Marks are objects as per {@link JProfiler::mark()}.
172 *
173 * @return array Array of profiler marks
174 *
175 * @since 11.1
176 */
177 public function getMarks()
178 {
179 return $this->marks;
180 }
181
182 /**
183 * Get all profiler mark buffers.
184 *
185 * Returns an array of all mark buffers created since the Profiler object
186 * was instantiated. Marks are strings as per {@link JProfiler::mark()}.
187 *
188 * @return array Array of profiler marks
189 *
190 * @since 11.1
191 */
192 public function getBuffer()
193 {
194 return $this->buffer;
195 }
196
197 /**
198 * Sets the start time.
199 *
200 * @param double $startTime Unix timestamp in microseconds for setting the Profiler start time.
201 * @param int $startMem Memory amount in bytes for setting the Profiler start memory.
202 *
203 * @return $this For chaining
204 *
205 * @since 12.1
206 */
207 public function setStart($startTime = 0, $startMem = 0)
208 {
209 $this->start = (double) $startTime;
210 $this->previousMem = (int) $startMem / 1048576;
211
212 return $this;
213 }
214 }
215