Repository: mcollina/autocannon
Pull request #380 (2021-07-06 18:26:56)

fix(report): align printResult to output the correct total requests
Closes https://github.com/mcollina/autocannon/issues/228

mcollina: I'm not sure this is the correct fix, as the additional requests are done. This PR is just hiding them.
elirandav: > I'm not sure this is the correct fix, as the additional requests are done. This PR is just hiding them.

correct @mcollina . 
we need to use the `result` and not `opts`. I aligned the PR. 

In the scenario I am fixing, all the requests were sent to the tested server, only the output was wrong: 10 requests instead of 20 requests. I verified it with my local server - it receives all 20 requests.  

Patch:
diff --git a/lib/printResult.js b/lib/printResult.js
index 50e03ad..c1d7216 100644
--- a/lib/printResult.js
+++ b/lib/printResult.js
@@ -69,7 +69,7 @@ const printResult = (result, opts) => {
   if (result.non2xx) {
     logToLocalStr(`${result['2xx']} 2xx responses, ${result.non2xx} non 2xx responses`)
   }
-  logToLocalStr(`${format(result.requests.total)} requests in ${result.duration}s, ${prettyBytes(result.throughput.total)} read`)
+  logToLocalStr(`${format(result.requests.sent)} requests in ${result.duration}s, ${prettyBytes(result.throughput.total)} read`)
   if (result.errors) {
     logToLocalStr(`${format(result.errors)} errors (${format(result.timeouts)} timeouts)`)
   }


Test patch:
diff --git a/test/printResult.test.js b/test/printResult.test.js
index ccc81ec..ba2d3c1 100644
--- a/test/printResult.test.js
+++ b/test/printResult.test.js
@@ -1,50 +1,97 @@
-'use strict'
-
-const test = require('tap').test
-const split = require('split2')
-const path = require('path')
-const childProcess = require('child_process')
-
-test('should stdout (print) the result', (t) => {
-  const lines = [
-    /.*/,
-    /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.*$/,
-    /$/,
-    /.* requests in ([0-9]|\.)+s, .* read/
-  ]
-
-  t.plan(lines.length * 2)
-
-  const child = childProcess.spawn(process.execPath, [path.join(__dirname, 'printResult-process.js')], {
-    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)
-    })
-    .on('end', t.end)
-})
+'use strict'
+
+const test = require('tap').test
+const split = require('split2')
+const path = require('path')
+const childProcess = require('child_process')
+const printResult = require('../lib/printResult')
+
+test('should stdout (print) the result', (t) => {
+  const lines = [
+    /.*/,
+    /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.*$/,
+    /$/,
+    /.* requests in ([0-9]|\.)+s, .* read/
+  ]
+
+  t.plan(lines.length * 2)
+
+  const child = childProcess.spawn(process.execPath, [path.join(__dirname, 'printResult-process.js')], {
+    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)
+    })
+    .on('end', t.end)
+})
+
+test('verify amount of total requests', (t) => {
+  t.plan(1)
+
+  // arrange
+  const connections = 10
+  const pipelining = 2
+  const result = {
+    connections,
+    pipelining,
+    latency: {},
+    requests: {
+      sent: connections * pipelining
+    },
+    throughput: {
+      average: 3319,
+      mean: 3319,
+      stddev: 0,
+      min: 3318,
+      max: 3318,
+      total: 3318,
+      p0_001: 3319,
+      p0_01: 3319,
+      p0_1: 3319,
+      p1: 3319,
+      p2_5: 3319,
+      p10: 3319,
+      p25: 3319,
+      p50: 3319,
+      p75: 3319,
+      p90: 3319,
+      p97_5: 3319,
+      p99: 3319,
+      p99_9: 3319,
+      p99_99: 3319,
+      p99_999: 3319
+    }
+  }
+
+  // act
+  const output = printResult(result, { })
+
+  // assert
+  const expectedRequests = connections * pipelining
+  t.match(output.includes(`${expectedRequests} requests in`), true)
+})