Skip to content

Instantly share code, notes, and snippets.

@adamzero1
Last active September 15, 2021 15:23
Show Gist options
  • Save adamzero1/bbf2b4b7cbce6a063be619e1d84cc940 to your computer and use it in GitHub Desktop.
Save adamzero1/bbf2b4b7cbce6a063be619e1d84cc940 to your computer and use it in GitHub Desktop.
Magento 2 Redis Key Sizes
<?php
require realpath(__DIR__) . '/app/bootstrap.php';
class RedisCustom extends Credis_Client {
private static function _prepare_command($args)
{
return sprintf('*%d%s%s%s', count($args), CRLF, implode(array_map(array('self', '_map'), $args), CRLF), CRLF);
}
private static function _map($arg)
{
return sprintf('$%d%s%s', strlen($arg), CRLF, $arg);
}
public function memoryUsage($key)
{
$command = self::_prepare_command(['MEMORY', 'USAGE', $key]);
$this->write_command($command);
$response = $this->read_reply('');
return (int)$response;
}
public function disableStandalone()
{
$this->standalone = false;
}
}
function toGb($bytes)
{
return sprintf('%.4f', $bytes / 1024 / 1024 / 1024).'GB';
}
$envConfig = include('app/etc/env.php');
if(!isset($envConfig['cache'])
|| !isset($envConfig['cache']['frontend'])
){
die('unable to find "cache" configuration'.PHP_EOL);
}
$caches = [];
foreach([
'default',
'page_cache'
] as $cacheType){
if(!isset($envConfig['cache']['frontend'][$cacheType])){
echo 'WARNING: unable to find frontend.'.$cacheType.' configuration'.PHP_EOL;
}else{
$defaultCache = $envConfig['cache']['frontend'][$cacheType];
if(!isset($defaultCache['backend'])){
echo 'WARNING: unable to find frontend.default.'.$cacheType.' configuration'.PHP_EOL;
}elseif ($defaultCache['backend'] != 'Cm_Cache_Backend_Redis'){
echo 'WARNING: unknown frontend.'.$cacheType.'.backend type, looking for "Cm_Cache_Backend_Redis"'.PHP_EOL;
}elseif (!isset($defaultCache['backend_options'])
|| !isset($defaultCache['backend_options']['server'])
|| !isset($defaultCache['backend_options']['database'])
){
echo 'WARNING: frontend.'.$cacheType.'.backend type configuration not correct'.PHP_EOL;
}else{
$caches[$cacheType] = $defaultCache;
}
}
}
echo '=================================================='.PHP_EOL;
echo ' STATS'.PHP_EOL;
echo '=================================================='.PHP_EOL;
foreach($caches as $cacheType => $cache){
echo ' '.strtoupper($cacheType).PHP_EOL;
echo '------------------------------------------------'.PHP_EOL;
$redis = new RedisCustom(
$cache['backend_options']['server'],
isset($cache['backend_options']['port'])? $cache['backend_options']['port'] : 6379,
300,
'',
$cache['backend_options']['database'],
isset($cache['backend_options']['password'])? $cache['backend_options']['password'] : null
);
$index = [];
$totalSize = 0;
$grouped = [];
$redis->connect();
$keys = $redis->keys('*');
echo 'Total keys: '.count($keys).PHP_EOL;
foreach($keys as $key){
// aws doesn't seem to support this
// $index[$key] = $redis->memoryUsage($key);
// echo 'memory usage: '.$index[$key];
$keyByteSize = mb_strlen($key);
$valueType = $redis->type($key);
switch($valueType){
case 'string':
$value = $redis->get($key);
break;
case 'hash':
$value = serialize($redis->hGetAll($key));
break;
case 'set':
$value = serialize($redis->sMembers($key));
break;
case 'none':
// key no longer exists
continue 2;
default:
throw new \Exception('Unexpected type: '.$valueType);
}
$valueByteSize = mb_strlen($value);
// echo 'key: '.$keyByteSize.PHP_EOL;
// echo 'value: '.$valueByteSize.PHP_EOL;
// echo 'total: '.($keyByteSize + $valueByteSize).PHP_EOL;
$index[$key] = ($keyByteSize + $valueByteSize);
$totalSize += $index[$key];
$parts = explode('_', $key);
$group = [];
foreach($parts as $part){
$group[] = $part;
$groupKey = implode('_', $group);
if(!isset($grouped[$groupKey])){
$grouped[$groupKey] = [
'size' => 0,
'keys' => 0,
];
}
$grouped[$groupKey]['size'] += $index[$key];
$grouped[$groupKey]['keys']++;
}
}
arsort($index);
uasort($grouped, function($a, $b){
return ($a['size'] <=> $b['size']) * -1;
});
echo 'Total Size: '.toGb($totalSize).PHP_EOL.PHP_EOL;
echo '10 Biggest Keys'.PHP_EOL;
$x=0;
foreach($index as $key => $size){
echo $key.' '.toGb($size).PHP_EOL;
$x++;
if($x >= 10){
break;
}
}
echo PHP_EOL.'10 Biggest Groups'.PHP_EOL;
$x=0;
foreach($grouped as $key => $info){
echo $key.' size: '.toGb($info['size']).', key count: '.$info['keys'].PHP_EOL;
$x++;
if($x >= 10){
break;
}
}
echo PHP_EOL.PHP_EOL.PHP_EOL;
$redis->close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment