Skip to content

Commit 3435a11

Browse files
committed
bench: updating benchmark structure
1 parent 26fa1c2 commit 3435a11

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

benches/index.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env tsx
22

3-
import fs from 'fs';
4-
import path from 'path';
5-
import url from 'url';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import url from 'node:url';
66
import si from 'systeminformation';
7-
8-
const dirname = url.fileURLToPath(new URL('.', import.meta.url));
7+
import { benchesPath } from './utils/utils.js';
98

109
async function main(): Promise<void> {
11-
await fs.promises.mkdir(path.join(dirname, 'results'), { recursive: true });
10+
await fs.promises.mkdir(path.join(benchesPath, 'results'), {
11+
recursive: true,
12+
});
1213
const resultFilenames = await fs.promises.readdir(
13-
path.join(dirname, 'results'),
14+
path.join(benchesPath, 'results'),
1415
);
1516
const metricsFile = await fs.promises.open(
16-
path.join(dirname, 'results', 'metrics.txt'),
17+
path.join(benchesPath, 'results', 'metrics.txt'),
1718
'w',
1819
);
1920
let concatenating = false;
2021
for (const resultFilename of resultFilenames) {
2122
if (/.+_metrics\.txt$/.test(resultFilename)) {
2223
const metricsData = await fs.promises.readFile(
23-
path.join(dirname, 'results', resultFilename),
24+
path.join(benchesPath, 'results', resultFilename),
2425
);
2526
if (concatenating) {
2627
await metricsFile.write('\n');
@@ -36,9 +37,16 @@ async function main(): Promise<void> {
3637
system: 'model, manufacturer',
3738
});
3839
await fs.promises.writeFile(
39-
path.join(dirname, 'results', 'system.json'),
40+
path.join(benchesPath, 'results', 'system.json'),
4041
JSON.stringify(systemData, null, 2),
4142
);
4243
}
4344

44-
void main();
45+
if (import.meta.url.startsWith('file:')) {
46+
const modulePath = url.fileURLToPath(import.meta.url);
47+
if (process.argv[1] === modulePath) {
48+
void main();
49+
}
50+
}
51+
52+
export default main;

benches/utils/utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
import fs from 'fs';
2-
import path from 'path';
3-
import url from 'url';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import url from 'node:url';
44
import b from 'benny';
55
import { codeBlock } from 'common-tags';
6-
import packageJson from '../../package.json';
6+
import packageJson from '../../package.json' assert { type: 'json' };
77

8-
const dirname = url.fileURLToPath(new URL('.', import.meta.url));
8+
const benchesPath = path.dirname(
9+
path.dirname(url.fileURLToPath(import.meta.url)),
10+
);
911

1012
const suiteCommon = [
1113
b.cycle(),
1214
b.complete(),
1315
b.save({
1416
file: (summary) => summary.name,
15-
folder: path.join(dirname, '../results'),
17+
folder: path.join(benchesPath, 'results'),
1618
version: packageJson.version,
1719
details: true,
1820
}),
1921
b.save({
2022
file: (summary) => summary.name,
21-
folder: path.join(dirname, '../results'),
23+
folder: path.join(benchesPath, 'results'),
2224
version: packageJson.version,
2325
format: 'chart.html',
2426
}),
2527
b.complete((summary) => {
2628
const filePath = path.join(
27-
dirname,
28-
'../results',
29+
benchesPath,
30+
'results',
2931
summary.name + '_metrics.txt',
3032
);
3133
fs.writeFileSync(
@@ -61,4 +63,4 @@ const suiteCommon = [
6163
}),
6264
];
6365

64-
export { suiteCommon };
66+
export { benchesPath, suiteCommon };

0 commit comments

Comments
 (0)