Created
February 7, 2012 18:18
-
-
Save jherdman/1761072 to your computer and use it in GitHub Desktop.
Redis Insertion Experiment
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 node | |
var redis = require('redis') | |
, client = redis.createClient() | |
, numEntries = parseInt(process.argv[2], 10) | |
, paused = false | |
, numKeys | |
, key | |
, set; | |
if (isNaN(numEntries)) { | |
console.error('Forgot to include number of sets to create'); | |
process.exit(1); | |
} | |
function seededArray(len) { | |
var array = new Array(len); | |
for (var i = 0; i < len; i++) { | |
array[i] = i.toString(); | |
} | |
return array; | |
} | |
function feed () { | |
if (numEntries <= 0) { | |
console.log('Finished'); | |
console.timeEnd('import'); | |
process.exit(0); | |
} | |
numEntries--; | |
numKeys = Math.floor(Math.random() * 10) + 1; | |
key = "exp:" + numEntries; | |
set = seededArray(numKeys); | |
if (client.sadd(key, set) === false) { | |
console.log('Pausing at ', numEntries, ' entries remaining'); | |
paused = true; | |
} else { | |
console.log('INSERTED ', key, ' => ', set); | |
process.nextTick(feed); | |
} | |
} | |
client.on('error', function (err) { | |
console.log('Error ' + err); | |
}); | |
client.on('ready', function () { | |
console.log('Flushing DB'); | |
client.flushall(function () {}); | |
console.log('And away we go!'); | |
console.time('import'); | |
feed(); | |
}); | |
client.on('drain', function () { | |
if (paused) { | |
console.log('Resuming at ', numEntries, ' remaining'); | |
paused = false; | |
process.nextTickt(feed); | |
} | |
}); |
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 | |
require 'hiredis' | |
require 'redis' | |
redis = Redis.new | |
num_entries = ARGV.shift.to_i | |
puts "Flusing existing entries" | |
redis.flushall | |
puts "And away we go!" | |
time_start = Time.now | |
num_entries.times do |count| | |
items = (1..rand(1..10)).to_a | |
key = "exp:#{count}" | |
redis.multi do |r| | |
items.each do |v| | |
r.sadd(key, v) | |
end | |
end | |
end | |
time_end = Time.now | |
puts "This took #{time_end - time_start} seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment