Skip to content

Instantly share code, notes, and snippets.

@ssimeonov
Created July 21, 2013 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssimeonov/6047200 to your computer and use it in GitHub Desktop.
Save ssimeonov/6047200 to your computer and use it in GitHub Desktop.
Estimating the per-entry overhead of ActiveSupport::Cache::MemoryStore.
task :cache_size => :environment do
require 'objspace'
def calc_overhead(run, count)
cache = ActiveSupport::Cache.lookup_store :memory_store, size: 128.gigabytes
# keep all key/value pairs unique
cache.instance_variable_get(:'@data').compare_by_identity
GC.start
before = ObjectSpace.count_objects_size({})[:TOTAL]
GC.start
count.times { cache.fetch(Object.new) { Object.new } }
GC.start
after = ObjectSpace.count_objects_size({})[:TOTAL]
entry_size = ActiveSupport::Cache::Entry.new(Object.new).size
overhead = 1.0 * (after - before) / count - entry_size
printf "%3d %12d %12d %8d %s\n", run, before, after, overhead, cache.inspect
overhead
end
puts "Estimating ActiveSupport::Cache::MemoryStore overhead on #{Time.now}"
p [RUBY_ENGINE, RUBY_VERSION, RUBY_PATCHLEVEL, RUBY_PLATFORM]
runs, count = 10, 2**16
puts "\n#{runs} runs of #{count} #fetch operations:\n\n"
puts "Run Before After Overhead Cache\n"
puts "--- ---------- --------- -------- -----\n"
avg_overhead = (1..runs).map { |i| calc_overhead(i, count) }.sum / runs
puts "\n---> Average overhead for #{runs} runs with size #{count} is #{avg_overhead.floor}"
end
Estimating ActiveSupport::Cache::MemoryStore overhead on 2013-07-20 21:51:00 -0400
["ruby", "1.9.3", 429, "x86_64-darwin12.4.0"]
10 runs of 65536 #fetch operations:
Run Before After Overhead Cache
--- ---------- --------- -------- -----
1 79707791 96374071 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
2 79707911 96418223 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
3 79707911 96389887 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
4 79707911 96394735 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
5 79708063 96392007 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
6 79708063 96385047 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
7 79708063 96394887 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
8 79708063 96369063 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
9 79708063 96400647 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
10 79708063 96392007 242 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
---> Average overhead for 10 runs with size 65536 is 242
Estimating ActiveSupport::Cache::MemoryStore overhead on 2013-07-20 21:45:33 -0400
["ruby", "2.0.0", 247, "x86_64-darwin12.4.0"]
10 runs of 65536 #fetch operations:
Run Before After Overhead Cache
--- ---------- --------- -------- -----
1 85518335 102035095 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
2 85518639 102039943 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
3 85518639 102042775 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
4 85518639 102039895 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
5 85518799 102040247 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
6 85518799 102038183 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
7 85518799 102041495 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
8 85518799 102039671 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
9 85518799 102040823 240 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
10 85518799 102032135 239 <#ActiveSupport::Cache::MemoryStore entries=65536, size=786432, options={:size=>137438953472}>
---> Average overhead for 10 runs with size 65536 is 240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment