Bobochka
12/15/2015 - 10:10 PM

Gett-media benchmarks

Gett-media benchmarks

require 'benchmark/ips'

redis = Redis.new(:host => "127.0.0.1", :port => 6379, :db => 0)

cache_options = { expires_in: 1 }

keys1 = ('a'..'z').to_a
keys2 = ('aa'..'az').to_a

Benchmark.ips do |x|
  x.config(:time => 15, :warmup => 1)

  x.report('current') do
    noncached_keys = []
        
    result = Rails.cache.fetch_multi(*keys1, cache_options) { |key| noncached_keys << key; nil }

    if noncached_keys.present?
      new_data = noncached_keys.map { |key| [key, key] }
      new_data.each do |key, data|
        result[key] = Rails.cache.write key, data, cache_options
      end
    end
  end

  x.report('pipelined') do
    noncached_keys = []
    values = redis.mget(*keys2)
    result = Hash[keys2.zip(values)]

    result.each { |k, v| v.nil? && (noncached_keys << k) }

    if noncached_keys.present?
      new_data = noncached_keys.map { |key| [key, key] }
      redis.pipelined do
        new_data.each do |key, data|
          result[key] = redis.setex(key, cache_options[:expires_in], data) && data
        end
      end
    end
  end
end

# Calculating -------------------------------------
#             current   133.000  i/100ms
#           pipelined   328.000  i/100ms
# -------------------------------------------------
#             current      1.327k (± 4.4%) i/s -     19.950k
#           pipelined      3.326k (± 3.6%) i/s -     49.856k