Skip to content

Instantly share code, notes, and snippets.

@keymastervn
Forked from jgaskins/benchmark.rb
Last active April 14, 2022 10:21
Show Gist options
  • Save keymastervn/98f885c2619c853c4fb35d395c715528 to your computer and use it in GitHub Desktop.
Save keymastervn/98f885c2619c853c4fb35d395c715528 to your computer and use it in GitHub Desktop.
Benchmarking redis gem vs hiredis
require 'bundler/inline'
gemfile true do
source 'https://rubygems.org'
gem 'redis'
gem 'hiredis'
gem 'benchmark-ips'
end
require 'benchmark'
require 'benchmark/ips'
require 'redis'
require 'hiredis'
redis = Redis.new(driver: :ruby)
hiredis = Redis.new(driver: :hiredis)
key = "foo"
value = "bar"
cpu_time_iterations = 100_000
puts "======= Get ======="
redis.set key, "." * 2048 # 2KB cache entry
Benchmark.ips do |x|
x.report("ruby") { redis.get key }
x.report("hiredis") { hiredis.get key }
x.compare!
end
puts
puts "Cache hit CPU time"
puts "ruby : #{Benchmark.measure { cpu_time_iterations.times { redis.get key } }.total}"
puts "hiredis: #{Benchmark.measure { cpu_time_iterations.times { hiredis.get key } }.total}"
puts
puts "Cache miss (returns nil)"
redis.del key
Benchmark.ips do |x|
x.report("ruby") { redis.get key }
x.report("hiredis") { hiredis.get key }
x.compare!
end
puts
puts "Cache miss CPU time"
puts "ruby : #{Benchmark.measure { cpu_time_iterations.times { redis.get key } }.total}"
puts "hiredis: #{Benchmark.measure { cpu_time_iterations.times { hiredis.get key } }.total}"
puts "======= Set ======="
Benchmark.ips do |x|
x.report("ruby") { redis.set(key, value) }
x.report("hiredis") { hiredis.set(key, value) }
x.compare!
end
puts
puts "Set CPU time"
puts "ruby : #{Benchmark.measure { cpu_time_iterations.times { redis.set(key, value) } }.total}"
puts "hiredis: #{Benchmark.measure { cpu_time_iterations.times { hiredis.set(key, value) } }.total}"
puts
puts "Set + ex/exat CPU time"
t = Time.now.to_i
puts "ruby : #{Benchmark.measure { cpu_time_iterations.times { redis.set(key, value, ex: t+10) } }.total}"
puts "hiredis: #{Benchmark.measure { cpu_time_iterations.times { hiredis.set(key, value, ex: t+10) } }.total}"
Fetching gem metadata from https://rubygems.org/....
Resolving dependencies...
Using benchmark-ips 2.10.0
Using bundler 2.2.17
Using hiredis 0.6.3
Using redis 4.6.0
Warming up --------------------------------------
ruby 1.040k i/100ms
hiredis 1.790k i/100ms
Calculating -------------------------------------
ruby 12.671k (±17.8%) i/s - 60.320k in 5.019264s
hiredis 17.649k (± 6.0%) i/s - 89.500k in 5.094707s
Comparison:
hiredis: 17648.8 i/s
ruby: 12670.8 i/s - 1.39x (± 0.00) slower
Cache hit CPU time
ruby : 5.770363999999999
hiredis: 3.0755919999999985
Cache miss (returns nil)
Warming up --------------------------------------
ruby 1.589k i/100ms
hiredis 1.847k i/100ms
Calculating -------------------------------------
ruby 15.262k (± 9.7%) i/s - 76.272k in 5.073104s
hiredis 17.945k (± 7.4%) i/s - 90.503k in 5.076556s
Comparison:
hiredis: 17945.1 i/s
ruby: 15261.8 i/s - same-ish: difference falls within error
Cache miss CPU time
ruby : 5.019928
hiredis: 3.159751
======= Set =======
Warming up --------------------------------------
ruby 1.395k i/100ms
hiredis 1.734k i/100ms
Calculating -------------------------------------
ruby 14.143k (±12.9%) i/s - 69.750k in 5.060075s
hiredis 17.794k (± 4.7%) i/s - 90.168k in 5.081172s
Comparison:
hiredis: 17794.4 i/s
ruby: 14142.5 i/s - 1.26x (± 0.00) slower
Set CPU time
ruby : 4.6450840000000015
hiredis: 3.0060930000000017
Set + ex/exat CPU time
ruby : 4.802317000000001
hiredis: 3.0586850000000005
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment