Skip to content

Instantly share code, notes, and snippets.

@micalevisk
Created September 14, 2021 16:56
Show Gist options
  • Save micalevisk/cc012249d2bb4511ef3d80a8a5d1a5ce to your computer and use it in GitHub Desktop.
Save micalevisk/cc012249d2bb4511ef3d80a8a5d1a5ce to your computer and use it in GitHub Desktop.
Comparing Nodejs HTTP server with 'Keep-Alive' header vs without it
#!/bin/bash
URL="http://localhost:3001"
// https://github.com/mcollina/autocannon
autocannon "$URL" \
--connections 100 \
--timeout 20 \
--pipelining 1 \
--amount 2000
Running 2000 requests test @ http://localhost:3001
100 connections
┌─────────┬──────┬──────┬───────┬───────┬──────────┬────────┬───────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼──────┼──────┼───────┼───────┼──────────┼────────┼───────┤
│ Latency │ 6 ms │ 9 ms │ 44 ms │ 44 ms │ 12.48 ms │ 8.3 ms │ 44 ms │
└─────────┴──────┴──────┴───────┴───────┴──────────┴────────┴───────┘
┌───────────┬────────┬────────┬────────┬────────┬────────┬───────┬────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼────────┼────────┼────────┼────────┼────────┼───────┼────────┤
│ Req/Sec │ 2000 │ 2000 │ 2000 │ 2000 │ 2000 │ 0 │ 2000 │
├───────────┼────────┼────────┼────────┼────────┼────────┼───────┼────────┤
│ Bytes/Sec │ 378 kB │ 378 kB │ 378 kB │ 378 kB │ 378 kB │ 0 B │ 378 kB │
└───────────┴────────┴────────┴────────┴────────┴────────┴───────┴────────┘
Req/Bytes counts sampled once per second.
2k requests in 1.03s, 378 kB read
Running 2000 requests test @ http://localhost:3001
100 connections
┌─────────┬───────┬───────┬────────┬────────┬─────────┬──────────┬────────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼───────┼───────┼────────┼────────┼─────────┼──────────┼────────┤
│ Latency │ 32 ms │ 91 ms │ 123 ms │ 124 ms │ 82.8 ms │ 29.34 ms │ 127 ms │
└─────────┴───────┴───────┴────────┴────────┴─────────┴──────────┴────────┘
┌───────────┬────────┬────────┬────────┬────────┬────────┬───────┬────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼────────┼────────┼────────┼────────┼────────┼───────┼────────┤
│ Req/Sec │ 1000 │ 1000 │ 1000 │ 1000 │ 1000 │ 0 │ 1000 │
├───────────┼────────┼────────┼────────┼────────┼────────┼───────┼────────┤
│ Bytes/Sec │ 106 kB │ 106 kB │ 106 kB │ 106 kB │ 106 kB │ 0 B │ 106 kB │
└───────────┴────────┴────────┴────────┴────────┴────────┴───────┴────────┘
Req/Bytes counts sampled once per second.
2k requests in 1.03s, 106 kB read
var http = require('http')
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain', 'Keep-Alive': 'timeout=60, max=1000'})
res.end('Hello World\n')
}).on('connection', function(socket) {
socket.setTimeout(1000 * 1 * 60); // 1 minute of timeout
}).listen(3001)
var http = require('http')
http.createServer(function (req, res) {
res.shouldKeepAlive = false
res.end('Hello World\n')
}).on('connection', function(socket) {
}).listen(3001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment