Created
October 9, 2012 21:44
-
-
Save larsburgess/3861648 to your computer and use it in GitHub Desktop.
Fast redis filler!
This file contains hidden or 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 | |
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