discourse/script/benchmarks/cache/bench.rb
Sam Saffron 14db879a31 DEV: Implement a faster Discourse.cache
This is a bottom up rewrite of Discourse cache to support faster performance
and a limited surface area.

ActiveSupport::Cache::Store accepts many options we do not use, this partial
implementation only picks the bits out that we do use and want to support.

Additionally params are named which avoids typos such as "expires_at" vs "expires_in"

This also moves a few spots in Discourse to use Discourse.cache over setex
Performance of setex and Discourse.cache.write is similar.
2019-12-03 14:03:30 +01:00

96 lines
2.5 KiB
Ruby

# frozen_string_literal: true
require 'benchmark/ips'
require File.expand_path('../../../../config/environment', __FILE__)
Benchmark.ips do |x|
x.report("redis setex string") do |times|
while times > 0
$redis.setex("test_key", 60, "test")
times -= 1
end
end
x.report("redis setex marshal string") do |times|
while times > 0
$redis.setex("test_keym", 60, Marshal.dump("test"))
times -= 1
end
end
x.report("Discourse cache string") do |times|
while times > 0
Discourse.cache.write("test_key", "test", expires_in: 60)
times -= 1
end
end
x.report("Rails cache string") do |times|
while times > 0
Rails.cache.write("test_key_rails", "test", expires_in: 60)
times -= 1
end
end
x.compare!
end
Benchmark.ips do |x|
x.report("redis get string") do |times|
while times > 0
$redis.get("test_key")
times -= 1
end
end
x.report("redis get string marshal") do |times|
while times > 0
Marshal.load($redis.get("test_keym"))
times -= 1
end
end
x.report("Discourse read cache string") do |times|
while times > 0
Discourse.cache.read("test_key")
times -= 1
end
end
x.report("Rails read cache string") do |times|
while times > 0
Rails.cache.read("test_key_rails")
times -= 1
end
end
x.compare!
end
# Comparison:
# redis setex string: 13250.0 i/s
# redis setex marshal string: 12866.4 i/s - same-ish: difference falls within error
# Discourse cache string: 10443.0 i/s - 1.27x slower
# Rails cache string: 10367.9 i/s - 1.28x slower
# Comparison:
# redis get string: 13147.4 i/s
# redis get string marshal: 12789.2 i/s - same-ish: difference falls within error
# Rails read cache string: 10486.4 i/s - 1.25x slower
# Discourse read cache string: 10457.1 i/s - 1.26x slower
#
# After Cache re-write
#
# Comparison:
# redis setex string: 13390.9 i/s
# redis setex marshal string: 13202.0 i/s - same-ish: difference falls within error
# Discourse cache string: 12406.5 i/s - same-ish: difference falls within error
# Rails cache string: 12289.2 i/s - same-ish: difference falls within error
#
# Comparison:
# redis get string: 13589.6 i/s
# redis get string marshal: 13118.3 i/s - same-ish: difference falls within error
# Rails read cache string: 12482.2 i/s - same-ish: difference falls within error
# Discourse read cache string: 12296.8 i/s - same-ish: difference falls within error