discourse/script/benchmarks/cache/bench.rb
2019-12-03 14:03:21 +01:00

82 lines
1.9 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