Skip to content

Instantly share code, notes, and snippets.

@pkra
Created February 20, 2017 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkra/6490061eac8a4f3bf53f38c5977cbf80 to your computer and use it in GitHub Desktop.
Save pkra/6490061eac8a4f3bf53f38c5977cbf80 to your computer and use it in GitHub Desktop.
mathjax-node experiment with forking child processes
const mathjax = require('mathjax-node');
const typeset = mathjax.typeset;
process.on('message', function(m) {
if (m === 'exit') {
// empty typeset to push exit on MathJax's queue (else we'd terminate too soon)
typeset({"math": "", "format": "TeX", "mml":true}, function(result) {
process.send('exit');
})
} else {
var data = JSON.parse(m);
typeset(data, function(result) {
process.send(result.mml);
})
}
});
const cp = require('child_process');
const os = require('os');
// const threads = os.cpus().length;
const threads = 2;
const n = 1000;
const start = new Date();
// 1,000 nodes (results in ms)
//
// 1 thread 3559, 4161, 4001
// 2 threads 3075, 2976, 3046
// 3 threads 3403, 3331, 3399
// 4 threads 3928, 3653, 3633
//
// 5,000 nodes
// 1 thread 15367
// 2 threads 11577
// 3 threads 11280
// 4 threads 11854
//
// 10,000 nodes
//
// 1 thread 34345, 30004, 29587
// 2 threads 19238, 18948, 21973
// 3 threads 18850, 19434, 19379
// 4 threads 20117, 19744, 19551
// SVG 2: 19347, 4: 19347, 3: 17643
const messaging = function(m, child) {
// Receive results from child process
console.log('received: ' + m);
if (m === 'exit') {
child.kill()
var end = new Date() - start;
console.log(end);
}
}
let children = {};
for (let k = 0; k < threads; k++){
let child = cp.fork('./child.js');
children[k] = child;
child.on('message', function(m){
messaging(m, child);
});
}
const mjprocess = function(i){
let k = i%threads;
let child = children[k];
child.send('{"math": "\\\\alpha' + i + '", "format": "TeX", "mml":true}');
if (i+threads > n-1) child.send('exit');
}
for (let i=0; i < n; i++){
mjprocess(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment