forked from carlosbuenosvinos/opcache-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcache.php
278 lines (251 loc) · 10 KB
/
opcache.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
<?php
if (isset($_GET['invalidate'])) {
opcache_invalidate($_GET['invalidate'], true);
header('Location: ' . $_SERVER['PHP_SELF'].'#scripts');
}
if (isset($_GET['reset'])) {
opcache_reset();
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ' . $_SERVER['PHP_SELF'].'#scripts');
}
/**
* Fetch configuration and status information from OpCache
*/
$config = opcache_get_configuration();
$status = opcache_get_status();
/**
* Turn bytes into a human readable format
* @param $bytes
*/
function size_for_humans($bytes)
{
if ($bytes > 1048576) {
return sprintf("%.2f MB", $bytes/1048576);
} elseif ($bytes > 1024) {
return sprintf("%.2f kB", $bytes/1024);
} else {
return sprintf("%d bytes", $bytes);
}
}
function getOffsetWhereStringsAreEqual($a, $b)
{
$i = 0;
while (strlen($a) && strlen($b) && strlen($a) > $i && $a[$i] === $b[$i]) {
$i++;
}
return $i;
}
function getSuggestionMessage($property, $value)
{
switch ($property) {
case 'opcache_enabled':
return $value ? '' : '<span class="glyphicon glyphicon-search"></span> You should enabled opcache';
break;
case 'cache_full':
return $value ? '<span class="glyphicon glyphicon-search"></span> You should increase opcache.memory_consumption' : '';
break;
case 'opcache.validate_timestamps':
return $value ? '<span class="glyphicon glyphicon-search"></span> If you are in a production environment you should disabled it' : '';
break;
}
return '';
}
function getStringFromPropertyAndValue($property, $value)
{
if ($value === false) {
return 'false';
}
if ($value === true) {
return 'true';
}
switch ($property) {
case 'used_memory':
case 'free_memory':
case 'wasted_memory':
case 'opcache.memory_consumption':
return size_for_humans($value);
break;
case 'current_wasted_percentage':
case 'opcache_hit_rate':
return number_format($value, 2).'%';
break;
case 'blacklist_miss_ratio':
return number_format($value, 2);
break;
}
return $value;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>OPcache Dashboard - Carlos Buenosvinos (@buenosvinos)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="//www.php.net/favicon.ico">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" rel="stylesheet">
<style>
body { padding-top: 70px; }
h2 {
padding-top: 100px;
margin-top: -100px;
display: inline-block; /* required for webkit browsers */
}
</style>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#navbar-opcache">
<a href="https://github.com/carlosbuenosvinos/opcache-dashboard"><img style="position: absolute; top: 50px; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
<nav id="navbar-opcache" class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Zend OPcache <?= $config['version']['version']?></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#hits">Hits</a></li>
<li><a href="#memory">Memory</a></li>
<li><a href="#keys">Keys</a></li>
<li><a href="#status">Status</a></li>
<li><a href="#configuration">Configuration</a></li>
<li><a href="#scripts">Scripts</a></li>
</ul>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>OPcache Dashboard</h1>
<h2>by Carlos Buenosvinos (<a href="https://twitter.com/buenosvinos">@buenosvinos</a>)</h2>
<p>PHP: <?= phpversion() ?> and OPcache: <?= $config['version']['version'] ?></p>
</div>
<?php
$stats = $status['opcache_statistics'];
$hitRate = round($stats['opcache_hit_rate'], 2);
?>
<h2 id="hits">Hits: <?= $hitRate ?>%</h2>
<div class="progress progress-striped">
<div class="progress-bar progress-bar-success" style="width: <?= $hitRate ?>%">
<span class="sr-only">Hits</span>
</div>
<div class="progress-bar progress-bar-danger" style="width: <?= (100 - $hitRate) ?>%">
<span class="sr-only">Misses</span>
</div>
</div>
<?php
$mem = $status['memory_usage'];
$totalMemory = $config['directives']['opcache.memory_consumption'];
$usedMemory = $mem['used_memory'];
$freeMemory = $mem['free_memory'];
$wastedMemory = $mem['wasted_memory'];
?>
<h2 id="memory">Memory: <?= size_for_humans($wastedMemory + $usedMemory) ?> of <?= size_for_humans($totalMemory) ?></h2>
<div class="progress progress-striped">
<div class="progress-bar progress-bar-danger" style="width: <?= round(($wastedMemory / $totalMemory) * 100, 0) ?>%">
<span class="sr-only">Wasted memory</span>
</div>
<div class="progress-bar progress-bar-warning" style="width: <?= round(($usedMemory / $totalMemory) * 100, 0) ?>%">
<span class="sr-only">Used memory</span>
</div>
<div class="progress-bar progress-bar-success" style="width: <?= round(($freeMemory / $totalMemory) * 100, 0) ?>%">
<span class="sr-only">Free memory</span>
</div>
</div>
<?php
$totalKeys = $stats['max_cached_keys'];
$usedKeys = $stats['num_cached_keys'];
$freeKeys = $totalKeys - $usedKeys;
?>
<h2 id="keys">Keys: <?= $usedKeys ?> of <?= $totalKeys ?></h2>
<div class="progress progress-striped">
<div class="progress-bar progress-bar-warning" style="width: <?= round(($usedKeys / $totalKeys) * 100, 0) ?>%">
<span class="sr-only">Used keys</span>
</div>
<div class="progress-bar progress-bar-success" style="width: <?= round(($freeKeys / $totalKeys) * 100, 0) ?>%">
<span class="sr-only">Free keys</span>
</div>
</div>
<h2 id="status">Status</h2>
<div class="table-responsive">
<table class="table table-striped table-hover">
<?php
foreach ($status as $key => $value) {
if ($key == 'scripts') {
continue;
}
if (is_array($value)) {
foreach ($value as $k => $v) {
$v = getStringFromPropertyAndValue($k, $v);
$m = getSuggestionMessage($k, $v);
?><tr class="<?= $m ? 'danger' : '' ?>"><th align="left"><?= $k ?></th><td align="right"><?= $v ?></td><td><?= $m ?></td></tr><?php
}
continue;
}
$mess = getSuggestionMessage($key, $value);
$value = getStringFromPropertyAndValue($key, $value);
?><tr class="<?= $mess ? 'danger' : '' ?>"><th align="left"><?= $key ?></th><td align="right"><?= $value ?></td><td><?= $mess ?></td></tr><?php
}
?>
</table>
</div>
<h2 id="configuration">Configuration</h2>
<div class="table-responsive">
<table class="table table-striped table-hover">
<?php foreach ($config['directives'] as $key => $value) {
$mess = getSuggestionMessage($key, $value);
?>
<tr class="<?= $mess ? 'danger' : '' ?>" >
<th align="left"><?= $key ?></th>
<td align="right"><?= getStringFromPropertyAndValue($key, $value) ?></td>
<td align="left"><?= $mess ?></td>
</tr>
<?php } ?>
</table>
</div>
<h2 id="scripts">Scripts (<?= count($status["scripts"]) ?>) <a type="button" class="btn btn-success" href="?reset">Reset all</a></h2>
<table class="table table-striped">
<tr>
<th>Options</th>
<th>Hits</th>
<th>Memory</th>
<th>Path</th>
</tr>
<?php
uasort($status['scripts'], function ($a, $b) { return $b['hits'] <=> $a ['hits']; });
$offset = null;
$previousKey = null;
foreach ($status['scripts'] as $key => $data) {
$offset = min(
getOffsetWhereStringsAreEqual(
(null === $previousKey) ? $key : $previousKey,
$key
),
(null === $offset) ? strlen($key) : $offset
);
$previousKey = $key;
}
foreach ($status['scripts'] as $key => $data) {
?>
<tr>
<td><a href="?invalidate=<?= $data['full_path'] ?>">Invalidate</a></td>
<td><?= $data['hits'] ?></td>
<td><?= size_for_humans($data['memory_consumption']) ?></td>
<td><?= substr($data['full_path'], $offset - 1) ?></td>
</tr>
<?php } ?>
</table>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
</body>
</html>