-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.ts
More file actions
209 lines (186 loc) · 5.54 KB
/
index.ts
File metadata and controls
209 lines (186 loc) · 5.54 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Benchmark from 'benchmark';
import type { Report } from '@code-pushup/models';
import { scoreReport } from '../../src/lib/reports/scoring.js';
import { scoreReportOptimized0 } from './optimized0.js';
import { scoreReportOptimized1 } from './optimized1.js';
import { scoreReportOptimized2 } from './optimized2.js';
import { scoreReportOptimized3 } from './optimized3.js';
type MinimalReportOptions = {
numAuditsP1?: number;
numAuditsP2?: number;
numGroupRefs2?: number;
};
const PROCESS_ARGUMENT_NUM_AUDITS_P1 = Number.parseInt(
process.argv
.find(arg => arg.startsWith('--numAudits1'))
?.split('=')
.at(-1) ?? '0',
10,
);
const PROCESS_ARGUMENT_NUM_AUDITS_P2 = Number.parseInt(
process.argv
.find(arg => arg.startsWith('--numAudits2'))
?.split('=')
.at(-1) ?? '0',
10,
);
const PROCESS_ARGUMENT_NUM_GROUPS_P2 = Number.parseInt(
process.argv
.find(arg => arg.startsWith('--numGroupRefs2'))
?.split('=')
.at(-1) ?? '0',
10,
);
const suite = new Benchmark.Suite('report-scoring');
const AUDIT_PREFIX = 'a-';
const GROUP_PREFIX = 'g:';
const PLUGIN_PREFIX = 'p.';
const SLUG_PLUGIN_P1 = PLUGIN_PREFIX + 1;
const AUDIT_P1_PREFIX = AUDIT_PREFIX + SLUG_PLUGIN_P1;
const SLUG_PLUGIN_P2 = PLUGIN_PREFIX + 2;
const AUDIT_P2_PREFIX = AUDIT_PREFIX + SLUG_PLUGIN_P2;
const GROUP_P2_PREFIX = GROUP_PREFIX + SLUG_PLUGIN_P2;
const NUM_AUDITS_P1 = PROCESS_ARGUMENT_NUM_AUDITS_P1 || 27;
const NUM_AUDITS_P2 = PROCESS_ARGUMENT_NUM_AUDITS_P2 || 18;
const NUM_GROUPS_P2 = PROCESS_ARGUMENT_NUM_GROUPS_P2 || NUM_AUDITS_P2 / 2;
// ==================
// Add listener
const listeners = {
cycle(event: Benchmark.Event) {
console.info(String(event.target));
},
complete: () => {
if (typeof suite.filter === 'function') {
console.info(' ');
console.info(`Fastest is ${String(suite.filter('fastest').map('name'))}`);
}
},
};
// ==================
// Add tests
suite.add('scoreReport', scoreReport);
suite.add('scoreReportOptimized0', scoreMinimalReportOptimized0);
suite.add('scoreReportOptimized1', scoreMinimalReportOptimized1);
suite.add('scoreReportOptimized2', scoreMinimalReportOptimized2);
suite.add('scoreReportOptimized3', scoreMinimalReportOptimized3);
// ==================
// Add Listener
Object.entries(listeners).forEach(([name, fn]) => {
suite.on(name, fn);
});
// ==================
console.info('You can adjust the number of runs with the following arguments:');
console.info(
`numAudits1 Number of audits in plugin 1. --numAudits1=${NUM_AUDITS_P1}`,
);
console.info(
`numAudits2 Number of audits in plugin 2. --numAudits2=${NUM_AUDITS_P2}`,
);
console.info(
`numGroupRefs2 Number of groups refs in plugin 2. --numGroupRefs2=${NUM_GROUPS_P2}`,
);
console.info(' ');
console.info('Start benchmark...');
console.info(' ');
const start = performance.now();
suite.run({
onComplete: () => {
console.info(
`Total Duration: ${((performance.now() - start) / 1000).toFixed(2)} sec`,
);
},
});
// ==============================================================
function scoreMinimalReportOptimized0() {
scoreReportOptimized0(minimalReport());
}
function scoreMinimalReportOptimized1() {
scoreReportOptimized1(minimalReport());
}
function scoreMinimalReportOptimized2() {
scoreReportOptimized2(minimalReport());
}
function scoreMinimalReportOptimized3() {
scoreReportOptimized3(minimalReport());
}
// ==============================================================
// eslint-disable-next-line max-lines-per-function
function minimalReport(opt?: MinimalReportOptions): Report {
const numAuditsP1 = opt?.numAuditsP1 ?? NUM_AUDITS_P1;
const numAuditsP2 = opt?.numAuditsP2 ?? NUM_AUDITS_P2;
const numGroupRefs2 = opt?.numGroupRefs2 ?? NUM_GROUPS_P2;
const date = new Date();
return {
date: date.toISOString(),
packageName: 'perf-benchmark',
version: '0',
commit: {
date,
message: 'perf: benchmark score report',
author: 'me',
hash: 'mock_hash',
},
duration: 0,
categories: [
{
slug: 'c1_',
title: 'Category 1',
refs: Array.from({ length: numAuditsP1 }).map((_, idx) => ({
type: 'audit',
plugin: SLUG_PLUGIN_P1,
slug: `${AUDIT_P1_PREFIX}${idx}`,
weight: 1,
})),
},
{
slug: 'c2_',
title: 'Category 2',
refs: Array.from({ length: numAuditsP2 }).map((_, idx) => ({
type: 'audit',
plugin: SLUG_PLUGIN_P2,
slug: `${AUDIT_P2_PREFIX}${idx}`,
weight: 1,
})),
},
],
plugins: [
{
date: '2022-01-01',
duration: 0,
slug: SLUG_PLUGIN_P1,
title: 'Plugin 1',
icon: 'slug',
audits: Array.from({ length: numAuditsP1 }).map((_, idx) => ({
value: 0,
slug: `${AUDIT_P1_PREFIX}${idx}`,
title: 'Default Title',
score: 0.1,
})),
groups: [],
},
{
date: '2022-01-01',
duration: 0,
slug: SLUG_PLUGIN_P2,
title: 'Plugin 2',
icon: 'slug',
audits: Array.from({ length: numAuditsP2 }).map((_, idx) => ({
value: 0,
slug: `${AUDIT_P2_PREFIX}${idx}`,
title: 'Default Title',
score: 0.1,
})),
groups: [
{
title: 'Group 1',
slug: GROUP_P2_PREFIX + 1,
refs: Array.from({ length: numGroupRefs2 }).map((_, idx) => ({
slug: `${AUDIT_P2_PREFIX}${idx}`,
weight: 1,
})),
},
],
},
],
};
}