Skip to content

Instantly share code, notes, and snippets.

@antw
Last active October 5, 2015 20:24
Show Gist options
  • Save antw/8cc5e0ac2b8fe056116c to your computer and use it in GitHub Desktop.
Save antw/8cc5e0ac2b8fe056116c to your computer and use it in GitHub Desktop.
Moses Network Cache Benchmark
require 'benchmark/ips'
require 'memcached'
require 'redis'
require 'msgpack'
def curve
35040.times.map do |i|
rand(0.0...0.5)
end
end
def correct?(name, thing)
result = [thing.class, thing.length, thing[0].class, thing[0]]
expectation = [Array, 35040, Float, CURVE.first]
if result != expectation
fail <<-MSG.gsub(/^ /, '')
Invalid result for #{name}:
expected: #{expectation.inspect}"
got: #{result.inspect}"
MSG
end
end
memcached = Memcached.new("localhost:11211")
redis = Redis.new
CURVE = curve
redis.flushdb
memcached.flush
Dir.mkdir('tmp') unless Dir.exists?('tmp')
puts "Write Benchmarks"
puts "----------------"
puts
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
# Raw Data
x.report("redis") do
redis.rpush("Households", CURVE)
end
x.report("memcached") do
memcached.set("Households", CURVE)
end
x.report("file") do
File.write("tmp/Households.tmp", CURVE.to_s, mode: 'wb')
end
# MessagePack
x.report("redis+msg") do
redis.set("HouseholdsMsg", CURVE.to_msgpack)
end
x.report("memcached+msg") do
memcached.set("HouseholdsMsg", CURVE.to_msgpack)
end
x.report("file+msg") do
File.write("tmp/Households.msg.tmp", CURVE.to_msgpack, mode: 'wb')
end
x.compare!
end
correct?("redis", redis.lrange("Households", 0, 35039).map(&:to_f))
correct?("redis+msg", MessagePack.unpack(redis.get("HouseholdsMsg")))
correct?("memcached", memcached.get("Households"))
correct?("memcached+msg", MessagePack.unpack(memcached.get("HouseholdsMsg")))
correct?("file", eval(File.read("tmp/Households.tmp")))
correct?("file+msg", MessagePack.unpack(File.read("tmp/Households.msg.tmp")))
puts
puts "Read Benchmarks"
puts "---------------"
puts
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
# Raw Data
x.report("redis") do
redis.lrange("Households", 0, 35039).map(&:to_f)
end
x.report("memcached") do
memcached.get("Households")
end
x.report("file") do
eval(File.read("tmp/Households.tmp"))
end
# MessagePack
x.report("redis+msg") do
MessagePack.unpack(redis.get("HouseholdsMsg"))
end
x.report("memcached+msg") do
MessagePack.unpack(memcached.get("HouseholdsMsg"))
end
x.report("file+msg") do
MessagePack.unpack(File.read("tmp/Households.msg.tmp"))
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment