forked from fastify/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 84
/
benchmark-compare.js
191 lines (179 loc) · 5.47 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
const chalk = require("chalk");
const Table = require("cli-table");
const { join } = require("path");
const { readdirSync, readFileSync } = require("fs");
const { program } = require("commander");
const { compare } = require("./lib/autocannon");
program
.option("-t, --table", "table")
.option("-p, --percentage", "percentage")
.option("-c --commandlineMdTable", "Print a table for use in MarkDown")
.parse(process.argv);
const options = program.opts();
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 (options.table && !options.percentage) {
const tableSeparatorChars = options.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: ["Server", "Requests/s", "Latency", "Throughput/Mb"],
});
if (options.commandlineMdTable) {
table.push([":--", "--:", ":-:", "--:"]);
}
const dataArray = [];
choices.forEach((file) => {
const content = readFileSync(`${resultsPath}/${file}.json`);
dataArray.push(JSON.parse(content.toString()));
});
dataArray.sort(
(a, b) => parseFloat(b.requests.mean) - parseFloat(a.requests.mean),
);
dataArray.forEach((data, i) => {
if (i === 0) {
console.log(
`duration: ${data.duration}s\nconnections: ${data.connections}\npipelining: ${data.pipelining}`,
);
console.log("");
}
const beBold = false;
table.push([
bold(
beBold,
chalk.blue(
options.commandlineMdTable
? `[${data.server}](https://github.com/benawad/node-graphql-benchmarks/tree/master/benchmarks/${data.server}.js)`
: data.server,
),
),
bold(beBold, data.requests.average.toFixed(1)),
bold(beBold, data.latency.average.toFixed(2)),
bold(beBold, (data.throughput.average / 1024 / 1024).toFixed(2)),
]);
});
console.log(table.toString());
} else if (options.percentage) {
const data = [];
choices.forEach((file) => {
const content = readFileSync(`${resultsPath}/${file}.json`);
data.push(JSON.parse(content.toString()));
});
data.sort(
(a, b) => parseFloat(b.requests.mean) - parseFloat(a.requests.mean),
);
const base = {
name: data[0].server,
request: data[0].requests.mean,
latency: data[0].latency.mean,
throughput: data[0].throughput.mean,
};
const table = new Table({
head: [
"Server",
`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 getPct = (base, value) => ((value / base) * 100).toFixed(2);
table.push([
bold(beBold, chalk.blue(result.server)),
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 {
async function getInquirer() {
const { default: inquirer } = await import("inquirer");
return inquirer;
}
getInquirer().then((inquirer) => {
return 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}`);
}
});
});
});
}