Skip to content

Instantly share code, notes, and snippets.

@danielnc
Created October 25, 2012 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielnc/3954646 to your computer and use it in GitHub Desktop.
Save danielnc/3954646 to your computer and use it in GitHub Desktop.
Ruby-cli --pipe vs Ruby-rb vs Ruby-rb Hiredis benchmark
require 'rubygems'
require 'bundler'
require 'bench_press'
require 'tempfile'
require 'redis'
require 'hiredis'
extend BenchPress
reps 100
total_data = 10_000
def gen_redis_proto(*cmd)
proto = ""
proto << "*"+cmd.length.to_s+"\r\n"
cmd.each{|arg|
proto << "$"+arg.to_s.bytesize.to_s+"\r\n"
proto << arg.to_s+"\r\n"
}
proto
end
def redis_data(connection, total)
total.times { |n| connection.set("Key#{n}","Value#{n}") }
end
def mass_create(total)
file = File.open("/tmp/redis#{Time.now.to_i}#{rand}", "w")
total.times { |n| file.write(gen_redis_proto("SET","Key#{n}","Value#{n}")) }
file.close
file.path
end
`redis-cli flush-all`
measure "Redis-cli pipe" do
system("cat #{mass_create(total_data)} | redis-cli --pipe")
end
`redis-cli flush-all`
measure "Redis-rb" do
redis_data(Redis.new, total_data)
end
`redis-cli flush-all`
measure "Redis-rb with Hiredis" do
redis_data(Redis.new(driver: :hiredis), total_data)
end
`redis-cli flush-all`
measure "Redis-rb pipelined" do
r = Redis.new
r.pipelined do
redis_data(r, total_data)
end
end
`redis-cli flush-all`
measure "Redis-rb pipelined with Hiredis" do
r = Redis.new(driver: :hiredis)
r.pipelined do
redis_data(r, total_data)
end
end
source "http://rubygems.org"
gem 'bundler', ">= 1.2.1"
gem 'redis', ">= 3.0.2"
gem 'hiredis', ">= 0.4.5"
gem 'bench_press', ">= 0.3.1"
GEM
remote: http://rubygems.org/
specs:
activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
bench_press (0.3.1)
activesupport (>= 2.3.5)
facter (>= 1.5.7)
httparty (>= 0.6.1)
jeweler (>= 1.4.0)
facter (1.6.13)
git (1.2.5)
hiredis (0.4.5)
httparty (0.9.0)
multi_json (~> 1.0)
multi_xml
i18n (0.6.1)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
rdoc
json (1.7.5)
multi_json (1.3.6)
multi_xml (0.5.1)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
redis (3.0.2)
PLATFORMS
ruby
DEPENDENCIES
bench_press (>= 0.3.1)
bundler (>= 1.2.1)
hiredis (>= 0.4.5)
redis (>= 3.0.2)
Benchmark
=========
Date: October 25, 2012
System Information
------------------
Operating System: Mac OS X 10.6.8 (10K549)
CPU: Intel Core 2 Duo 2.4 GHz
Processor Count: 2
Memory: 4 GB
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]
"Redis-rb pipelined with Hiredis" is up to 92% faster over repetitions
-----------------------------------------------------------------------
Redis-rb pipelined with Hiredis 51.649276 secs Fastest
Redis-cli pipe 79.242871 secs 34% Slower
Redis-rb pipelined 193.139364 secs 73% Slower
Redis-rb with Hiredis 583.220636 secs 91% Slower
Redis-rb 678.863098 secs 92% Slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment