Create a gist now

Instantly share code, notes, and snippets.

@TimothyGu /README.md Secret
Last active Sep 24, 2017

Embed
What would you like to do?
Worker data transfer benchmarks

Data communication throughput benchmarks

Worker vs. Cluster

a.js uses Workers. b.js uses cluster module.

$ ../ayo/ayo a.js
bench: 2392.848ms
$ ../ayo/ayo b.js
bench: 3218.135ms

ArrayBuffer vs. ArrayBuffer transfer vs. SharedArrayBuffer

# after modification of a.js to use ArrayBuffers instead of structured data
$ ../ayo/ayo --harmony-sharedarraybuffer a.js
bench: 5.231ms
$ ../ayo/ayo a.js
bench: 1192.106ms
$ ../ayo/ayo a-with-transfer.js
bench: 5.183ms

License

Public Domain / CC0

'use strict';
const { Worker } = require('worker');
const { randomFillSync } = require('crypto');
const bigBuffer = new ArrayBuffer(10 * (1024 ** 2));
randomFillSync(new Uint8Array(bigBuffer));
const worker = new Worker(__dirname + '/a-with-transfer.worker.js');
let i = 100;
worker.on('message', (buf) => {
if (--i > 0) {
worker.postMessage(buf, [buf]);
return;
}
console.timeEnd('bench');
worker.terminate();
});
worker.once('online', () => {
console.time('bench');
worker.postMessage(bigBuffer, [bigBuffer]);
});
'use strict';
const worker = require('worker');
worker.on('workerMessage', (buf) => {
worker.postMessage(buf, [buf]);
});
'use strict';
const { Worker } = require('worker');
const { randomFillSync } = require('crypto');
// const bigBuffer = new (typeof SharedArrayBuffer === 'function' ? SharedArrayBuffer : ArrayBuffer)(10 * (1024 ** 2));
// randomFillSync(new Uint8Array(bigBuffer));
const bigBuffer = require('./generated.json');
const worker = new Worker(__dirname + '/a.worker.js');
let i = 100;
worker.on('message', (buf) => {
if (--i > 0) {
worker.postMessage(buf);
return;
}
console.timeEnd('bench');
worker.terminate();
});
worker.once('online', () => {
console.time('bench');
worker.postMessage(bigBuffer);
});
'use strict';
const worker = require('worker');
worker.on('workerMessage', (buf) => {
worker.postMessage(buf);
});
'use strict';
const cluster = require('cluster');
if (cluster.isMaster) {
const bigBuffer = require('./generated.json');
const worker = cluster.fork();
let i = 100;
worker.on('message', (buf) => {
if (--i > 0) {
worker.send(buf);
return;
}
console.timeEnd('bench');
worker.kill();
});
worker.once('online', () => {
console.time('bench');
worker.send(bigBuffer);
});
} else {
process.on('message', (buf) => {
process.send(buf);
});
}
This file has been truncated, but you can view the full file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment