Skip to content

Instantly share code, notes, and snippets.

@llk23r
Created February 9, 2023 10:32
Show Gist options
  • Save llk23r/bb069f48afb4f78efbeecca3f405d6c6 to your computer and use it in GitHub Desktop.
Save llk23r/bb069f48afb4f78efbeecca3f405d6c6 to your computer and use it in GitHub Desktop.
atomic vs pipelined hset
require "redis"
require "benchmark"
def setup
@redis = Redis.new
@redis.flushdb
end
def teardown
@redis.flushdb
@redis.quit
end
def atomic_hset
setup
n = 1_000_000
time = Benchmark.realtime do
n.times do |i|
@redis.hset("key#{i}", "field#{i}", "value#{i}")
end
end
teardown
puts "#{n} Atomic hset: #{time}s"
end
def pipelined_hset
setup
n = 1_000_000
time = Benchmark.realtime do
@redis.pipelined do
n.times do |i|
@redis.hset("key#{i}", "field#{i}", "value#{i}")
end
end
end
teardown
puts "#{n} Pipelined hset: #{time}s"
end
atomic_hset
pipelined_hset
# Performance Comparison:
#
# | Operation | Items | Time (s) |
# |-----------|-------|----------|
# | Atomic hset | 1 million | 397.95090500000515 |
# | Pipelined hset | 1 million | 16.061125000000175 |
#
# Conclusion:
# Pipelined hset is 24 times faster than atomic hset.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment