forked from ifuelinteractive/Cm_Cache_Backend_Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.php
88 lines (79 loc) · 2.6 KB
/
stats.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
<?php
$server = 'tcp://127.0.0.1:6379';
$db = 0;
$limit = 20;
array_shift($argv);
while($arg = array_shift($argv)) {
switch($arg) {
case '--db': $db = intval(array_shift($argv)); break;
case '--server': $server = array_shift($argv); break;
case '--limit': $limit = array_shift($argv); break;
default: die("Unrecognized argument '$arg'.\nUsage: path/to/stats.php [--server tcp://127.0.0.1:6378] [--db 0] [--limit 20]\n");
}
}
require __DIR__.'/lib/Credis/Client.php';
require './lib/Zend/Cache/Backend/Interface.php';
require './lib/Zend/Cache/Backend/ExtendedInterface.php';
require './lib/Zend/Cache/Backend.php';
require __DIR__.'/Cm/Cache/Backend/Redis.php';
$client = new Credis_Client($server);
$client->select($db);
$tagStats = array();
foreach($client->sMembers(Cm_Cache_Backend_Redis::SET_TAGS) as $tag) {
if (preg_match('/^\w{3}_MAGE$/', $tag)) continue;
$ids = $client->sMembers(Cm_Cache_Backend_Redis::PREFIX_TAG_IDS . $tag);
$tagSizes = array();
$missing = 0;
foreach ($ids as $id) {
$data = $client->hGet(Cm_Cache_Backend_Redis::PREFIX_KEY.$id, Cm_Cache_Backend_Redis::FIELD_DATA);
$size = strlen($data);
if ($size) $tagSizes[] = $size;
else $missing++;
}
if ($tagSizes) {
$tagStats[$tag] = array(
'count' => count($tagSizes),
'min' => min($tagSizes),
'max' => max($tagSizes),
'avg size' => array_sum($tagSizes) / count($tagSizes),
'total size' => array_sum($tagSizes),
'missing' => $missing,
);
}
}
function _format_bytes($a_bytes)
{
if ($a_bytes < 1024) {
return $a_bytes .' B';
} elseif ($a_bytes < 1048576) {
return round($a_bytes / 1024, 4) .' KB';
} else {
return round($a_bytes / 1048576, 4) . ' MB';
}
}
function printStats($data, $key, $limit) {
echo "Top $limit tags by ".ucwords($key)."\n";
echo "------------------------------------------------------------------------------------\n";
$sort = array();
foreach ($data as $tag => $stats) {
$sort[$tag] = $stats[$key];
}
array_multisort($sort, SORT_DESC, $data);
$i = 0;
$fmt = "%-40s| %-8s| %-15s| %-15s\n";
printf($fmt, 'Tag', 'Count', 'Avg Size', 'Total Size');
foreach ($data as $tag => $stats) {
$tag = substr($tag, 4);
if (++$i > $limit) break;
$avg = _format_bytes($stats['avg size']);
$total = _format_bytes($stats['total size']);
printf($fmt, $tag, $stats['count'], $avg, $total);
}
echo "\n";
}
// Top 20 by total size
printStats($tagStats, 'total size', $limit, true);
// Top 20 by average size
printStats($tagStats, 'avg size', $limit, true);
// Top 20 by count
printStats($tagStats, 'count', $limit, true);