Last active
December 10, 2024 02:16
-
-
Save aks/cafe8508c0864e18e249558379d6273e to your computer and use it in GitHub Desktop.
Run some common commands on plain redis and namespaced redis and report the realtime performances with comparison ratios.
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
#!/usr/bin/env ruby | |
# run a bunch of tests on the redis server | |
# with and without namespace | |
# | |
# usage: ruby test-redis-ns.rb | |
require 'redis' | |
require 'redis-namespace' | |
require 'benchmark' | |
class TestRedis | |
NUM_KEYS = 10_000 | |
NUM_HKEYS = 100 | |
NUM_ITEMS = 60 | |
def initialize | |
@redis = new_redis | |
@redis_ns = Redis::Namespace.new(:ns, :redis => new_redis, warning: false) | |
end | |
def new_redis | |
Redis.new(:host => 'localhost', :port => 6379) | |
end | |
ACTIONS = { | |
set: [ | |
NUM_KEYS, | |
"Setting #{NUM_KEYS} keys", | |
:redis_set | |
], | |
get: [ | |
NUM_KEYS, | |
"Getting #{NUM_KEYS} keys", | |
:redis_get | |
], | |
hset: [ | |
NUM_HKEYS, | |
"HSETting #{NUM_ITEMS} items on #{NUM_HKEYS} hashes", | |
:redis_hset | |
], | |
hget: [ | |
NUM_HKEYS, | |
"HGETting #{NUM_ITEMS} items on #{NUM_HKEYS} hashes", | |
:redis_hget | |
] | |
} | |
def redis_set(redis, num) | |
redis.set("test_key_#{num}", num.to_s) | |
end | |
def redis_get(redis, num) | |
redis.get("test_key_#{num}") | |
end | |
def redis_hset(redis, num) | |
NUM_ITEMS.times { |inum| redis.hset("test_hkey_#{num}", "item_#{inum}", "test_key:#{num} - item:#{inum}") } | |
end | |
def redis_hget(redis, num) | |
NUM_ITEMS.times { |inum| redis.hget("test_hkey_#{num}", "item_#{inum}") } | |
end | |
def test_redis | |
@times = {} | |
@counts = {} | |
[ | |
[:redis, @redis, -> { reset_redis }], | |
[:redis_ns, @redis_ns, -> { reset_redis_ns }] | |
].each do |kind, redis, init| | |
puts "\nTesting #{kind}:" | |
init.call | |
GC.disable | |
test_redis_actions(kind, redis) | |
GC.enable | |
end | |
show_summary | |
end | |
def reset_redis | |
@redis.flushall | |
end | |
def reset_redis_ns | |
@redis_ns.redis.flushall | |
end | |
def test_redis_actions(kind, redis) | |
ACTIONS.each_pair do |action, (how_many, description, code)| | |
@counts[action] = how_many | |
@times[action] ||= {} | |
print "#{description} for #{kind}: " | |
@times[action][kind] = realtime { iterate(redis, how_many, code) } | |
end | |
end | |
def show_summary | |
puts "\nRedis vs Redis::Namespace Performance Times:" | |
printf "%8s %8s %8s %10s %6s \n", 'Action', 'Kind', 'Count', 'Time', 'Ratio' | |
@times.each_pair do |action, kind_data| | |
min_time = @times[action].values.min | |
count = @counts[action] | |
kind_data.each_pair do |kind, time| | |
printf "%8s %8s %8d %10.6f %6.2f%% \n", action, kind.to_s, count, time, (time*100/min_time) - 100 | |
end | |
puts '' | |
end | |
end | |
def iterate(redis, times, code) | |
chunk = (times / 10).to_i | |
times.times do |num| | |
print '.' if num % chunk == 0 | |
case code | |
when Symbol | |
send(code, redis, num) | |
when Proc | |
code.call(redis, num) | |
else | |
raise 'Invalid code' | |
end | |
end | |
puts '' | |
end | |
def realtime | |
Benchmark.realtime { yield } | |
end | |
end | |
TestRedis.new.test_redis |
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
$ ./test-redis-ns.rb | |
Testing redis: | |
Setting 10000 keys for redis: .......... | |
Getting 10000 keys for redis: .......... | |
HSETting 60 items on 100 hashes for redis: .......... | |
HGETting 60 items on 100 hashes for redis: .......... | |
Testing redis_ns: | |
Setting 10000 keys for redis_ns: .......... | |
Getting 10000 keys for redis_ns: .......... | |
HSETting 60 items on 100 hashes for redis_ns: .......... | |
HGETting 60 items on 100 hashes for redis_ns: .......... | |
Redis vs Redis::Namespace Performance Times: | |
Action Kind Count Time Ratio | |
set redis 10000 6.085226 0.00% | |
set redis_ns 10000 6.397227 5.13% | |
get redis 10000 6.707689 6.48% | |
get redis_ns 10000 6.299558 0.00% | |
hset redis 100 3.701900 0.00% | |
hset redis_ns 100 3.717516 0.42% | |
hget redis 100 3.735009 0.00% | |
hget redis_ns 100 4.003329 7.18% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment