forked from fastify/benchmarks
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark-compare.js
122 lines (112 loc) · 4.07 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
#!/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')
.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 table = new Table({
head: ['', 'Version', 'Router', 'Requests/s', 'Latency', 'Throughput/Mb']
})
choices.forEach((result) => {
let data = readFileSync(`${resultsPath}/${result}.json`)
data = JSON.parse(data.toString())
const beBold = result === 'fastify'
const { version = 'N/A', hasRouter = false } = info(result) || {}
table.push([
bold(beBold, chalk.blue(result)),
bold(beBold, version),
bold(beBold, hasRouter ? '✓' : '✗'),
bold(beBold, data.requests.average),
bold(beBold, data.latency.average),
bold(beBold, (data.throughput.average / 1024 / 1024).toFixed(2))
])
})
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: [
'',
'Version',
'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 { version = 'N/A', hasRouter = false } = info(result.server) || {}
const getPct = (base, value) => ((value / base * 100).toFixed(2))
table.push([
bold(beBold, chalk.blue(result.server)),
bold(beBold, version),
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}`)
}
})
})
}