forked from mcollina/autocannon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocannon.js
executable file
·129 lines (109 loc) · 2.73 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
#! /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'],
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',
forever: 'f',
idReplacement: 'I',
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
}
})
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 index = header.indexOf('=')
obj[header.slice(0, index)] = header.slice(index + 1)
return obj
}, {})
}
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)))
}