forked from fastify/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark-compare.js
155 lines (143 loc) · 4.88 KB
/
benchmark-compare.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
#!/usr/bin/env node
'use strict'
const inquirer = require('inquirer')
const chalk = require('chalk')
const Table = require('cli-table')
const { join } = require('path')
const { readdirSync, readFileSync } = require('fs')
const { compare } = require('./lib/autocannon')
const { info } = require('./lib/packages')
const commander = require('commander')
commander
.option('-t, --table', 'table')
.option('-p, --percentage', 'percentage')
.option('-c --commandlineMdTable', 'Print a table for use in MarkDown')
.parse(process.argv)
const resultsPath = join(process.cwd(), 'results')
let choices = readdirSync(resultsPath)
.filter((file) => file.match(/(.+)\.json$/))
.sort()
.map((choice) => choice.replace('.json', ''))
const bold = (writeBold, str) => writeBold ? chalk.bold(str) : str
if (!choices.length) {
console.log(chalk.red('Benchmark to gather some results to compare.'))
} else if (commander.table && !commander.percentage) {
const tableSeparatorChars = commander.commandlineMdTable
? {
top: '',
'top-left': '',
'top-mid': '',
'top-right': '',
bottom: '',
'bottom-left': '',
'bottom-mid': '',
'bottom-right': '',
mid: '',
'left-mid': '',
'mid-mid': '',
'right-mid': '',
left: '|',
right: '|',
middle: '|'
}
: {}
const table = new Table({
chars: tableSeparatorChars,
head: ['', 'Router', 'Requests/s', 'Latency', 'Throughput/Mb']
})
if (commander.commandlineMdTable) {
table.push([':--', '--:', ':-:', '--:', '--:'])
}
choices.map(file => {
let content = readFileSync(`${resultsPath}/${file}.json`)
return JSON.parse(content.toString())
})
.sort((a, b) => {
return parseFloat(b.requests.mean) - parseFloat(a.requests.mean)
})
.forEach((data) => {
const beBold = data.server === 'fastify'
const { hasRouter = false } = info(data.server) || {}
const {
requests: { average: requests },
latency: { average: latency },
throughput: { average: throughput }
} = data
table.push([
bold(beBold, chalk.blue(data.server)),
bold(beBold, hasRouter ? '✓' : '✗'),
bold(beBold, requests ? requests.toFixed(1) : 'N/A'),
bold(beBold, latency ? latency.toFixed(2) : 'N/A'),
bold(beBold, throughput ? (throughput / 1024 / 1024).toFixed(2) : 'N/A')
])
})
console.log(table.toString())
} else if (commander.percentage) {
let data = []
choices.forEach(file => {
let content = readFileSync(`${resultsPath}/${file}.json`)
data.push(JSON.parse(content.toString()))
})
data.sort((a, b) => {
return parseFloat(b.requests.mean) - parseFloat(a.requests.mean)
})
const base = Object.assign({}, {
name: data[0].server,
request: data[0].requests.mean,
latency: data[0].latency.mean,
throughput: data[0].throughput.mean
})
const table = new Table({
head: [
'',
'Router',
`Requests/s\n(% of ${base.name})`,
`Latency\n(% of ${base.name})`,
`Throughput/Mb\n(% of ${base.name})`
]
})
data.forEach(result => {
const beBold = result.server === 'fastify'
const { hasRouter = false } = info(result.server) || {}
const getPct = (base, value) => ((value / base * 100).toFixed(2))
table.push([
bold(beBold, chalk.blue(result.server)),
bold(beBold, hasRouter ? '✓' : '✗'),
bold(beBold, `${result.requests.mean}\n(${getPct(base.request, result.requests.mean)})`),
bold(beBold, `${result.latency.mean}\n(${getPct(base.latency, result.latency.mean)})`),
bold(beBold, `${(result.throughput.mean / 1024 / 1024).toFixed(2)}\n(${getPct(base.throughput, result.throughput.mean)})`)
])
})
console.log(table.toString())
} else {
inquirer.prompt([{
type: 'list',
name: 'choice',
message: 'What\'s your first pick?',
choices
}]).then((firstChoice) => {
choices = choices.filter(choice => choice !== firstChoice.choice)
inquirer.prompt([{
type: 'list',
name: 'choice',
message: 'What\'s your second one?',
choices
}]).then((secondChoice) => {
const [a, b] = [firstChoice.choice, secondChoice.choice]
const result = compare(a, b)
if (result === true) {
console.log(chalk.green.bold(`${a} and ${b} both are fast!`))
} else {
const fastest = chalk.bold.yellow(result.fastest)
const fastestAverage = chalk.green(result.fastestAverage)
const slowest = chalk.bold.yellow(result.slowest)
const slowestAverage = chalk.green(result.slowestAverage)
const diff = chalk.bold.green(result.diff)
console.log(`
${chalk.blue('Both are awesome but')} ${fastest} ${chalk.blue('is')} ${diff} ${chalk.blue('faster than')} ${slowest}
• ${fastest} ${chalk.blue('request average is')} ${fastestAverage}
• ${slowest} ${chalk.blue('request average is')} ${slowestAverage}`)
}
})
})
}