diff --git a/examples/wait-enter.ts b/examples/wait-enter.ts new file mode 100644 index 0000000..3f2ad27 --- /dev/null +++ b/examples/wait-enter.ts @@ -0,0 +1,19 @@ +// Pauses startup until Enter is pressed — handy for attaching a profiler +// or inspector before a benchmark begins. Top-level await in an --import +// module runs to completion before the main module is evaluated. +// +// Usage: node --import ./examples/wait-enter.ts examples/benchmark-peers/main.ts + +import { isMainThread } from 'node:worker_threads'; +import { createInterface } from 'node:readline/promises'; + +// Worker threads inherit execArgv (including --import) — only the main +// thread should prompt; workers must start immediately. +if (isMainThread) { + const rl = createInterface({ input: process.stdin, output: process.stdout }); + await rl.question(`Press Enter to start ${process.argv[1] ?? ''}… `); + rl.close(); + // Don't let stdin pin the event loop afterward — benchmarks and runtime + // examples rely on natural process exit. + process.stdin.unref(); +}