summaryrefslogtreecommitdiff
path: root/benchmark/cluster
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2017-06-04 20:15:14 -0400
committerBrian White <mscdex@mscdex.net>2017-06-09 01:38:58 -0400
commit8208fdae2be11ff3c1126dc669ca63b6d08b0cb1 (patch)
tree2d530b55a36c79cb188cce192a8228218175f1e1 /benchmark/cluster
parentdd83d118697071b7b3191d4124930d3427450fda (diff)
downloadandroid-node-v8-8208fdae2be11ff3c1126dc669ca63b6d08b0cb1.tar.gz
android-node-v8-8208fdae2be11ff3c1126dc669ca63b6d08b0cb1.tar.bz2
android-node-v8-8208fdae2be11ff3c1126dc669ca63b6d08b0cb1.zip
child_process: reduce nextTick() usage
PR-URL: https://github.com/nodejs/node/pull/13459 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'benchmark/cluster')
-rw-r--r--benchmark/cluster/echo.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/benchmark/cluster/echo.js b/benchmark/cluster/echo.js
new file mode 100644
index 0000000000..0733bdbd20
--- /dev/null
+++ b/benchmark/cluster/echo.js
@@ -0,0 +1,70 @@
+'use strict';
+
+const cluster = require('cluster');
+if (cluster.isMaster) {
+ const common = require('../common.js');
+ const bench = common.createBenchmark(main, {
+ workers: [1],
+ payload: ['string', 'object'],
+ sendsPerBroadcast: [1, 10],
+ n: [1e5]
+ });
+
+ function main(conf) {
+ var n = +conf.n;
+ var workers = +conf.workers;
+ var sends = +conf.sendsPerBroadcast;
+ var expectedPerBroadcast = sends * workers;
+ var payload;
+ var readies = 0;
+ var broadcasts = 0;
+ var msgCount = 0;
+
+ switch (conf.payload) {
+ case 'string':
+ payload = 'hello world!';
+ break;
+ case 'object':
+ payload = { action: 'pewpewpew', powerLevel: 9001 };
+ break;
+ default:
+ throw new Error('Unsupported payload type');
+ }
+
+ for (var i = 0; i < workers; ++i)
+ cluster.fork().on('online', onOnline).on('message', onMessage);
+
+ function onOnline(msg) {
+ if (++readies === workers) {
+ bench.start();
+ broadcast();
+ }
+ }
+
+ function broadcast() {
+ var id;
+ if (broadcasts++ === n) {
+ bench.end(n);
+ for (id in cluster.workers)
+ cluster.workers[id].disconnect();
+ return;
+ }
+ for (id in cluster.workers) {
+ const worker = cluster.workers[id];
+ for (var i = 0; i < sends; ++i)
+ worker.send(payload);
+ }
+ }
+
+ function onMessage(msg) {
+ if (++msgCount === expectedPerBroadcast) {
+ msgCount = 0;
+ broadcast();
+ }
+ }
+ }
+} else {
+ process.on('message', function(msg) {
+ process.send(msg);
+ });
+}