Skip to content

Instantly share code, notes, and snippets.

@nateberkopec
Created July 14, 2015 18:22
Show Gist options
  • Save nateberkopec/14d6a2fb7fe5da06a1f6 to your computer and use it in GitHub Desktop.
Save nateberkopec/14d6a2fb7fe5da06a1f6 to your computer and use it in GitHub Desktop.
Cache Benchmark
require 'active_support'
require 'benchmark/ips'
require 'lru_redux'
require 'dalli'
require 'kgio'
require 'active_support/cache/dalli_store'
require 'active_support/cache/redis_store'
require 'fileutils'
FileUtils.rm_rf('tmp/cache')
FileUtils.mkdir('tmp/cache')
KEY_SIZE = ENV['KEY_SIZE'] ? ENV['KEY_SIZE'].to_i : 2_000
Benchmark.ips do |x|
file_store = ActiveSupport::Cache::FileStore.new("tmp/cache")
x.report(file_store.class) { file_store.fetch(rand(KEY_SIZE)) { :value } }
memory_store = ActiveSupport::Cache::MemoryStore.new
x.report(memory_store.class) { memory_store.fetch(rand(KEY_SIZE)) { :value } }
lru_redux = LruRedux::ThreadSafeCache.new(1_000)
x.report(lru_redux.class) { lru_redux.getset(rand(KEY_SIZE)) { :value } }
dc = ActiveSupport::Cache::DalliStore.new('localhost:11211')
dc.clear
begin
dc.dalli.alive!
x.report(dc.class) { dc.fetch(rand(KEY_SIZE)) { :value } }
rescue Dalli::RingError
puts "Not running Memcache bench - you must have a Memcache server available"
end
begin
redis = ActiveSupport::Cache::RedisStore.new('localhost:6379')
redis.clear
x.report(redis.class) { redis.fetch(rand(KEY_SIZE)) { :value } }
rescue Redis::CannotConnectError
puts "Not running Redis bench - you must have a Redis server available"
end
if ENV['MEMCACHE_SERVERS']
dc_networked = ActiveSupport::Cache::DalliStore.new(ENV['MEMCACHE_SERVERS'])
dc_networked.clear
x.report("#{dc_networked.class} at #{ENV['MEMCACHE_SERVERS']}") do
dc_networked.fetch(rand(KEY_SIZE)) { :value }
end
end
if ENV['REDIS_SERVER']
redis_networked = ActiveSupport::Cache::RedisStore.new(host: ENV["REDIS_SERVER"], port: 11469)
redis_networked.clear
x.report("#{redis_networked.class} at #{ENV['REDIS_SERVER']}") do
redis_networked.fetch(rand(KEY_SIZE)) { :value }
end
end
x.compare!
end
FileUtils.rm_rf('tmp/cache')
source "https://rubygems.org"
gem "rails", "~> 4.2"
gem "benchmark-ips"
gem "lru_redux"
gem "dalli"
gem "kgio"
gem "redis-activesupport"
katana:rails_cache_bench nateberkopec$ MEMCACHE_SERVERS=pub-memcache-13640.us-east-1-1.2.ec2.garantiadata.com:13640 REDIS_SERVER=pub-redis-11469.us-east-1-4.2.ec2.garantiadata.com ruby bench.rb
Calculating -------------------------------------
ActiveSupport::Cache::FileStore
222.000 i/100ms
ActiveSupport::Cache::MemoryStore
3.375k i/100ms
LruRedux::ThreadSafeCache
12.187k i/100ms
ActiveSupport::Cache::DalliStore
529.000 i/100ms
ActiveSupport::Cache::RedisStore
469.000 i/100ms
ActiveSupport::Cache::DalliStore at pub-memcache-13640.us-east-1-1.2.ec2.garantiadata.com:13640
2.000 i/100ms
ActiveSupport::Cache::RedisStore at pub-redis-11469.us-east-1-4.2.ec2.garantiadata.com
2.000 i/100ms
-------------------------------------------------
ActiveSupport::Cache::FileStore
12.341k (±16.1%) i/s - 59.052k
ActiveSupport::Cache::MemoryStore
52.808k (± 4.3%) i/s - 266.625k
LruRedux::ThreadSafeCache
337.353k (± 6.5%) i/s - 1.682M
ActiveSupport::Cache::DalliStore
6.629k (± 2.0%) i/s - 33.327k
ActiveSupport::Cache::RedisStore
6.305k (± 2.6%) i/s - 31.892k
ActiveSupport::Cache::DalliStore at pub-memcache-13640.us-east-1-1.2.ec2.garantiadata.com:13640
26.891 (±14.9%) i/s - 134.000
ActiveSupport::Cache::RedisStore at pub-redis-11469.us-east-1-4.2.ec2.garantiadata.com
25.825 (±11.6%) i/s - 128.000
Comparison:
LruRedux::ThreadSafeCache: 337353.5 i/s
ActiveSupport::Cache::MemoryStore: 52808.1 i/s - 6.39x slower
ActiveSupport::Cache::FileStore: 12341.5 i/s - 27.33x slower
ActiveSupport::Cache::DalliStore: 6629.1 i/s - 50.89x slower
ActiveSupport::Cache::RedisStore: 6304.6 i/s - 53.51x slower
ActiveSupport::Cache::DalliStore at pub-memcache-13640.us-east-1-1.2.ec2.garantiadata.com:13640: 26.9 i/s - 12545.27x slower
ActiveSupport::Cache::RedisStore at pub-redis-11469.us-east-1-4.2.ec2.garantiadata.com: 25.8 i/s - 13062.87x slowe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment