Skip to content

Instantly share code, notes, and snippets.

@NateBarnes
Created June 27, 2012 06:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save NateBarnes/3001890 to your computer and use it in GitHub Desktop.
Save NateBarnes/3001890 to your computer and use it in GitHub Desktop.
Postgres vs Redis Object Storage Benchmarks
require "pg"
require "redis"
words = File.read("/usr/share/dict/words").split "\n"
pgconn = PGconn.open :dbname => "test_hstore", :port => 5433, :user => "test_user", :password => "changeme"
redis = Redis.new
reps = [1,10,100,1000,10000]
pg_inserts = []
pg_selects = []
redis_sets = []
redis_gets = []
reps.each do |num|
t0 = Time.now
num.times do |idx|
pgconn.exec "INSERT INTO test_table VALUES (#{idx+1}, hstore('#{words[idx]}'=>'#{idx}'))"
end
pg_inserts << Time.now - t0
t0 = Time.now
num.times do
pgconn.exec "SELECT * FROM test_table"
end
pg_selects << Time.now - t0
pgconn.exec "TRUNCATE TABLE test_table"
end
reps.each do |num|
t0 = Time.now
num.times do |idx|
redis.hset idx, words[idx], idx
end
redis_sets << Time.now - t0
t0 = Time.now
num.times do |idx|
redis.hget idx, words[idx]
end
redis_gets << Time.now - t0
num.times do |idx|
redis.del idx
end
end
log = File.open "benchmarks.log", "w"
log.puts "Postgres Benchmarks: "
log.puts " Inserts:"
reps.each_with_index do |num, idx|
log.puts " #{num}: #{pg_inserts[idx]}"
end
log.puts " Selects:"
reps.each_with_index do |num, idx|
log.puts " #{num}: #{pg_selects[idx]}"
end
log.puts " "
log.puts "Redis Benchmarks: "
log.puts " Sets:"
reps.each_with_index do |num, idx|
log.puts " #{num}: #{redis_sets[idx]}"
end
log.puts " Gets:"
reps.each_with_index do |num, idx|
log.puts " #{num}: #{redis_gets[idx]}"
end
Postgres Benchmarks:
Inserts:
1: 0.00212
10: 0.008992
100: 0.045435
1000: 0.443837
10000: 4.660529
Selects:
1: 0.033782
10: 0.003462
100: 0.023476
1000: 1.077131
10000: 164.168994
Redis Benchmarks:
Sets:
1: 0.001448
10: 0.001114
100: 0.744665
1000: 0.296913
10000: 1.122622
Gets:
1: 0.000119
10: 0.001038
100: 0.020944
1000: 0.098915
10000: 0.92371
@benkoffel
Copy link

The Postgres GET test is invalid as you have a bug in your code.

You are selecting the whole table X times, compared to redis where you are selecting each individual value.

pgconn.exec "SELECT * FROM test_table"

redis.hget idx, words[idx]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment