Skip to content

Instantly share code, notes, and snippets.

@sah
Forked from defunkt/memrate.rb
Created April 29, 2010 04:47
Show Gist options
  • Save sah/383155 to your computer and use it in GitHub Desktop.
Save sah/383155 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
## Usage: memrate.rb [-l <lib>] [-i <num>] [-t <threshold>] [-k <size>] [<host>]
## Measure memcached server read time.
##
## Options
## -l, --library=<lib> Memcached client library to use. memcache-client by
## default. memcached also supported.
## -i, --iterations=<num> Number of times to read the value. If not set, read
## until interrupted.
## -t, --threshold=<time> Report iterations that take longer than <time>
## milliseconds to complete. Default: 20ms
## -k, --size=<value> Size of value to read in kilobytes. Default: 1
##
## When no <host> is specified, localhost:11211 is assumed.
# Parse options
require 'optparse'
library = 'memcache-client'
iterations = 0
threshold = 20
size = 1
ARGV.options do |o|
o.on('-l', '--library=LIB') { |v| library = v }
o.on('-i', '--iterations=NUM') { |v| iterations = v }
o.on('-t', '--threshold=VALUE') { |v| threshold = v.to_i }
o.on('-k', '--size=SIZE') { |v| size = v.to_i }
o.on('-h', '--help') { exec "grep ^## < #$0 | cut -c4-" }
o.parse!
end
server = ARGV[0] || 'localhost:11211'
# Choose your memcache client library
if library == 'memcache-client'
require 'memcache'
client = MemCache.new(server)
elsif library == 'memcached'
require 'memcached'
client = Memcached.new(server)
else
fail "invalid memcache client library: #{library.inspect}"
end
# Dump config to stderr
$stderr.puts "server: #{server}"
$stderr.puts "library: #{library}"
$stderr.puts "threshold: #{threshold}ms"
$stderr.puts "value size: #{size}KB"
# Genrate a key and value based on the size
key = "memrate:#{Time.now.to_f}"
value = ('0' * 1024) * size
client.set(key, value)
# Repeatedly read value from server and measure read time against
# threshold.
iteration = 0
over_threshold = 0
bailout = false
trap('INT') { puts; bailout = true }
while iterations == 0 || iteration < iterations
iteration += 1
start = Time.now
client.get(key)
time = (Time.now - start) * 1000
if time > threshold
over_threshold += 1
printf "%07d %f\n", iteration, time
end
break if bailout
end
# Summarize
printf "total requests: %d\n", iteration
printf "over threshold: %d (%f%%)\n",
over_threshold,
(over_threshold / iteration.to_f) * 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment