Sha256: bd8b1bbb437a4d0bc3e9dc5c76a14f91a5c24f3d71405a619eedb08ffb60aa77

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

#!/usr/bin/env ruby

lib = File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'fast_cache'
require 'lru_redux'
require 'active_support/cache'
require 'json'

class ModifiedLru < LruRedux::Cache
  # Modify API to be consistent with other caches
  def fetch(key, &block)
    getset(key, &block)
  end
end

def make_caches(size, exemplar_value, ttl)
  marshaled = Marshal.dump(exemplar_value)
  {
      lru_redux: ModifiedLru.new(size),
      fast_cache: FastCache::Cache.new(size, ttl),
      memory_store: ActiveSupport::Cache.lookup_store(:memory_store, size: size * marshaled.length + 1)
  }
end

def complex_value
  sample_file = File.expand_path('../../bench/caching_sample.json',  __FILE__)
  JSON.parse(File.read(sample_file))
end

def run_test(size, value, ttl = 60*60)
  caches = make_caches(size, value, ttl)

  Benchmark.bmbm do |bm|
    caches.each.map(&:last).map(&:clear)

    caches.each_pair do |name, cache|
      bm.report name do
        1000000.times do
          cache.fetch(rand(2 * size)) { value }
        end
      end
    end
  end
end

puts "Simple value benchmark\n"
run_test 1_000, :value

puts "\nComplex value benchmark\n"
run_test 1_000, complex_value

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fast_cache-1.0.1 bin/fast-cache-benchmark
fast_cache-1.0.0 bin/fast-cache-benchmark