Skip to content

Commit 901e0c7

Browse files
committed
bench: adding dummy benchmark
1 parent b9d31fd commit 901e0c7

File tree

6 files changed

+415
-1
lines changed

6 files changed

+415
-1
lines changed

benches/exec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import path from 'node:path';
2+
import url from 'node:url';
3+
import b from 'benny';
4+
import { suiteCommon } from './utils/index.js';
5+
6+
const filePath = url.fileURLToPath(import.meta.url);
7+
8+
async function main() {
9+
const summary = await b.suite(
10+
path.basename(filePath, path.extname(filePath)),
11+
b.add('stub', () => {
12+
1 + 1;
13+
}),
14+
...suiteCommon,
15+
);
16+
return summary;
17+
}
18+
19+
if (import.meta.url.startsWith('file:')) {
20+
const modulePath = url.fileURLToPath(import.meta.url);
21+
if (process.argv[1] === modulePath) {
22+
void main();
23+
}
24+
}
25+
26+
export default main;

benches/index.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env tsx
2+
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import url from 'node:url';
6+
import si from 'systeminformation';
7+
import { benchesPath } from './utils/utils.js';
8+
import exec from './exec.js';
9+
10+
async function main(): Promise<void> {
11+
await fs.promises.mkdir(path.join(benchesPath, 'results'), {
12+
recursive: true,
13+
});
14+
await exec();
15+
const resultFilenames = await fs.promises.readdir(
16+
path.join(benchesPath, 'results'),
17+
);
18+
const metricsFile = await fs.promises.open(
19+
path.join(benchesPath, 'results', 'metrics.txt'),
20+
'w',
21+
);
22+
let concatenating = false;
23+
for (const resultFilename of resultFilenames) {
24+
if (/.+_metrics\.txt$/.test(resultFilename)) {
25+
const metricsData = await fs.promises.readFile(
26+
path.join(benchesPath, 'results', resultFilename),
27+
);
28+
if (concatenating) {
29+
await metricsFile.write('\n');
30+
}
31+
await metricsFile.write(metricsData);
32+
concatenating = true;
33+
}
34+
}
35+
await metricsFile.close();
36+
const systemData = await si.get({
37+
cpu: '*',
38+
osInfo: 'platform, distro, release, kernel, arch',
39+
system: 'model, manufacturer',
40+
});
41+
await fs.promises.writeFile(
42+
path.join(benchesPath, 'results', 'system.json'),
43+
JSON.stringify(systemData, null, 2),
44+
);
45+
}
46+
47+
if (import.meta.url.startsWith('file:')) {
48+
const modulePath = url.fileURLToPath(import.meta.url);
49+
if (process.argv[1] === modulePath) {
50+
void main();
51+
}
52+
}
53+
54+
export default main;

benches/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './utils.js';

benches/utils/utils.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import url from 'node:url';
4+
import b from 'benny';
5+
import { codeBlock } from 'common-tags';
6+
import packageJson from '../../package.json';
7+
8+
const benchesPath = path.dirname(
9+
path.dirname(url.fileURLToPath(import.meta.url)),
10+
);
11+
12+
const suiteCommon = [
13+
b.cycle(),
14+
b.complete(),
15+
b.save({
16+
file: (summary) => summary.name,
17+
folder: path.join(benchesPath, 'results'),
18+
version: packageJson.version,
19+
details: true,
20+
}),
21+
b.save({
22+
file: (summary) => summary.name,
23+
folder: path.join(benchesPath, 'results'),
24+
version: packageJson.version,
25+
format: 'chart.html',
26+
}),
27+
b.complete((summary) => {
28+
const filePath = path.join(
29+
benchesPath,
30+
'results',
31+
summary.name + '_metrics.txt',
32+
);
33+
fs.writeFileSync(
34+
filePath,
35+
codeBlock`
36+
# TYPE ${summary.name}_ops gauge
37+
${summary.results
38+
.map(
39+
(result) =>
40+
`${summary.name}_ops{name="${result.name}"} ${result.ops}`,
41+
)
42+
.join('\n')}
43+
44+
# TYPE ${summary.name}_margin gauge
45+
${summary.results
46+
.map(
47+
(result) =>
48+
`${summary.name}_margin{name="${result.name}"} ${result.margin}`,
49+
)
50+
.join('\n')}
51+
52+
# TYPE ${summary.name}_samples counter
53+
${summary.results
54+
.map(
55+
(result) =>
56+
`${summary.name}_samples{name="${result.name}"} ${result.samples}`,
57+
)
58+
.join('\n')}
59+
` + '\n',
60+
);
61+
// eslint-disable-next-line no-console
62+
console.log('\nSaved to:', path.resolve(filePath));
63+
}),
64+
];
65+
66+
export { benchesPath, suiteCommon };

0 commit comments

Comments
 (0)