Skip to content

Instantly share code, notes, and snippets.

@bluwy
Last active January 3, 2025 11:36
Show Gist options
  • Save bluwy/e6389ea0298434f5baea652b2cca6959 to your computer and use it in GitHub Desktop.
Save bluwy/e6389ea0298434f5baea652b2cca6959 to your computer and use it in GitHub Desktop.
Benchmark the time spent on transferring data in child process

Copy the file s and run node main.js. There's some numbers you can tweak at the top of the file imitating a project with some modules and average module file size.

It may take a while to run. Line 29 shows the time a took between a stdin write and stdout read for each request made.

process.stdin.on('data', (data) => {
process.stdout.write(data.toString().repeat(1))
})
const { spawn } = require('child_process')
const { performance } = require('perf_hooks')
const NUMBER_OF_MODULES = 40000
const AVERAGE_MODULE_SIZE = 300 // characters
const child = spawn('node', ['child.js'])
const startTimes = {}
let idCounter = 0
let buffer = ''
child.stdout.on('data', (data) => {
buffer += data.toString()
let lines = buffer.split('\n')
buffer = lines.pop() // Keep the last partial line in the buffer
lines.forEach((line) => {
if (line) {
handle(line)
}
})
})
let durations = []
function handle(data) {
const [id, message] = data.split(':')
const endTime = performance.now()
console.log(id, endTime - startTimes[id])
durations.push(endTime - startTimes[id])
delete startTimes[id]
if (+id === NUMBER_OF_MODULES - 1) {
console.log('avg', durations.reduce((a, b) => a + b, 0) / durations.length)
process.exit()
}
}
const inputs = Array.from({ length: NUMBER_OF_MODULES }, (_, i) => i + 1)
inputs.forEach((input) => {
const id = idCounter++
startTimes[id] = performance.now()
child.stdin.write(`${id}:${'a'.repeat(AVERAGE_MODULE_SIZE)}\n`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment