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);

}

?>

 

PHP OpCache

 

   

   

   

 

 

 

 


   

     

     

     

     

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 "

\n";

    }

    continue;

  }

  if($value===false) $value = "false";

  if($value===true) $value = "true";

  echo "

\n";

}

?>

     

$k$v
$key$value

     

   


   

     

     

     

     

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 "

\n";

}

?>

     

$key$value

     

   


   

     

     

     

     

     

       

       

       

     

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 "

";

        echo "

";    

    }

    

    foreach ($files as $file => $data) {

        echo "

";

        echo "

";

        echo "

";

        

        if ($count > 1) {

            echo "

";

        } else echo "

";       

        echo "

";

    }    

    ++$id;

}

?>

     

Hits Memory Path
{$dir} ({$count} files)
{$data["hits"]} " .size_for_humans($data["memory_consumption"]). " {$file} {$dir}/{$file}

     

   


 


 

 

$mem = $status['memory_usage'];

$stats = $status['opcache_statistics'];

$free_keys = $stats['max_cached_keys'] - $stats['num_cached_keys'];

echo <<