Profiler.php
4.44 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Profiler;
defined('JPATH_PLATFORM') or die;
/**
* Utility class to assist in the process of benchmarking the execution
* of sections of code to understand where time is being spent.
*
* @since 1.7.0
*/
class Profiler
{
/**
* @var integer The start time.
* @since 3.0.0
*/
protected $start = 0;
/**
* @var string The prefix to use in the output
* @since 3.0.0
*/
protected $prefix = '';
/**
* @var array The buffer of profiling messages.
* @since 3.0.0
*/
protected $buffer = null;
/**
* @var array The profiling messages.
* @since 3.0.0
*/
protected $marks = null;
/**
* @var float The previous time marker
* @since 3.0.0
*/
protected $previousTime = 0.0;
/**
* @var float The previous memory marker
* @since 3.0.0
*/
protected $previousMem = 0.0;
/**
* @var array JProfiler instances container.
* @since 1.7.3
*/
protected static $instances = array();
/**
* Constructor
*
* @param string $prefix Prefix for mark messages
*
* @since 1.7.0
*/
public function __construct($prefix = '')
{
$this->start = microtime(1);
$this->prefix = $prefix;
$this->marks = array();
$this->buffer = array();
}
/**
* Returns the global Profiler object, only creating it
* if it doesn't already exist.
*
* @param string $prefix Prefix used to distinguish profiler objects.
*
* @return Profiler The Profiler object.
*
* @since 1.7.0
*/
public static function getInstance($prefix = '')
{
if (empty(self::$instances[$prefix]))
{
self::$instances[$prefix] = new Profiler($prefix);
}
return self::$instances[$prefix];
}
/**
* Output a time mark
*
* @param string $label A label for the time mark
*
* @return string
*
* @since 1.7.0
*/
public function mark($label)
{
$current = microtime(1) - $this->start;
$currentMem = memory_get_usage() / 1048576;
$m = (object) array(
'prefix' => $this->prefix,
'time' => ($current > $this->previousTime ? '+' : '-') . (($current - $this->previousTime) * 1000),
'totalTime' => ($current * 1000),
'memory' => ($currentMem > $this->previousMem ? '+' : '-') . ($currentMem - $this->previousMem),
'totalMemory' => $currentMem,
'label' => $label,
);
$this->marks[] = $m;
$mark = sprintf(
'%s %.3f seconds (%.3f); %0.2f MB (%0.3f) - %s',
$m->prefix,
$m->totalTime / 1000,
$m->time / 1000,
$m->totalMemory,
$m->memory,
$m->label
);
$this->buffer[] = $mark;
$this->previousTime = $current;
$this->previousMem = $currentMem;
return $mark;
}
/**
* Get the current time.
*
* @return float The current time
*
* @since 1.7.0
* @deprecated 4.0 - Use PHP's microtime(1)
*/
public static function getmicrotime()
{
list ($usec, $sec) = explode(' ', microtime());
return (float) $usec + (float) $sec;
}
/**
* Get information about current memory usage.
*
* @return integer The memory usage
*
* @link PHP_MANUAL#memory_get_usage
* @since 1.7.0
* @deprecated 4.0 - Use PHP's native memory_get_usage()
*/
public function getMemory()
{
return memory_get_usage();
}
/**
* Get all profiler marks.
*
* Returns an array of all marks created since the Profiler object
* was instantiated. Marks are objects as per {@link JProfiler::mark()}.
*
* @return array Array of profiler marks
*
* @since 1.7.0
*/
public function getMarks()
{
return $this->marks;
}
/**
* Get all profiler mark buffers.
*
* Returns an array of all mark buffers created since the Profiler object
* was instantiated. Marks are strings as per {@link Profiler::mark()}.
*
* @return array Array of profiler marks
*
* @since 1.7.0
*/
public function getBuffer()
{
return $this->buffer;
}
/**
* Sets the start time.
*
* @param double $startTime Unix timestamp in microseconds for setting the Profiler start time.
* @param int $startMem Memory amount in bytes for setting the Profiler start memory.
*
* @return $this For chaining
*
* @since 3.0.0
*/
public function setStart($startTime = 0.0, $startMem = 0)
{
$this->start = (double) $startTime;
$this->previousMem = (int) $startMem / 1048576;
return $this;
}
}