Skip to content

Instantly share code, notes, and snippets.

@katsuma
Created June 12, 2010 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsuma/436046 to your computer and use it in GitHub Desktop.
Save katsuma/436046 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
require 'memcache'
require 'tokyocabinet'
require 'tokyotyrant'
require 'redis'
include TokyoCabinet
include TokyoTyrant
class BenchKvs
attr_accessor :use_memcache_client
attr_accessor :mc, :tc, :tt, :rd
BENCH_TIMES = 1000
def initialize(use_memcache_client = false)
@use_memcache_client = use_memcache_client
@mc = MemCache::new('localhost:11211')
unless @use_memcache_client
@tc = HDB::new
@tc.open("tc.hdb", HDB::OWRITER | HDB::OCREAT)
@tt = RDB::new
@tt.open("localhost", 1978)
@rd = Redis.new
else
@tt = MemCache::new("localhost:1978")
@rd = MemCache::new('localhost:6379')
end
end
def start
Benchmark.bm do |b|
if @mc
b.report("memcache:set") {
1.upto BENCH_TIMES do |i|
@mc["foo_#{i}"] = "bar_#{i}"
end
}
b.report("memcache:get") {
1.upto BENCH_TIMES do |i|
@mc["foo_#{i}"]
end
}
end
if @tc
b.report("TokyoCabinet:set") {
1.upto BENCH_TIMES do |i|
@tc["foo_#{i}"] = "bar_#{i}"
end
}
b.report("TokyoCabinet:get") {
1.upto BENCH_TIMES do |i|
@tc["foo_#{i}"]
end
}
end
if @tt
b.report("TokyoTyrant:set") {
1.upto BENCH_TIMES do |i|
@tt["foo_#{i}"] = "bar_#{i}"
end
}
b.report("TokyoTyrant:get") {
1.upto BENCH_TIMES do |i|
@tt["foo_#{i}"]
end
}
end
if @rd
b.report("Redis:set") {
1.upto BENCH_TIMES do |i|
@rd.set "foo_#{i}", "bar_#{i}"
end
}
unless @use_memcache_client # not support redis get by memcache-protocol
b.report("Redis:get") {
1.upto BENCH_TIMES do |i|
@rd.get "foo_#{i}"
end
}
end
end
end
end
end
(BenchKvs.new(false)).start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment