Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fast redis filler!
#!/usr/bin/env ruby
require 'redis'
require 'hiredis'
require 'digest/sha1'
require 'benchmark'
def new_digest
Digest::SHA1.hexdigest( Time.now.strftime("%9N").to_s )
end
def swap
`free -m | grep 'Swap' | awk '{print $3}'`
end
r = Redis.new(driver: :hiredis)
Benchmark.measure do
50000.times do |i|
data = {}
start = Time.now.to_f
10000.times do
digest = "#{new_digest}#{new_digest}"
key = "#{digest[0..1]}#{i.to_s}"
data[key] ||= []
data[key] << digest
end
r.pipelined do
data.each do |key,value|
r.sadd(key, value)
end
end
finish = Time.now.to_f
duration = "%.3f" % (finish - start)
puts "[#{Time.at(finish).to_i}][#{duration}] Saved #{i * 10000} items"
puts "[#{Time.at(finish).to_i}][#{duration}] Swap: #{swap}" if i > 0 and i % 25 == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment