forked from mcollina/autocannon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocannon.js
executable file
·138 lines (118 loc) · 3.02 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#! /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
module.exports.start = start
module.exports.parseArguments = parseArguments
function parseArguments (argvs) {
const argv = minimist(argvs, {
boolean: ['json', 'n', 'help', 'renderLatencyTable', 'renderProgressBar', 'forever', 'idReplacement', 'excludeErrorStats'],
alias: {
connections: 'c',
pipelining: 'p',
timeout: 't',
duration: 'd',
amount: 'a',
json: 'j',
renderLatencyTable: ['l', 'latency'],
method: 'm',
headers: ['H', 'header'],
body: 'b',
servername: 's',
bailout: 'B',
input: 'i',
maxConnectionRequests: 'M',
maxOverallRequests: 'O',
connectionRate: 'r',
overallRate: 'R',
reconnectRate: 'D',
renderProgressBar: 'progress',
title: 'T',
version: 'v',
forever: 'f',
idReplacement: 'I',
socketPath: 'S',
excludeErrorStats: 'x',
help: 'h'
},
default: {
connections: 10,
timeout: 10,
pipelining: 1,
duration: 10,
reconnectRate: 0,
renderLatencyTable: false,
renderProgressBar: true,
json: false,
forever: false,
method: 'GET',
idReplacement: false,
excludeErrorStats: false
}
})
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) => {
let index
if (
(index = header.indexOf('=')) > 0 ||
(index = header.indexOf(':')) > 0
) {
obj[header.slice(0, index)] = header.slice(index + 1)
return obj
} else throw new Error(`An HTTP header was not correctly formatted: ${header}`)
}, {})
}
return argv
}
function start (argv) {
if (!argv) {
// we are printing the help
return
}
const tracker = run(argv)
tracker.on('done', (result) => {
if (argv.json) {
console.log(JSON.stringify(result))
}
})
tracker.on('error', (err) => {
if (err) {
throw err
}
})
// 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(parseArguments(process.argv.slice(2)))
}