InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Find memcache request hit rate on linux command line

By admin | Last updated on Mar 20, 2016

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.

Suggested posts:

  1. How to use pm2 to manage node.js application in production
  2. How to kill unresponsive ssh session using escape sequence
  3. Linux replace comma with newline
  4. PHP beginning and end of string regex examples
  5. PHP apc – setup and performance benchmarks on Ubuntu Linux
  6. How to show environment variable for a process id (pid)
  7. Embed youtube video with javascript on-click lazy loading approach
  8. AngularJS animattion using ng-class
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Memcache, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress