/
printResult.js
179 lines (154 loc) · 5.1 KB
/
printResult.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict'
const Table = require('cli-table3')
const Chalk = require('chalk')
const testColorSupport = require('color-support')
const prettyBytes = require('pretty-bytes')
const { percentiles } = require('./histUtil')
const format = require('./format')
const defaults = {
// use stderr as its progressBar's default
outputStream: process.stderr,
renderResultsTable: true,
renderLatencyTable: false,
verbose: true
}
class TableWithoutColor extends Table {
constructor (opts = {}) {
super({ ...opts, style: { head: [], border: [] } })
}
}
const printResult = (result, opts) => {
opts = Object.assign({}, defaults, opts)
let strResult = ''
if (opts.verbose) {
const chalk = new Chalk.Instance(testColorSupport({ stream: opts.outputStream, alwaysReturn: true }))
const ColorSafeTable = chalk.level === 0 ? TableWithoutColor : Table
const shortLatency = new ColorSafeTable({
head: asColor(chalk.cyan, ['Stat', '2.5%', '50%', '97.5%', '99%', 'Avg', 'Stdev', 'Max'])
})
shortLatency.push(asLowRow(chalk.bold('Latency'), asMs(result.latency)))
logToLocalStr('\n' + shortLatency.toString())
const requests = new ColorSafeTable({
head: asColor(chalk.cyan, ['Stat', '1%', '2.5%', '50%', '97.5%', 'Avg', 'Stdev', 'Min'])
})
requests.push(asHighRow(chalk.bold('Req/Sec'), asNumber(result.requests)))
requests.push(asHighRow(chalk.bold('Bytes/Sec'), asBytes(result.throughput)))
logToLocalStr(requests.toString())
if (opts.renderStatusCodes === true) {
const statusCodeStats = new ColorSafeTable({
head: asColor(chalk.cyan, ['Code', 'Count'])
})
Object.keys(result.statusCodeStats).forEach(statusCode => {
const stats = result.statusCodeStats[statusCode]
const colorize = colorizeByStatusCode(chalk, statusCode)
statusCodeStats.push([colorize(statusCode), stats.count])
})
logToLocalStr(statusCodeStats.toString())
}
logToLocalStr('')
if (result.sampleInt === 1000) {
logToLocalStr('Req/Bytes counts sampled once per second.')
} else {
logToLocalStr('Req/Bytes counts sampled every ' + result.sampleInt / 1000 + ' seconds.')
}
logToLocalStr('# of samples: ' + result.samples)
logToLocalStr('')
if (opts.renderLatencyTable) {
const latencies = new ColorSafeTable({
head: asColor(chalk.cyan, ['Percentile', 'Latency (ms)'])
})
percentiles.map((perc) => {
const key = `p${perc}`.replace('.', '_')
return [
chalk.bold('' + perc),
result.latency[key]
]
}).forEach(row => {
latencies.push(row)
})
logToLocalStr(latencies.toString())
logToLocalStr('')
}
}
if (result.non2xx) {
logToLocalStr(`${result['2xx']} 2xx responses, ${result.non2xx} non 2xx responses`)
}
logToLocalStr(`${format(result.requests.sent)} requests in ${result.duration}s, ${prettyBytes(result.throughput.total)} read`)
if (result.errors) {
logToLocalStr(`${format(result.errors)} errors (${format(result.timeouts)} timeouts)`)
}
if (result.mismatches) {
logToLocalStr(`${format(result.mismatches)} requests with mismatched body`)
}
if (result.resets) {
logToLocalStr(`request pipeline was reset ${format(result.resets)} ${result.resets === 1 ? 'time' : 'times'}`)
}
function logToLocalStr (msg) {
strResult += msg + '\n'
}
return strResult
}
// create a table row for stats where low values is better
function asLowRow (name, stat) {
return [
name,
stat.p2_5,
stat.p50,
stat.p97_5,
stat.p99,
stat.average,
stat.stddev,
typeof stat.max === 'string' ? stat.max : Math.floor(stat.max * 100) / 100
]
}
// create a table row for stats where high values is better
function asHighRow (name, stat) {
return [
name,
stat.p1,
stat.p2_5,
stat.p50,
stat.p97_5,
stat.average,
stat.stddev,
typeof stat.min === 'string' ? stat.min : Math.floor(stat.min * 100) / 100
]
}
function asColor (colorise, row) {
return row.map((entry) => colorise(entry))
}
function asMs (stat) {
const result = Object.create(null)
for (const k of Object.keys(stat)) {
result[k] = `${stat[k]} ms`
}
result.max = typeof stat.max === 'string' ? stat.max : `${Math.floor(stat.max * 100) / 100} ms`
return result
}
function asNumber (stat) {
const result = Object.create(null)
for (const k of Object.keys(stat)) {
result[k] = stat[k].toLocaleString(undefined, {
// to show all digits
maximumFractionDigits: 20
})
}
return result
}
function asBytes (stat) {
const result = Object.create(stat)
for (const p of percentiles) {
const key = `p${p}`.replace('.', '_')
result[key] = prettyBytes(stat[key])
}
result.average = prettyBytes(stat.average)
result.stddev = prettyBytes(stat.stddev)
result.max = prettyBytes(stat.max)
result.min = prettyBytes(stat.min)
return result
}
function colorizeByStatusCode (chalk, statusCode) {
const codeClass = Math.floor(parseInt(statusCode) / 100) - 1
return [chalk.cyan, chalk.cyan, chalk.cyan, chalk.redBright, chalk.redBright][codeClass]
}
module.exports = printResult