php 缓存使用监控测试代码。
将以下代码存在PHP WEB网站下面,然后通过IE浏览器进行访问。
#cat ocpcache.php
/*
* 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);
} else if ($bytes > 1024) {
return sprintf("%.2f kB", $bytes/1024);
} else return sprintf("%d bytes", $bytes);
}
?>
body{
font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;
margin:auto;
position:relative;
width:1024px;
}
text{
font:10px sans-serif;
}
form{
position: absolute;
right:210px;
top:50px;
}
#graph{
position: absolute;
right:0px;
top:80px;
}
#stats{
position: absolute;
right:234px;
top:240px;
}
tbody tr:nth-child(even) {
background-color:#eee;
}
p.capitalize{
text-transform:capitalize;
}
.tabs{
position:relative;
min-height:200px;
clear:both;
margin:25px 0;
}
.tab{
float:left;
}
.tab label{
background: #eee;
padding:10px;
border:1px solid #ccc;
margin-left:-1px;
position:relative;
left:1px;
}
.tab [type=radio]{
display: none;
}
.content{
position:absolute;
top:28px;
left: 0;
background:white;
padding:20px;
border:1px solid #ccc;
height:500px;
width:480px;
overflow-y:auto;
overflow-x:hidden;
}
.content table {
width:100%;
}
.clickable {
cursor: hand;
cursor: pointer;
}
[type=radio]:checked ~ label{
background: white;
border-bottom:1px solid white;
z-index:2;
}
[type=radio]:checked ~ label ~ .content{
z-index: 1;
}
var hidden = {};
function toggleVisible(head, row) {
if (!hidden[row]) {
d3.selectAll(row)
.transition().style('display', 'none');
hidden[row] = true;
d3.select(head).transition().style('color', '#ccc');
} else {
d3.selectAll(row)
.transition().style('display');
hidden[row] = false;
d3.select(head).transition().style('color', '#000');
}
}
PHP = phpversion()?> OpCache = $config['version']['version']?>
Memory
Keys
Hits
Status
foreach($status as $key=>$value) {
if($key=='scripts') continue;
if(is_array($value)) {
foreach($value as $k=>$v) {
if($v===false) $value = "false";
if($v===true) $value = "true";
if($k=='used_memory' || $k=='free_memory' || $k == 'wasted_memory') $v = size_for_humans($v);
if($k=='current_wasted_percentage' || $k=='opcache_hit_rate') $v = number_format($v,2).'%';
if($k=='blacklist_miss_ratio') $v = number_format($v,2);
echo "
$k$v\n";}
continue;
}
if($value===false) $value = "false";
if($value===true) $value = "true";
echo "
$key$value\n";}
?>
Configuration
foreach($config['directives'] as $key=>$value) {
if($value===false) $value = "false";
if($value===true) $value = "true";
if($key == 'opcache.memory_consumption') $value = size_for_humans($value);
echo "
$key$value\n";}
?>
Scripts (=count($status["scripts"]); ?>)
Hits | Memory | Path |
---|
foreach($status['scripts'] as $key=>$data) {
$dirs[dirname($key)][basename($key)]=$data;
}
asort($dirs);
$id = 1;
foreach($dirs as $dir => $files) {
$count = count($files);
if ($count > 1) {
echo "
";echo "
{$dir} ({$count} files)";echo "
";}
foreach ($files as $file => $data) {
echo "
";echo "
{$data["hits"]}";echo "
" .size_for_humans($data["memory_consumption"]). "";if ($count > 1) {
echo "
{$file}";} else echo "
{$dir}/{$file}";echo "
";}
++$id;
}
?>
$mem = $status['memory_usage'];
$stats = $status['opcache_statistics'];
$free_keys = $stats['max_cached_keys'] - $stats['num_cached_keys'];
echo <<
var dataset = {
memory: [{$mem['used_memory']},{$mem['free_memory']},{$mem['wasted_memory']}],
keys: [{$stats['num_cached_keys']},{$free_keys},0],
hits: [{$stats['hits']},{$stats['misses']},0]
};
EOB;
?>
var width = 600,
height = 400,
radius = Math.min(width, height) / 2;
var color = d3.scale.category20();
var pie = d3.layout.pie()
.sort(null);
var arc = d3.svg.arc()
.innerRadius(radius - 20)
.outerRadius(radius - 50);
var svg = d3.select("#graph").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var path = svg.selectAll("path")
.data(pie(dataset.memory))
.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc)
.each(function(d) { this._current = d; }); // store the initial values
d3.selectAll("input").on("change", change);
set_text("memory");
function set_text(t) {
if(t=="memory") {
d3.select("#stats").html(
"
Used | <?php echo size_for_humans($mem['used_memory'])?> |
---|
"
Free<?php echo size_for_humans($mem['free_memory'])?>"+"
Wasted<?php echo size_for_humans($mem['wasted_memory'])?>"+"
<?php echo number_format($mem['current_wasted_percentage'],2)?>%");
} else if(t=="keys") {
d3.select("#stats").html(
"
Cached keys | "+dataset[t][0]+" |
---|
"
Free Keys"+dataset[t][1]+"");
} else if(t=="hits") {
d3.select("#stats").html(
"
Cache Hits | "+dataset[t][0]+" |
---|
"
Misses"+dataset[t][1]+"");
}
}
function change() {
path = path.data(pie(dataset[this.value])); // update the data
path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
set_text(this.value);
}
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}