-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbenchmark.js
More file actions
141 lines (116 loc) · 3.39 KB
/
Copy pathbenchmark.js
File metadata and controls
141 lines (116 loc) · 3.39 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
const { readFileSync } = require(`fs`);
const path = require(`path`);
const newBenchmark = JSON.parse(
readFileSync(path.join(__dirname, `public`, `benchmark.json`), `utf8`),
);
const twoDecimal = (value) => Number.parseFloat(value.toFixed(2));
const currentBenchmark = () => {
return fetch(`https://soft-pithivier-093469.netlify.app/benchmark.json`)
.then((response) => {
if (!response.ok) {
console.warn(`Failed to fetch data - new benchmark will be applied`);
return newBenchmark;
}
return response.json();
})
.then((benchmark) => {
return benchmark;
})
.catch(() => {
console.warn(`Unhandled error - new benchmark will be applied`);
return newBenchmark;
});
};
currentBenchmark().then((currentBenchmark) => {
const logSetup = () => {
console.log(`@@@ Setup @@@`);
console.table(newBenchmark.limits);
};
const logCurrentBenchmark = () => {
console.log(`@@@ Current benchmark @@@`);
console.table(
Object.entries(currentBenchmark.chunks).reduce(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(acc, [key, { sizes, ...values }]) => {
acc[key] = values;
return acc;
},
{},
),
);
console.table({
sum: currentBenchmark.totalSize,
});
};
const logNewBenchmark = () => {
console.log(`@@@ New benchmark @@@`);
console.table(
Object.entries(newBenchmark.chunks).reduce(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(acc, [key, { sizes, ...values }]) => {
acc[key] = values;
return acc;
},
{},
),
);
console.table({
sum: newBenchmark.totalSize,
});
};
const logDifferences = () => {
console.log(`@@@ Differences @@@`);
const sumDiff = twoDecimal(
newBenchmark.totalSize - currentBenchmark.totalSize,
);
const chunksDiff = Object.entries(newBenchmark.chunks).reduce(
(acc, [key, value]) => {
if (currentBenchmark.chunks[key] === undefined) {
acc[key] = {
sizesAsString: value.sizesAsString,
totalSize: `+${value.totalSize}`,
};
return acc;
}
if (value.totalSize !== currentBenchmark.chunks[key].totalSize) {
const totalChunksGroupDiff = twoDecimal(
value.totalSize - currentBenchmark.chunks[key].totalSize,
);
acc[key] = {
sizesAsString: value.sizesAsString,
totalSize:
totalChunksGroupDiff > 0
? `+${totalChunksGroupDiff}`
: `${totalChunksGroupDiff}`,
};
}
return acc;
},
{},
);
if (Object.keys(chunksDiff).length > 0) {
console.table(chunksDiff);
}
const sum =
sumDiff === 0 ? `=` : sumDiff > 0 ? `+${sumDiff}` : `${sumDiff}`;
console.table({
sum,
});
};
const verifyBenchmark = () => {
if (newBenchmark.failedChunkGroups.length > 0) {
throw Error(
`
Benchmark check failed - limit is ${newBenchmark.limits.chunk}${newBenchmark.limits.unit} per chunk.
Here you've list of failed chunk groups:
${newBenchmark.failedChunkGroups.join(`,`)}
`,
);
}
};
logSetup();
logCurrentBenchmark();
logNewBenchmark();
logDifferences();
verifyBenchmark();
});