设为首页 收藏本站
查看: 539|回复: 0

[经验分享] php 缓存使用监控测试代码

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-9 09:01:40 | 显示全部楼层 |阅读模式
php 缓存使用监控测试代码。
将以下代码存在PHP WEB网站下面,然后通过IE浏览器进行访问。
#cat ocpcache.php
<?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&nbsp;MB", $bytes/1048576);
    } else if ($bytes > 1024) {
        return sprintf("%.2f&nbsp;kB", $bytes/1024);
    } else return sprintf("%d&nbsp;bytes", $bytes);
}
?>
<!DOCTYPE html>
<meta charset="utf-8">
<html><head>
<style>
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;
}
</style>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.0.1/d3.v3.min.js"></script>
<script language="javascript">
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');
    }
}
</script>
</head>
<body>
  <h1>PHP <?= phpversion()?> OpCache <?= $config['version']['version']?></h1>
  <form>
    <label><input type="radio" name="dataset" value="memory" checked> Memory</label>
    <label><input type="radio" name="dataset" value="keys"> Keys</label>
    <label><input type="radio" name="dataset" value="hits"> Hits</label>
  </form>
  <div id="stats">
  </div>
  <div class="tabs">

    <div class="tab">
      <input type="radio" id="tab-status" name="tab-group-1" checked>
      <label for="tab-status">Status</label>
      <div class="content">
      <table>
<?php
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 "<tr><th align=\"left\">$k</th><td align=\"right\">$v</td></tr>\n";
    }
    continue;
  }
  if($value===false) $value = "false";
  if($value===true) $value = "true";
  echo "<tr><th align=\"left\">$key</th><td align=\"right\">$value</td></tr>\n";
}
?>
      </table>
      </div>
    </div>

    <div class="tab">
      <input type="radio" id="tab-config" name="tab-group-1">
      <label for="tab-config">Configuration</label>
      <div class="content">
      <table>
<?php
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 "<tr><th align=\"left\">$key</th><td align=\"right\">$value</td></tr>\n";
}
?>
      </table>
      </div>
    </div>

    <div class="tab">
      <input type="radio" id="tab-scripts" name="tab-group-1">
      <label for="tab-scripts">Scripts (<?=count($status["scripts"]); ?>)</label>
      <div class="content">
      <table style="font-size:0.8em;">
      <tr>
        <th width="10%">Hits</th>
        <th width="20%">Memory</th>
        <th width="70%">Path</th>
      </tr>
<?php
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 "<tr>";
        echo "<th class=\"clickable\" id=\"head-{$id}\" colspan=\"3\"  '#row-{$id}')\">{$dir} ({$count} files)</th>";
        echo "</tr>";   
    }

    foreach ($files as $file => $data) {
        echo "<tr id=\"row-{$id}\">";
        echo "<td>{$data["hits"]}</td>";
        echo "<td>" .size_for_humans($data["memory_consumption"]). "</td>";

        if ($count > 1) {
            echo "<td>{$file}</td>";
        } else echo "<td>{$dir}/{$file}</td>";      
        echo "</tr>";
    }   
    ++$id;
}
?>
      </table>
      </div>
    </div>

  </div>

  <div id="graph">
  </div>
<?php
$mem = $status['memory_usage'];
$stats = $status['opcache_statistics'];
$free_keys = $stats['max_cached_keys'] - $stats['num_cached_keys'];
echo <<<EOB
<script>
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(
      "<table><tr><th style='background:#1f77b4;' align=right>Used</th><td align=right><?php echo size_for_humans($mem['used_memory'])?></td></tr>"+
      "<tr><th style='background:#aec7e8;' align=right>Free</th><td align=right><?php echo size_for_humans($mem['free_memory'])?></td></tr>"+
      "<tr><th style='background:#ff7f0e;' align=right>Wasted</th><td align=right><?php echo size_for_humans($mem['wasted_memory'])?></td></tr>"+
      "<tr><th style='background:#ff7f0e;'> </th><td align=right><?php echo number_format($mem['current_wasted_percentage'],2)?>%</td></tr></table>"
    );
  } else if(t=="keys") {
    d3.select("#stats").html(
      "<table><tr><th style='background:#1f77b4;'>Cached keys</th><td align=right>"+dataset[t][0]+"</td></tr>"+
      "<tr><th style='background:#aec7e8;'>Free Keys</th><td align=right>"+dataset[t][1]+"</td></tr></table>"
    );
  } else if(t=="hits") {
    d3.select("#stats").html(
      "<table><tr><th style='background:#1f77b4;' align=right>Cache Hits</th><td align=right>"+dataset[t][0]+"</td></tr>"+
      "<tr><th style='background:#aec7e8;' align=right>Misses</th><td align=right>"+dataset[t][1]+"</td></tr></table>"
    );
  }
}
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));
  };
}
</script>
</body>
</html>  



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-37636-1-1.html 上篇帖子: PHP连接Mysql的乱码问题 下篇帖子: 解决跨浏览器下PHP下载文件名中的中文乱码问题 监控
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表