Skip to content

Instantly share code, notes, and snippets.

@xk
Created May 19, 2012 11:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xk/2730481 to your computer and use it in GitHub Desktop.
Save xk/2730481 to your computer and use it in GitHub Desktop.
7M keys hash and GC hiccups
// 2012-05-19 jorge@jorgechamorro.com
var o;
function boot () {
o= {};
var i= 7e6;
while (i--) {
var key= i.toString(36);
o[key]= { id:key };
}
}
function rnd (x) { return Math.floor(Math.random()*x) }
function touchSingleThread () {
keysCtr++;
var key= rnd(7e6).toString(36);
o[key]= {id:key};
process.stdout.write(' '+ key);
process.nextTick(touchSingleThread);
}
function touchMultiThread () {
var key= rnd(7e6).toString(36);
var cmd= 'o["'+ key+ '"]= {id:"'+ key+ '"};"'+ key+ '";';
thread.eval(cmd, cb);
}
function cb (err, key) {
keysCtr++;
process.stdout.write(' '+ key);
process.nextTick(touchMultiThread);
}
function loop () {
if ((++loopsCtr % 1e3) === 0) dspRates();
process.nextTick(loop);
}
function dspRates () {
var now= Date.now();
var str= "\n\n\n***** Event loop ticks per second -> "+ Math.floor(loopsCtr*1e3/((now- t0) || 1));
str+= ", keys per second -> "+ Math.floor(keysCtr*1e3/((now- t0) || 1))+ "\n\n";
console.log(str);
}
var loopsCtr= 0;
var keysCtr= 0;
var t0;
if (process.argv.length > 2) {
console.log("Multi thread");
var thread= require('threads_a_gogo').create().eval(boot).eval('boot()', function () {
t0= Date.now();
loop();
touchMultiThread();
});
}
else {
console.log("Single thread");
boot();
t0= Date.now();
loop();
touchSingleThread();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment