Skip to content

Instantly share code, notes, and snippets.

@arp
Created August 28, 2012 00:57
Show Gist options
  • Save arp/3493985 to your computer and use it in GitHub Desktop.
Save arp/3493985 to your computer and use it in GitHub Desktop.
Memcached flood
#!/usr/bin/env ruby
#
# A simple tool for testing Memcached's evictions functionality.
#
# It saves TOTAL_BYTES of data in chunks of CHUNK_BYTES size.
# Well-behaving Memcached server should start evicting old data
# unless it's configured to return error on memory exhaustion.
#
require 'rubygems'
require 'memcached'
TOTAL_BYTES = 10 * 1024 * 1024 * 1024 # 10GB
CHUNK_BYTES = 32768 # 32KB
TTL = 24 * 3600 # 24 hours
host = ARGV.first or fail "Please specify the host:port you want to test"
puts "Saving #{TOTAL_BYTES} bytes of data to #{host}..."
m = Memcached.new host, :exceptions_to_retry => [],
:show_backtraces => true,
:exception_retry_limit => 1
nchunks = TOTAL_BYTES / CHUNK_BYTES
value = 'x' * CHUNK_BYTES
wipestr = ([8]*4).pack('c*')
nchunks.times do |i|
m.set "memflood:#{i.to_s}", value, TTL, false
print wipestr + (100 * i.to_f / nchunks).round.to_s + '%'
end
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment