| 14 | const { spawn } = require('child_process'); |
| 15 | |
| 16 | function main({ dur, len, serialization }) { |
| 17 | const msg = { payload: '.'.repeat(len) }; |
| 18 | const options = { |
| 19 | stdio: ['ignore', 'ignore', 'ignore', 'ipc'], |
| 20 | serialization, |
| 21 | }; |
| 22 | const child = spawn(process.argv[0], |
| 23 | [process.argv[1], 'child'], options); |
| 24 | |
| 25 | let messages = 0; |
| 26 | let finished = false; |
| 27 | |
| 28 | child.on('message', () => { |
| 29 | messages++; |
| 30 | // Keep one round-trip in flight per completed one so both the serialize |
| 31 | // (write) and deserialize (read) paths stay saturated on both ends. |
| 32 | if (!finished) |
| 33 | child.send(msg); |
| 34 | }); |
| 35 | |
| 36 | bench.start(); |
| 37 | // Prime a window of in-flight messages so the IPC channel never drains. |
| 38 | for (let i = 0; i < 256; i++) |
| 39 | child.send(msg); |
| 40 | |
| 41 | setTimeout(() => { |
| 42 | finished = true; |
| 43 | bench.end(messages); |
| 44 | child.kill(); |
| 45 | }, dur * 1000); |
| 46 | } |
| 47 | } |