forked from mcollina/autocannon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocannon.js
executable file
·109 lines (93 loc) · 2.31 KB
/
autocannon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /usr/bin/env node
'use strict'
const minimist = require('minimist')
const fs = require('fs')
const path = require('path')
const help = fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf8')
const run = require('./lib/run')
const track = require('./lib/progressTracker')
module.exports = run
module.exports.track = track
function start () {
const argv = minimist(process.argv.slice(2), {
boolean: ['json', 'n', 'help', 'renderLatencyTable', 'renderProgressBar'],
alias: {
connections: 'c',
pipelining: 'p',
timeout: 't',
duration: 'd',
amount: 'a',
json: 'j',
renderLatencyTable: ['l', 'latency'],
method: 'm',
headers: ['H', 'header'],
body: 'b',
bailout: 'B',
input: 'i',
maxConnectionRequests: 'M',
maxOverallRequests: 'O',
connectionRate: 'r',
overallRate: 'R',
reconnectRate: 'D',
renderProgressBar: 'progress',
title: 'T',
version: 'v',
help: 'h'
},
default: {
connections: 10,
timeout: 10,
pipelining: 1,
duration: 10,
reconnectRate: 0,
renderLatencyTable: false,
renderProgressBar: true,
json: false,
method: 'GET'
}
})
argv.url = argv._[0]
// support -n to disable the progress bar and results table
if (argv.n) {
argv.renderProgressBar = false
argv.renderResultsTable = false
}
if (argv.version) {
console.log('autocannon', 'v' + require('./package').version)
console.log('node', process.version)
return
}
if (!argv.url || argv.help) {
console.error(help)
return
}
if (argv.input) {
argv.body = fs.readFileSync(argv.input)
}
if (argv.headers) {
if (!Array.isArray(argv.headers)) {
argv.headers = [argv.headers]
}
argv.headers = argv.headers.reduce((obj, header) => {
const split = header.split('=')
obj[split[0]] = split[1]
return obj
}, {})
}
const tracker = run(argv, (err, result) => {
if (err) {
throw err
}
if (argv.json) {
console.log(JSON.stringify(result))
}
})
// if not rendering json, or if std isn't a tty, track progress
if (!argv.json || !process.stdout.isTTY) track(tracker, argv)
process.once('SIGINT', () => {
tracker.stop()
})
}
if (require.main === module) {
start()
}