Repository: mcollina/autocannon
Pull request #438 (2022-03-26 18:30:04)

fix: printing of result when interrupted
Hello,

I just noticed that whenever I forcefully (CTRL-C) interrupt the execution of this script
```javascript
'use strict'

const autocannon = require('autocannon')

const instance = autocannon({
  url: 'http://localhost:3000'
})

// this is used to kill the instance on CTRL-C
process.once('SIGINT', () => {
  instance.stop()
})

// just render results
autocannon.track(instance, {renderProgressBar: false})
```

the log in the console is a little broken because the first row isn't being written on a new line.

Example:
```
^C┌─────────┬──────┬──────┬───────┬──────┬────────┬─────────┬────────┐
│ Stat    │ 2.5% │ 50%  │ 97.5% │ 99%  │ Avg    │ Stdev   │ Max    │
├─────────┼──────┼──────┼───────┼──────┼────────┼─────────┼────────┤
│ Latency │ 1 ms │ 2 ms │ 5 ms  │ 7 ms │ 2.2 ms │ 5.52 ms │ 230 ms │
└─────────┴──────┴──────┴───────┴──────┴────────┴─────────┴────────┘
┌───────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐
│ Stat      │ 1%     │ 2.5%   │ 50%    │ 97.5%  │ Avg    │ Stdev  │ Min    │
├───────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤
│ Req/Sec   │ 2657   │ 2657   │ 3905   │ 4055   │ 3660.5 │ 582.09 │ 2657   │
├───────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤
│ Bytes/Sec │ 497 kB │ 497 kB │ 730 kB │ 758 kB │ 684 kB │ 109 kB │ 497 kB │
└───────────┴────────┴────────┴────────┴────────┴────────┴────────┴────────┘

Req/Bytes counts sampled once per second.
# of samples: 4

15k requests in 4.03s, 2.74 MB read
```

I thought this fix could make sense for all use cases so I decided to just send a pull request to fix it globally.

ps. This is my first pull request ever. Hope I'm not doing anything wrong! 🙈

claudio-di-sciacca: I think i fixed the failing tests btw. Do I need to do something else to close this?
mcollina: Still lgtm

Patch:
diff --git a/.gitignore b/.gitignore
index dffbde9..933e16f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -133,3 +133,5 @@ yarn.lock
 build/Release
 
 profile-*
+
+.devcontainer
\ No newline at end of file
diff --git a/lib/printResult.js b/lib/printResult.js
index 20abcc5..279fd94 100644
--- a/lib/printResult.js
+++ b/lib/printResult.js
@@ -24,7 +24,7 @@ const printResult = (result, opts) => {
     head: asColor(chalk.cyan, ['Stat', '2.5%', '50%', '97.5%', '99%', 'Avg', 'Stdev', 'Max'])
   })
   shortLatency.push(asLowRow(chalk.bold('Latency'), asMs(result.latency)))
-  logToLocalStr(shortLatency.toString())
+  logToLocalStr('\n' + shortLatency.toString())
 
   const requests = new Table({
     head: asColor(chalk.cyan, ['Stat', '1%', '2.5%', '50%', '97.5%', 'Avg', 'Stdev', 'Min'])


Test patch:
diff --git a/test/basic-auth.test.js b/test/basic-auth.test.js
index 8213582..c80ca61 100644
--- a/test/basic-auth.test.js
+++ b/test/basic-auth.test.js
@@ -11,6 +11,7 @@ const lines = [
   /10 connections.*$/,
   /$/,
   /.*/,
+  /$/,
   /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
   /.*/,
   /Latency.*$/,
diff --git a/test/cli-ipc.test.js b/test/cli-ipc.test.js
index 6db948b..49ce1eb 100644
--- a/test/cli-ipc.test.js
+++ b/test/cli-ipc.test.js
@@ -14,6 +14,7 @@ const lines = [
   /10 connections.*$/,
   /$/,
   /.*/,
+  /$/,
   /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
   /.*/,
   /Latency.*$/,
diff --git a/test/cli.test.js b/test/cli.test.js
index 1de44b3..3ef17b0 100644
--- a/test/cli.test.js
+++ b/test/cli.test.js
@@ -15,6 +15,7 @@ test('should run benchmark against server', (t) => {
     /10 connections.*$/,
     /$/,
     /.*/,
+    /$/,
     /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
     /.*/,
     /Latency.*$/,
@@ -66,6 +67,7 @@ test('should parse HAR file and run requests', (t) => {
     /1 connections.*$/,
     /$/,
     /.*/,
+    /$/,
     /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
     /.*/,
     /Latency.*$/,
@@ -207,6 +209,7 @@ test('run with workers', { skip: !hasWorkerSupport }, (t) => {
     /4 workers.*$/,
     /$/,
     /.*/,
+    /$/,
     /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
     /.*/,
     /Latency.*$/,
diff --git a/test/envPort.test.js b/test/envPort.test.js
index 525a240..2f04b19 100644
--- a/test/envPort.test.js
+++ b/test/envPort.test.js
@@ -11,6 +11,7 @@ const lines = [
   /10 connections.*$/,
   /$/,
   /.*/,
+  /$/,
   /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
   /.*/,
   /Latency.*$/,
diff --git a/test/onPort.test.js b/test/onPort.test.js
index 4a5fd64..86a7222 100644
--- a/test/onPort.test.js
+++ b/test/onPort.test.js
@@ -12,6 +12,7 @@ test('--on-port flag', { skip: !hasAsyncHooks() }, (t) => {
     /10 connections.*$/,
     /$/,
     /.*/,
+    /$/,
     /Stat.*2\.5%.*50%.*97\.5%.*99%.*Avg.*Stdev.*Max.*$/,
     /.*/,
     /Latency.*$/,
diff --git a/test/printResult-renderStatusCodes.test.js b/test/printResult-renderStatusCodes.test.js
index 6516d9f..7aa9086 100644
--- a/test/printResult-renderStatusCodes.test.js
+++ b/test/printResult-renderStatusCodes.test.js
@@ -8,6 +8,7 @@ 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.*$/,
diff --git a/test/printResult.test.js b/test/printResult.test.js
index 4c2a89e..1341643 100644
--- a/test/printResult.test.js
+++ b/test/printResult.test.js
@@ -9,6 +9,7 @@ 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.*$/,