-
-
Save fabn/6d688a4d4cdc2b148f3d to your computer and use it in GitHub Desktop.
Redis benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :benchmarks do | |
namespace :redis do | |
desc 'Compare redis set vs redis hmset' | |
task :hmset do | |
REDIS = Redis.new(url: 'redis://localhost:6379/11') | |
REDIS.flushdb | |
Benchmark.ips do |x| | |
x.report('set') do | |
index = rand(1..1_000_000) | |
REDIS.set("string-#{index}", SecureRandom.hex(100)) | |
end | |
x.report('hmset') do | |
index = rand(1..1_000_000) | |
obj = {value: SecureRandom.hex(100), compression: 1, marshal: :oj} | |
REDIS.hmset("hash-#{index}", *obj.to_a.flatten) | |
end | |
x.report('setex') do | |
index = rand(1..1_000_000) | |
REDIS.setex("string-#{index}", 3600, SecureRandom.hex(100)) | |
end | |
x.report('hmset + expire') do | |
index = rand(1..1_000_000) | |
obj = {value: SecureRandom.hex(100), compression: 1, marshal: :oj} | |
REDIS.multi do | |
REDIS.hmset("hash-#{index}", *obj.to_a.flatten) | |
REDIS.expire("hash-#{index}", 3600) | |
end | |
end | |
x.compare! | |
end | |
end | |
desc 'Compare redis get vs redis hgetall' | |
task :hgetall do | |
REDIS = Redis.new(url: 'redis://localhost:6379/11') | |
REDIS.flushdb | |
keys = (1..10_000).to_a | |
keys.each do |index| | |
REDIS.set("string-#{index}", SecureRandom.hex(100)) | |
obj = {value: SecureRandom.hex(100), compression: 1, marshal: :oj} | |
REDIS.hmset("hash-#{index}", *obj.to_a.flatten) | |
end | |
Benchmark.ips do |x| | |
x.report('get') do | |
index = keys.sample | |
REDIS.get("string-#{index}") | |
end | |
x.report('hgetall') do | |
index = keys.sample | |
REDIS.hgetall("hash-#{index}") | |
end | |
x.compare! | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Calculating ------------------------------------- | |
set 175.000 i/100ms | |
hmset 180.000 i/100ms | |
setex 178.000 i/100ms | |
hmset + expire 150.000 i/100ms | |
------------------------------------------------- | |
set 2.383k (±30.3%) i/s - 10.500k | |
hmset 2.349k (±29.1%) i/s - 10.620k | |
setex 2.412k (±30.2%) i/s - 10.680k | |
hmset + expire 1.683k (± 8.6%) i/s - 8.400k | |
Comparison: | |
setex: 2412.1 i/s | |
set: 2383.2 i/s - 1.01x slower | |
hmset: 2348.6 i/s - 1.03x slower | |
hmset + expire: 1682.6 i/s - 1.43x slower | |
Calculating ------------------------------------- | |
get 191.000 i/100ms | |
hgetall 180.000 i/100ms | |
------------------------------------------------- | |
get 2.482k (±29.2%) i/s - 11.269k | |
hgetall 2.150k (±26.3%) i/s - 10.080k | |
Comparison: | |
get: 2482.4 i/s | |
hgetall: 2149.6 i/s - 1.15x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment