forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-benchmarks.ts
More file actions
149 lines (129 loc) · 3.68 KB
/
Copy pathcheck-benchmarks.ts
File metadata and controls
149 lines (129 loc) · 3.68 KB
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
import { codechecks, CodeChecksReport } from '@codechecks/client';
import * as bytes from 'bytes';
import { Benchmarks, getBenchmarks, LIBS } from './get-benchmarks';
const markdownTable = require('markdown-table');
const benchmarksKey = 'nest/performance-benchmark';
export default async function checkBenchmarks() {
const currentBenchmarks = await getBenchmarks();
await codechecks.saveValue(benchmarksKey, currentBenchmarks);
if (!codechecks.isPr()) {
return;
}
const baselineBenchmarks = await codechecks.getValue<Benchmarks>(
benchmarksKey,
);
const report = getCodechecksReport(currentBenchmarks, baselineBenchmarks);
await codechecks.report(report);
}
function getCodechecksReport(
current: Benchmarks,
baseline: Benchmarks | undefined,
): CodeChecksReport {
const diff = getDiff(current, baseline);
const shortDescription = getShortDescription(baseline, diff);
const longDescription = getLongDescription(current, baseline, diff);
return {
name: 'Benchmarks',
status: 'success',
shortDescription,
longDescription,
};
}
function getShortDescription(
baseline: Benchmarks | undefined,
diff: BenchmarksDiff,
): string {
if (!baseline) {
return 'New benchmarks generated';
}
const avgDiff = getAverageDiff(diff);
if (avgDiff > 0) {
return `Performance improved by ${avgDiff.toFixed(
2,
)}% on average, good job!`;
}
if (avgDiff === 0) {
return `No changes in performance detected`;
}
if (avgDiff < 0) {
return `Performance decreased by ${avgDiff.toFixed(
2,
)}% on average, be careful!`;
}
}
function getLongDescription(
current: Benchmarks,
baseline: Benchmarks | undefined,
diff: BenchmarksDiff,
): string {
function printTableRow(id: string, label: string): string[] {
return [
label,
current[id].requestsPerSec.toFixed(0),
current[id].transferPerSec,
baseline ? formatPerc(diff[id].requestsPerSecDiff) : '-',
baseline ? formatPerc(diff[id].transferPerSecDiff) : '-',
];
}
const table = [
['', 'Req/sec', 'Trans/sec', 'Req/sec DIFF', 'Trans/sec DIFF'],
printTableRow('nest', 'Nest-Express'),
printTableRow('nest-fastify', 'Nest-Fastify'),
printTableRow('express', 'Express'),
printTableRow('fastify', 'Fastify'),
];
return markdownTable(table);
}
function getDiff(
current: Benchmarks,
baseline: Benchmarks | undefined,
): BenchmarksDiff {
const diff: BenchmarksDiff = {};
for (const l of LIBS) {
if (!baseline) {
diff[l] = undefined;
continue;
}
const currentValue = current[l];
const baselineValue = baseline[l];
diff[l] = {
requestsPerSecDiff: getRequestDiff(
currentValue.requestsPerSec,
baselineValue.requestsPerSec,
),
transferPerSecDiff: getTransferDiff(
currentValue.transferPerSec,
baselineValue.transferPerSec,
),
};
}
return diff;
}
function getTransferDiff(
currentTransfer: string,
baselineTransfer: string,
): number {
return 1 - bytes.parse(currentTransfer) / bytes.parse(baselineTransfer);
}
function getAverageDiff(diff: BenchmarksDiff) {
return (
(diff['nest'].transferPerSecDiff +
diff['nest'].requestsPerSecDiff +
diff['nest-fastify'].transferPerSecDiff +
diff['nest-fastify'].requestsPerSecDiff) /
4
);
}
function getRequestDiff(currentRequest: number, baselineRequest: number) {
return 1 - currentRequest / baselineRequest;
}
interface BenchmarkDiff {
transferPerSecDiff: number | undefined;
requestsPerSecDiff: number | undefined;
}
interface BenchmarksDiff {
[lib: string]: BenchmarkDiff;
}
function formatPerc(n: number) {
return (n > 0 ? '+' : '') + (n * 100).toFixed(2) + '%';
}