Repository: mcollina/autocannon
Pull request #530 (2024-10-11 20:58:26)

assume onPort if -- node is set
Adds support for #180 

@mcollina Please review when you have some time.

karankraina: Thanks for your review @mcollina . Also, if we remove onPort flag, I think the check should not striclty be restricted to `-- node`. We should assume on Port if `-- <any-script>` is provided. Your thoughts ?
mcollina: Ah, that's true. Then, this is good. Can you just update the docs?
karankraina: Sure, updating.
karankraina: @mcollina Does it look okay to you?
karankraina: @mcollina checks on node v14 on mac and node v20 on windows are failing. Can see other merges with these fails too. Any known issues ?
mcollina: Drop node 14 and v16 support, I'll bump
the major.

windows is likely flaky.
karankraina: Should I add v22 ?
mcollina: yes please
karankraina: Done but windows still not happy. I think we need another issue to address that. And if this looks good, can we merge it?
karankraina: @mcollina 
mcollina: I'll look into the windows tests asap.

Patch:
diff --git a/README.md b/README.md
index dac38ed..144f5e6 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ Available options:
         Start the command listed after -- on the command line. When it starts listening on a port,
         start sending requests to that port. A URL is still required to send requests to
         the correct path. The hostname can be omitted, `localhost` will be used by default.
+        If the command after -- is `node <script>`, this flag is optional and assumed to be `true`.
   -m/--method METHOD
         The HTTP method to use. default: 'GET'.
   -t/--timeout NUM
diff --git a/autocannon.js b/autocannon.js
index 8d67285..d50078c 100755
--- a/autocannon.js
+++ b/autocannon.js
@@ -115,6 +115,11 @@ function parseArguments (argvs) {
 
   argv.url = argv._.length > 1 ? argv._ : argv._[0]
 
+  // Assume onPort if `-- node` is provided
+  if (argv['--'][0] === 'node') {
+    argv.onPort = true
+  }
+
   if (argv.onPort) {
     argv.spawn = argv['--']
   }


Test patch:
diff --git a/test/onPort.test.js b/test/onPort.test.js
index 86a7222..d3b86f1 100644
--- a/test/onPort.test.js
+++ b/test/onPort.test.js
@@ -59,3 +59,57 @@ test('--on-port flag', { skip: !hasAsyncHooks() }, (t) => {
       t.ok(regexp.test(line), 'line matches ' + regexp)
     })
 })
+
+test('assume --on-port flag if -- node is set', { skip: !hasAsyncHooks() }, (t) => {
+  const lines = [
+    /Running 1s test @ .*$/,
+    /10 connections.*$/,
+    /$/,
+    /.*/,
+    /$/,
+    /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
+    /.*/,
+    /Latency.*$/,
+    /$/,
+    /.*/,
+    /Stat.*1%.*2\.5%.*50%.*97\.5%.*Avg.*Stdev.*Min.*$/,
+    /.*/,
+    /Req\/Sec.*$/,
+    /$/,
+    /Bytes\/Sec.*$/,
+    /.*/,
+    /$/,
+    /Req\/Bytes counts sampled once per second.*$/,
+    /# of samples: 10*$/,
+    /$/,
+    /.* requests in ([0-9]|\.)+s, .* read/
+  ]
+
+  t.plan(lines.length * 2)
+
+  const child = spawn(process.execPath, [
+    path.join(__dirname, '..'),
+    '-c', '10',
+    '-d', '1',
+    '/',
+    '--', 'node', path.join(__dirname, './targetProcess')
+  ], {
+    cwd: __dirname,
+    env: process.env,
+    stdio: ['ignore', 'pipe', 'pipe'],
+    detached: false
+  })
+
+  t.teardown(() => {
+    child.kill()
+  })
+
+  child
+    .stderr
+    .pipe(split())
+    .on('data', (line) => {
+      const regexp = lines.shift()
+      t.ok(regexp, 'we are expecting this line')
+      t.ok(regexp.test(line), 'line matches ' + regexp + `actual: ${line}  expected: ${regexp}`)
+    })
+})