Skip to content

Instantly share code, notes, and snippets.

@sebv
Last active November 22, 2015 09:57
Show Gist options
  • Save sebv/c944f149bcd0ac3d48a4 to your computer and use it in GitHub Desktop.
Save sebv/c944f149bcd0ac3d48a4 to your computer and use it in GitHub Desktop.
var B = require("bluebird");
var _ = require("lodash");
function run(concurrency) {
function log(id, msg) {
// only logging first 100
if(id <= 100) {
console.log(id + " -> " + msg);
}
}
function waitFor(id ,duration, remaining) {
if(_.isUndefined(remaining)) { remaining = duration; }
if(remaining <= 0) {
log(id, "done");
return;
}
log(id, remaining + "/" + duration + " seconds remaining");
return B.delay(1000)
.then(function(){
return waitFor(id, duration, remaining - 1);
});
}
var promises = [];
_.times(concurrency, function(idx) {
promises.push(B.delay(Math.random()*1000) // spreading the load
.then(function() {
return waitFor(idx + 1, 10);
}));
});
ids = null;
B.all(promises).then(function() {
console.log("All done");
}).done();
}
//run(10);
//run(100);
//run(1000);
//run(10000);
//run(100000);
run(500000);
//run(1000000); // reaching the limit, too much gc
//run(1500000); // about the the limit
//run(2000000); // crashes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment