Skip to content

Instantly share code, notes, and snippets.

@machu
Created October 2, 2009 00:03
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 machu/199335 to your computer and use it in GitHub Desktop.
Save machu/199335 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
require 'memcache'
require 'pstore'
def benchmark(cache)
count = 10000
key_size = 1000
Benchmark.bm do |bm|
bm.report("#{cache.class.to_s}#put") do
count.times do |i|
key = (i % key_size).to_s
cache.put(key, key * 100)
end
end
bm.report("#{cache.class.to_s}#get") do
count.times do |i|
key = (i % key_size).to_s
cache.get(i.to_s)
end
end
end
end
class MemCacheAdapter
def initialize
@cache = MemCache.new 'localhost:11211'
end
def get(key)
@cache[key]
end
def put(key, value)
@cache[key] = value
end
end
class PStoreAdapter
def initialize(dbfile)
@cache= PStore.new(dbfile)
end
def get(key)
@cache.transaction(true) do |cache|
cache[key]
end
end
def put(key, value)
@cache.transaction do |cache|
cache[key] = value
end
end
end
class PStoreAdapter2
def initialize(dbfile)
@dbfile = dbfile
end
def cache(key)
PStore.new("#{@dbfile}_#{key}.pstore")
end
def get(key)
cache(key).transaction(true) do |cache|
cache[key]
end
end
def put(key, value)
cache(key).transaction do |cache|
cache[key] = value
end
end
end
[PStoreAdapter.new('pstore1/bench_pstore'),
PStoreAdapter2.new('pstore2/bench_pstore'),
MemCacheAdapter.new
].each {|cache| benchmark(cache) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment