Skip to content

Instantly share code, notes, and snippets.

@bolshakov
Last active May 13, 2025 11:04
Show Gist options
  • Save bolshakov/bb43abedf984dbbcfe4597526ab52daa to your computer and use it in GitHub Desktop.
Save bolshakov/bb43abedf984dbbcfe4597526ab52daa to your computer and use it in GitHub Desktop.
Stoplight: ZSET Bucket Size Benchmark
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# frozen_string_literal: true
require "benchmark/ips"
require "stoplight"
require "redis"
require "timecop"
redis = Redis.new
data_store = Stoplight::DataStore::Redis.new(redis)
# In your benchmark file
BUCKET_SIZES = [60, 300, 600, 900, 1800, 3600].freeze # 1m to 1h
WINDOW_SIZES = (300..3600).step(300).to_a.freeze # 5m to 1h
results = {}
BUCKET_SIZES.each do |bucket_size_value|
puts "=== Testing bucket_size: #{bucket_size_value}s ==="
WINDOW_SIZES.each do |window_size|
puts "Testing window_size: #{window_size}s"
# Configure your ZSET implementation with this bucket size
# (You might need to make bucket size configurable)
stoplight = Stoplight(SecureRandom.uuid, window_size: window_size, data_store: data_store)
Stoplight::DataStore::Redis.singleton_class.class_eval(<<~RUBY)
def bucket_size
#{bucket_size_value}
end
RUBY
start_time = Time.at(1747129162) - window_size
result = Benchmark.ips do |b|
b.report("#{bucket_size_value}s buckets, #{window_size}s window") do
Timecop.freeze(start_time + rand(window_size * 2)) do
stoplight.run {}
end
end
end
results[[bucket_size_value, window_size]] = result.data.dig(0, :ips)
end
end
require "json"
File.write("zset_performance_matrix.json", results.to_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment