Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redis simple key stats (total size, average size, count keys)
#!/usr/bin/env bash
human_size() {
awk -v sum="$1" 'BEGIN {
hum[1024^3]="Gb";
hum[1024^2]="Mb";
hum[1024]="Kb";
for (x=1024^3; x>=1024; x/=1024) {
if (sum>=x) {
printf "%.2f %s\n",sum/x,hum[x];
break;
}
}
if (sum<1024)
printf "%d bytes",sum;
}'
}
redis_cmd='redis-cli'
for k in `$redis_cmd keys "*"`;
do
key_size_bytes=`$redis_cmd debug object $k | perl -wpe 's/^.+serializedlength:([\d]+).+$/$1/g'`;
size_key_list="$size_key_list$key_size_bytes-$k\n";
done
count=0;
total=0;
for l in `echo -e "$size_key_list"` ; do
if [[ -n "$l" ]]; then
count=$(echo $count+1 | bc)
size=`echo $l | perl -wpe 's/^(\d+).+/$1/g'`;
hsize=`human_size "$size"`;
key=`echo $l | perl -wpe 's/^\d+(.+)/$1/g' | tr -d "-"`;
total=$(echo $total+$size | bc)
#printf "%-10s%s\n" "$hsize" "$key";
fi
done
printf "\nRedisDB Simple Stats\n\n"
printf "%-20s%s\n" "total keys:" "$count"
htotal=`human_size "$total"`
printf "%-20s%s\n" "total size:" "$htotal"
avg=$(echo "scale=2; $total / $count" | bc)
havg=`human_size "$avg"`
printf "%-20s%s\n\n" "avg size:" "$havg";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.