Wednesday, November 23, 2011

Rails Benchmarking Reloaded

While evaluating caching strategies in Rails 3.1, I found existing articles comparing rails cache store backends to be quite lacking and/or outdated. The last article I could find compares file_store to mem_cache_store. Given that mem_cache_store is being replaced by "Dalli", it seems that existing benchmarks comparing the available options for rails cache backends are lacking in their ability to provide value with respect to the options available today.

  • File Store
  • Memcached Store
  • Dalli
  • Mongo Store
  • Redis Store
Test
require 'benchmark'
task :benchmark => :environment do
stores = {
:file_store=>[Rails.root+"/tmp/cache"],
:mem_cache_store=>["localhost"],
:dalli_store=>["localhost"],
:redis_store=> [],
:mongo_store=> [],
}
actions = {
:hit => lambda {
Rails.cache.fetch("test")
},
:miss => lambda {
Rails.cache.read("test"+rand().to_s[0..10])
}
}
stores.each do |store,args|
ActionController::Base.cache_store = store, *args
puts "Cache Store: #{store}"
marks = Benchmark.bm(15) do |x|
actions.each do |label, proc|
puts "Action: #{label}"
Rails.cache.delete("test")
Rails.cache.fetch("test") { [Time.now, 1.year.ago] }
x.report("times:") do
20000.times do
proc.call
end
end
end
end
end
end
view raw cachemark.rake hosted with ❤ by GitHub

Results


Though it looks like mongo-store demonstrates the best overall performance, it should be noted that a mongo server is unlikely to be used solely for caching (the same applies to redis), it is likely that non-caching related queries will be running concurrently on a mongo/redis server which could affect the suitability of these benchkmarks.

1 comment:

Kamil said...

Nice work! I didn't know mongo store before, I definitely give mongo a chance since is already use as main database