stats.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. $server = 'tcp://127.0.0.1:6379';
  3. $db = 0;
  4. $limit = 20;
  5. array_shift($argv);
  6. while($arg = array_shift($argv)) {
  7. switch($arg) {
  8. case '--db': $db = intval(array_shift($argv)); break;
  9. case '--server': $server = array_shift($argv); break;
  10. case '--limit': $limit = array_shift($argv); break;
  11. default: die("Unrecognized argument '$arg'.\nUsage: path/to/stats.php [--server tcp://127.0.0.1:6378] [--db 0] [--limit 20]\n");
  12. }
  13. }
  14. require __DIR__.'/lib/Credis/Client.php';
  15. require './lib/Zend/Cache/Backend/Interface.php';
  16. require './lib/Zend/Cache/Backend/ExtendedInterface.php';
  17. require './lib/Zend/Cache/Backend.php';
  18. require __DIR__.'/Cm/Cache/Backend/Redis.php';
  19. $client = new Credis_Client($server);
  20. $client->select($db);
  21. $tagStats = array();
  22. foreach($client->sMembers(Cm_Cache_Backend_Redis::SET_TAGS) as $tag) {
  23. if (preg_match('/^\w{3}_MAGE$/', $tag)) continue;
  24. $ids = $client->sMembers(Cm_Cache_Backend_Redis::PREFIX_TAG_IDS . $tag);
  25. $tagSizes = array();
  26. $missing = 0;
  27. foreach ($ids as $id) {
  28. $data = $client->hGet(Cm_Cache_Backend_Redis::PREFIX_KEY.$id, Cm_Cache_Backend_Redis::FIELD_DATA);
  29. $size = strlen($data);
  30. if ($size) $tagSizes[] = $size;
  31. else $missing++;
  32. }
  33. if ($tagSizes) {
  34. $tagStats[$tag] = array(
  35. 'count' => count($tagSizes),
  36. 'min' => min($tagSizes),
  37. 'max' => max($tagSizes),
  38. 'avg size' => array_sum($tagSizes) / count($tagSizes),
  39. 'total size' => array_sum($tagSizes),
  40. 'missing' => $missing,
  41. );
  42. }
  43. }
  44. function _format_bytes($a_bytes)
  45. {
  46. if ($a_bytes < 1024) {
  47. return $a_bytes .' B';
  48. } elseif ($a_bytes < 1048576) {
  49. return round($a_bytes / 1024, 4) .' KB';
  50. } else {
  51. return round($a_bytes / 1048576, 4) . ' MB';
  52. }
  53. }
  54. function printStats($data, $key, $limit) {
  55. echo "Top $limit tags by ".ucwords($key)."\n";
  56. echo "------------------------------------------------------------------------------------\n";
  57. $sort = array();
  58. foreach ($data as $tag => $stats) {
  59. $sort[$tag] = $stats[$key];
  60. }
  61. array_multisort($sort, SORT_DESC, $data);
  62. $i = 0;
  63. $fmt = "%-40s| %-8s| %-15s| %-15s\n";
  64. printf($fmt, 'Tag', 'Count', 'Avg Size', 'Total Size');
  65. foreach ($data as $tag => $stats) {
  66. $tag = substr($tag, 4);
  67. if (++$i > $limit) break;
  68. $avg = _format_bytes($stats['avg size']);
  69. $total = _format_bytes($stats['total size']);
  70. printf($fmt, $tag, $stats['count'], $avg, $total);
  71. }
  72. echo "\n";
  73. }
  74. // Top 20 by total size
  75. printStats($tagStats, 'total size', $limit, true);
  76. // Top 20 by average size
  77. printStats($tagStats, 'avg size', $limit, true);
  78. // Top 20 by count
  79. printStats($tagStats, 'count', $limit, true);