forked from biomejs/biome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
249 lines (224 loc) · 6.46 KB
/
bench.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/node
import { execSync } from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
import * as process from "node:process";
import * as util from "node:util";
const REPOSITORIES = {
eslint: {
repository: "https://github.com/eslint/eslint.git",
lintedDirs: ["lib", "messages", "tests/lib", "tests/performance", "tools"],
},
prettier: {
repository: "https://github.com/prettier/prettier.git",
formattedDirs: ["src", "scripts"],
},
webpack: {
repository: "https://github.com/webpack/webpack.git",
formattedDirs: ["lib", "examples", "benchmark"],
lintedDirs: ["lib"],
},
};
const TMP_DIR = path.resolve("./target");
function benchmarkFormatter(biomeBin, options) {
// Run Dprint once to run the installer
execSync("npx dprint --version");
for (const [name, config] of Object.entries(REPOSITORIES)) {
if (
!config.formattedDirs ||
("repository" in options && options.repository !== name)
) {
continue;
}
console.info(`\n⌛ repository: ${name}`);
const projectDirectory = cloneProject(
name,
config.repository,
config.formattedDirs,
);
console.info("");
const dirs = config.formattedDirs.join(" ");
const dirGlobs = config.formattedDirs.map((dir) => `"${dir}/**"`).join(" ");
const biomeCommand = `${biomeBin} format --config-path=../../ --write --max-diagnostics=0 ${dirs}`;
const benchCommands = {
prettier: `../../node_modules/.bin/prettier --config=../../.prettierrc.json --ignore-path=../../.prettierignore --write --log-level=error ${dirs}`,
// FIXME: Parallel Prettier is crashing on Node 22
// "parallel-prettier": `../../node_modules/.bin/pprettier --write --concurrency ${os.cpus().length} ${dirGlobs}`,
dprint: `../../node_modules/dprint/dprint fmt --config=../../dprint.json ${dirGlobs}`,
biome: biomeCommand,
"biome-1-thread": withEnvVariable("RAYON_NUM_THREADS", "1", biomeCommand),
};
let suite = "";
for (const benchName of Object.keys(benchCommands)) {
if ("bench" in options && !options.bench.includes(benchName)) {
continue;
}
suite += ` -n "${benchName}" '${benchCommands[benchName]}'`;
}
if (suite.length === 0) {
console.error(`Benchmark '${options.bench}' doesn't exist.`);
process.exit(1);
}
// Run 2 warmups to make sure the files are formatted correctly
let hyperfineCommand = `hyperfine --warmup 2 --shell=${shellOption()} ${suite}`;
if (options.verbose) {
hyperfineCommand += " --show-output";
console.info(`${hyperfineCommand}\n`);
}
execSync(hyperfineCommand, {
cwd: projectDirectory,
stdio: "inherit",
});
}
}
function benchmarkLinter(biomeBin, options) {
for (const [name, config] of Object.entries(REPOSITORIES)) {
if (
!config.lintedDirs ||
("repository" in options && options.repository !== name)
) {
continue;
}
console.info(`\n⌛ repository: ${name}`);
const projectDirectory = cloneProject(
name,
config.repository,
config.formattedDirs,
);
console.info("");
const dirs = config.lintedDirs.map((dir) => `"${dir}"`).join(" ");
const biomeCmd = `"${biomeBin}" lint --config-path=../../ --max-diagnostics=0 ${dirs}`;
const benchCommands = {
eslint: `../../node_modules/.bin/eslint --quiet --config=../../eslint.config.js --no-ignore ${dirs}`,
"ts-eslint": `../../node_modules/.bin/eslint --quiet --config=../../ts-eslint.config.js --no-ignore ${dirs}`,
biome: biomeCmd,
"biome-1-thread": withEnvVariable("RAYON_NUM_THREADS", "1", biomeCmd),
"ts-biome": `"${biomeBin}" lint --config-path=../../ts-biome.json --max-diagnostics=0 ${dirs}`,
};
let suite = "";
for (const benchName of Object.keys(benchCommands)) {
if ("bench" in options && !options.bench.includes(benchName)) {
continue;
}
suite += ` -n "${benchName}" '${benchCommands[benchName]}'`;
}
if (suite.length === 0) {
console.error(`Benchmark '${options.bench}' doesn't exist.`);
process.exit(1);
}
let hyperfineCommand = `hyperfine --ignore-failure --shell=${shellOption()} ${suite}`;
if (options.verbose) {
hyperfineCommand += " --show-output";
console.info(`${hyperfineCommand}\n`);
}
execSync(hyperfineCommand, {
cwd: projectDirectory,
stdio: "inherit",
});
}
}
function shellOption() {
if (process.platform === "win32") {
// Use Powershell so that it is possible to set an environment variable for a single command (ugh!)
return "powershell";
}
return "default";
}
function withEnvVariable(name, value, command) {
switch (process.platform) {
case "win32": {
return `$Env:${name}=${value}; ${command}`;
}
default:
return `${name}="${value}" ${command}`;
}
}
function withDirectory(cwd) {
return {
run(command, options) {
execSync(command, {
cwd,
...options,
});
},
};
}
function cloneProject(name, repository, dirs = []) {
const projectDirectory = path.join(TMP_DIR, name);
const inProjectDirectory = withDirectory(projectDirectory);
if (fs.existsSync(projectDirectory)) {
inProjectDirectory.run("git reset --hard @{u}");
inProjectDirectory.run("git clean -df");
inProjectDirectory.run("git pull --quiet --depth=1 --ff-only");
} else {
withDirectory(TMP_DIR).run(
`git clone --quiet --depth=1 ${dirs.length > 0 ? "--sparse" : ""} ${repository}`,
{
stdio: "inherit",
},
);
}
if (dirs.length > 0) {
console.info(
`Adding directories ${dirs.join()} to sparse checkout in ${projectDirectory}`,
);
inProjectDirectory.run(`git sparse-checkout add ${dirs.join(" ")}`);
}
return projectDirectory;
}
function buildBiome() {
console.info("Building Biome...");
execSync(
withEnvVariable(
"BIOME_VERSION",
"0.0.0",
"cargo build --bin biome --release",
),
{
stdio: "inherit",
},
);
return path.resolve("../target/release/biome");
}
function run({ positionals, values: options }) {
fs.mkdirSync(TMP_DIR, { recursive: true });
let biomeBinPath;
if ("biome" in options) {
biomeBinPath = options.biome;
} else {
biomeBinPath = buildBiome();
}
switch (positionals[0]) {
case "formatter":
benchmarkFormatter(biomeBinPath, options);
break;
case "linter":
benchmarkLinter(biomeBinPath, options);
break;
default:
console.error(
`A positional argument among 'formatter' and 'linter' must be passed.`,
);
process.exit(1);
}
}
run(
util.parseArgs({
allowPositionals: true,
options: {
bench: {
multiple: true,
type: "string",
},
biome: {
type: "string",
},
repository: {
type: "string",
},
verbose: {
type: "boolean",
},
},
}),
);