Skip to content

Instantly share code, notes, and snippets.

@jherdman
Created February 7, 2012 18:18
Show Gist options
  • Save jherdman/1761072 to your computer and use it in GitHub Desktop.
Save jherdman/1761072 to your computer and use it in GitHub Desktop.
Redis Insertion Experiment
#!/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);
}
});
#!/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