Find memcache request hit rate on linux command line

Memcached gives you stats like total hits so far and total miss. It can also tell you total uptime. This can be used to find total hit rate (request, hit and miss) since the start time of the memcached. Here are quick instruction to find memcache hit rate (since beginning or last 1 min) on Ubuntu Linux. I’m using memcached version 1.4.14 for the purpose of this article.

Total hit rate since beginning

To find total hit rate since beginning, save the following bash script to a file rate.sh and then run it.

#!/bin/bash
STATS=$(printf "stats\n" | nc localhost 11211)
gethits1=$(echo "$STATS" | grep "get_hits" | cut -d" " -f3 | tr -d '\n')
uptime1=$(echo "$STATS" | grep "uptime" | cut -d" " -f3)
hitratesincebeginning=$(echo "$gethits1 $uptime1" | awk '{printf "%.2f", ($1/$2)}')
echo "$hitratesincebeginning"

Here is how the output will look like:

0.26

Recent hit rate

To find the recent hit rate, we can use the same approach but we find the total hits for an interval of say 1 min. Then we can easily find the hit rate for that interval.

Here is the bash script we can use for this.

#!/bin/bash
STATS=$(printf "stats\n" | nc localhost 11211)
gethits1=$(echo "$STATS" | grep "get_hits" | cut -d" " -f3 | tr -d '\n')
uptime1=$(echo "$STATS" | grep "uptime" | cut -d" " -f3)
hitratesincebeginning=$(echo "$gethits1 $uptime1" | awk '{printf "%.2f", ($1/$2)}')
echo "$hitratesincebeginning"

sleep 60
STATS=$(printf "stats\n" | nc localhost 11211)
gethits2=$(echo "$STATS" | grep "get_hits" | cut -d" " -f3 | tr -d '\n')
uptime2=$(echo "$STATS" | grep "uptime" | cut -d" " -f3)
hitraterecent=$(echo "$gethits1 $uptime1 $gethits2 $uptime2" | awk '{printf "%.2f", (($3-$1)/($4-$2))}')
echo "$hitraterecent"

Here is how the output will look like:

0.26
2.00

This tells us that memcache has received avg 0.26 hit req/s since it was started and in last minute it received 2.00 hit req/s. The same approach can be used to find miss rate and overall requests (hit+miss) rate also.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments