forked from jerome-breton/xhprof-simple-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler.php
295 lines (255 loc) · 10.6 KB
/
profiler.php
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
class JbnProfiler
{
//Allowed ip as they can be read in $_SERVER['REMOTE_ADDR']
//You might want to alter _getRemoteAddr() if you have a reverse proxy
protected $_allowedIp = array(
'10.0.0.0/8', //Local network
'172.16.0.0/12', //Local network
'192.168.0.0/16', //Local network
);
//Can be set to false if method is not allowed
protected $_enableKey = 'PROFILE';
protected $_enableKeyGet = true;
protected $_enableKeyPost = true;
protected $_enableKeyCookie = true;
protected $_enableKeyEnv = true;
//If the key is truthy with this value then the trace will be triggered but output will not be altered
//(footer will not be added, usefull for AJAX calls)
protected $_silentWord = 'silent';
//This class will use $_SERVER['HTTP_HOST'] as the namespace
//Passing this parameter will allow to prefix it with something (useful for sorting profiles)
//You can set this parameter to false to disallow some methods
protected $_namespaceKey = 'PROFILENS';
protected $_namespaceKeyGet = true;
protected $_namespaceKeyPost = true;
protected $_namespaceKeyCookie = true;
protected $_namespaceKeyEnv = true;
//Allows or disallow CLI usage of profiler
protected $_allowedCli = true;
//Base URL of the html folder of this projet
protected $_baseUrl = 'http://localhost/html';
//XHProf Flags for profiling, defaults to CPU + MEMORY
protected $_flags = -1;
//CSS passed in style attribute of the <div> and <a> added at the end of the page
protected $_boxStyle = 'font-size:1.25em;border:solid 0.125em red;background:white;margin:1em;clear:both;text-align: center;';
protected $_linkStyle = 'padding:0.25em 0.5em;margin:0.5em 0.25em;-webkit-appearance: button;-moz-appearance: button;appearance: button;text-decoration: none;color: initial;';
/** @var ProfilerRuns_Default */
private $_manager;
public function __construct($params = array(), $debug = false)
{
foreach($params as $key => $value){
if(!empty($value)){
$varName = '_'.$key;
$this->$varName = $value;
}
}
if ($debug) {
$this->debugProfilerEnabled();
}
if ($this->_profilerEnabled()) {
//Include XHProf libs
require_once __DIR__.'/lib/utils/lib.php';
require_once __DIR__.'/lib/utils/runs.php';
$this->_manager = new ProfilerRuns_Default();
//Register function that will stop profiling at the end
register_shutdown_function(array($this, 'doShutdown'));
//Begin profiling
call_user_func($this->_getExtensionName().'_enable', $this->_getFlags());
}
}
public function doShutdown()
{
//Stop profiling
$profile = call_user_func($this->_getExtensionName().'_disable');
$profileId = $this->_manager->save_run($profile, $this->_getProfileNamespace());
$this->_displayFooter($profileId);
}
protected function _profilerEnabled()
{
return $this->_extensionLoaded() && $this->_enableKeyPresent() && $this->_allowed();
}
public function debugProfilerEnabled()
{
ini_set('display_errors', 1);
echo '<br />Extension Loaded:' . ($this->_extensionLoaded() ? $this->_getExtensionName() : 'n');
echo '<br />Get Found:' . ($this->_enableKeyGet() ? 'y' : 'n');
echo '<br />Post Found:' . ($this->_enableKeyPost() ? 'y' : 'n');
echo '<br />Cookie Found:' . ($this->_enableKeyCookie() ? 'y' : 'n');
echo '<br />CLI:' . ($this->_isCli() ? 'y' : 'n');
echo '<br />IP:' . $this->_getRemoteAddr();
echo 'Allowed:' . ($this->_allowed() ? 'y' : 'n');
}
protected function _extensionLoaded()
{
return (bool)$this->_getExtensionName();
}
protected function _enableKeyPresent()
{
return $this->_enableKeyGet() || $this->_enableKeyPost() || $this->_enableKeyCookie() || $this->_enableKeyEnv();
}
private function _enableKeyGet()
{
return ($this->_enableKeyGet && isset($_GET[$this->_enableKey])) ? $_GET[$this->_enableKey] : false;
}
private function _enableKeyPost()
{
return ($this->_enableKeyPost && isset($_POST[$this->_enableKey])) ? $_POST[$this->_enableKey] : false;
}
private function _enableKeyCookie()
{
return ($this->_enableKeyCookie && isset($_COOKIE[$this->_enableKey])) ? $_COOKIE[$this->_enableKey] : false;
}
private function _enableKeyEnv()
{
return ($this->_enableKeyEnv && isset($_SERVER[$this->_enableKey])) ? $_SERVER[$this->_enableKey] : false;
}
protected function _allowed()
{
//If CLI we only check allowedCli
if ($this->_isCli()) {
return $this->_allowedCli;
}
//Else, we check the IP
$ip = ip2long($this->_getRemoteAddr());
foreach ($this->_allowedIp as $range) {
if (strstr($range, '/') !== false) {
$range = explode('/', $range);
$corr = (pow(2, 32) - 1) - (pow(2, 32 - $range[1]) - 1);
$first = ip2long($range[0]) & ($corr);
$length = pow(2, 32 - $range[1]) - 1;
if ($ip >= $first || $ip <= ($first + $length)) {
return true;
}
} else {
if ($ip == ip2long($range)) {
return true;
}
}
}
return false;
}
protected function _getProfileNamespace()
{
if ($get = $this->_getProfileNamespacePrefixGet()) {
$namespace = $get . '_';
} elseif ($post = $this->_getProfileNamespacePrefixPost()) {
$namespace = $post . '_';
} elseif ($cookie = $this->_getProfileNamespacePrefixCookie()) {
$namespace = $cookie . '_';
} elseif ($env = $this->_getProfileNamespacePrefixEnv()) {
$namespace = $env . '_';
} else {
$namespace = '';
}
if($this->_isCli()){
return $namespace . 'CLI';
}
return $namespace . str_replace('.','_',$_SERVER['HTTP_HOST']);
}
protected function _silentOutput()
{
return ($this->_enableKeyGet() == $this->_silentWord)
|| ($this->_enableKeyPost() == $this->_silentWord)
|| ($this->_enableKeyCookie() == $this->_silentWord)
|| ($this->_enableKeyEnv() == $this->_silentWord)
|| $this->_isNotHtml();
}
//Try to detect non HTML responses such as binary or XHR to not break them
protected function _isNotHtml(){
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') //Browser XHR header
|| (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'],'html') === false); //Browser does not expect HTML
}
private function _getProfileNamespacePrefixGet()
{
return ($this->_namespaceKeyGet && isset($_GET[$this->_namespaceKey])) ? $_GET[$this->_namespaceKey] : false;
}
private function _getProfileNamespacePrefixPost()
{
return ($this->_namespaceKeyPost && isset($_POST[$this->_namespaceKey])) ? $_POST[$this->_namespaceKey] : false;
}
private function _getProfileNamespacePrefixCookie()
{
return ($this->_namespaceKeyCookie && isset($_COOKIE[$this->_namespaceKey])) ? $_COOKIE[$this->_namespaceKey] : false;
}
private function _getProfileNamespacePrefixEnv()
{
return ($this->_namespaceKeyEnv && isset($_SERVER[$this->_namespaceKey])) ? $_SERVER[$this->_namespaceKey] : false;
}
protected function _getBaseUrl()
{
return $this->_baseUrl;
}
/**
* @return mixed
*/
protected function _getRemoteAddr()
{
return $_SERVER['REMOTE_ADDR'];
}
/**
* @return bool
*/
protected function _isCli()
{
return PHP_SAPI == 'cli';
}
/**
* @param $profileId
*/
protected function _displayFooter($profileId)
{
if ($this->_silentOutput()) {
return;
}
$urls = array(
'📅 Profiler' => sprintf('%s/index.php?run=%s&source=%s', $this->_getBaseUrl(), $profileId, $this->_getProfileNamespace()),
'🔀 Callgraph' => sprintf('%s/callgraph.php?run=%s&source=%s', $this->_getBaseUrl(), $profileId, $this->_getProfileNamespace()),
'🕙 Previous runs' => $this->_getBaseUrl()
);
if ($this->_isCli()) {
echo "\n------------------------------------------------------------------------------------------------------------\n";
echo "- Profile path:\t\t{$this->_manager->get_dir()}/{$profileId}.{$this->_getProfileNamespace()}.{$this->_manager->get_suffix()}\n";
foreach($urls as $title => $url){
echo "- {$title}:\t{$url}\n";
}
echo "------------------------------------------------------------------------------------------------------------\n";
} else {
if($this->_enableKeyCookie()){
$urls['❌ Disable persistence'] = 'javascript:(function(){document.cookie=\'' . $this->_enableKey .'=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/\'})()';
} else {
$urls['📌 Enable persistence'] = 'javascript:(function(){document.cookie=\'' . $this->_enableKey .'=1; path=/\'})()';
}
echo "<div style=\"{$this->_boxStyle}\">";
foreach($urls as $title => $url){
echo "<a href=\"{$url}\" target=\"_blank\" style=\"{$this->_linkStyle}\">{$title}</a>";
}
echo "</div>";
}
}
protected function _getExtensionName()
{
if (extension_loaded('tideways_xhprof')) {
return 'tideways_xhprof';
} else if (extension_loaded('tideways')) {
return 'tideways';
} else if (extension_loaded('uprofiler')) {
return 'uprofiler';
} else if(extension_loaded('xhprof')) {
return 'xhprof';
}
return false;
}
/**
* Get profiling flags compatible with xhprof or tideways
* Value can be overloaded in construct(array('flags' => XHPROF_FLAGS_CPU))
*/
protected function _getFlags(){
if($this->_flags != -1){
return $this->_flags;
}
$flagsCpu = constant(strtoupper($this->_getExtensionName()).'_FLAGS_CPU');
$flagsMemory = constant(strtoupper($this->_getExtensionName()).'_FLAGS_MEMORY');
return $flagsCpu + $flagsMemory;
}
}