Skip to content

Instantly share code, notes, and snippets.

@jherdman
Created February 7, 2012 07:11
Show Gist options
  • Save jherdman/1757902 to your computer and use it in GitHub Desktop.
Save jherdman/1757902 to your computer and use it in GitHub Desktop.
Redis insertion experiment with Node
#!/usr/bin/env node
var redis = require('redis')
, client = redis.createClient()
, numEntries = parseInt(process.argv[2], 10)
, paused = false
, numKeys
, key
, set;
function seededArray(len) {
var array = new Array(len);
for (var i = 0; i < len; i++) {
array[i] = 'a';
}
return array;
}
function feed () {
if (numEntries <= 0) {
console.log('Finished');
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(op);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment