Skip to content

Commit 56ccb81

Browse files
committed
bench: updating benchmark structure
1 parent 6d0a412 commit 56ccb81

File tree

8 files changed

+123
-153
lines changed

8 files changed

+123
-153
lines changed

benches/suites/baseline_tcp/baseline_tcp_1KiB.ts renamed to benches/baseline_tcp_1KiB.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { Host } from '../../../src/types.js';
1+
import type { Host } from '#types.js';
22
import * as net from 'net';
3+
import path from 'node:path';
34
import url from 'node:url';
45
import b from 'benny';
5-
import { suiteCommon, summaryName } from '../../utils.js';
6+
import { suiteCommon } from './utils/utils.js';
67
import { promise } from '#utils.js';
78

89
const filePath = url.fileURLToPath(import.meta.url);
@@ -38,7 +39,7 @@ async function main() {
3839

3940
// Running benchmark
4041
const summary = await b.suite(
41-
summaryName(filePath),
42+
path.basename(filePath, path.extname(filePath)),
4243
b.add('send 1KiB of data over tcp', async () => {
4344
const prom = promise();
4445
client.write(data1KiB, () => {

benches/suites/baseline_websocket/baseline_websocket_1KiB.ts renamed to benches/baseline_websocket_1KiB.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import type { Host } from '../../../src/types.js';
1+
import type { Host } from '#types.js';
22
import type { AddressInfo } from 'net';
33
import * as https from 'node:https';
4+
import path from 'node:path';
45
import url from 'node:url';
56
import b from 'benny';
67
import * as ws from 'ws';
7-
import { suiteCommon, summaryName } from '../../utils.js';
8-
import * as testsUtils from '../../../tests/utils.js';
8+
import { suiteCommon } from './utils/utils.js';
9+
import * as testsUtils from '../tests/utils.js';
910
import { promise } from '#utils.js';
1011

1112
const filePath = url.fileURLToPath(import.meta.url);
@@ -42,7 +43,7 @@ async function main() {
4243

4344
// Running benchmark
4445
const summary = await b.suite(
45-
summaryName(filePath),
46+
path.basename(filePath, path.extname(filePath)),
4647
b.add('send 1KiB of data over ws', async () => {
4748
const sendProm = promise();
4849
client.send(data1KiB, { binary: true }, () => {

benches/suites/connection/connection_1KiB.ts renamed to benches/connection_1KiB.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import type { Host } from '../../../src/types.js';
1+
import type { Host } from '#types.js';
2+
import path from 'node:path';
23
import url from 'node:url';
34
import b from 'benny';
45
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
5-
import { suiteCommon, summaryName } from '../../utils.js';
6-
import * as events from '../../../src/events.js';
7-
import * as testsUtils from '../../../tests/utils.js';
8-
import WebSocketServer from '../../../src/WebSocketServer.js';
9-
import WebSocketClient from '../../../src/WebSocketClient.js';
6+
import { suiteCommon } from './utils/utils.js';
7+
import * as testsUtils from '../tests/utils.js';
8+
import * as events from '#events.js';
9+
import WebSocketServer from '#WebSocketServer.js';
10+
import WebSocketClient from '#WebSocketClient.js';
1011

1112
const filePath = url.fileURLToPath(import.meta.url);
1213

@@ -51,7 +52,7 @@ async function main() {
5152

5253
// Running benchmark
5354
const summary = await b.suite(
54-
summaryName(filePath),
55+
path.basename(filePath, path.extname(filePath)),
5556
b.add('send 1KiB of data over connection', async () => {
5657
// @ts-ignore: protected property
5758
await client.connection.send(data1KiB);

benches/index.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env tsx
22

3-
import type { Summary } from 'benny/lib/internal/common-types.js';
43
import fs from 'node:fs';
54
import path from 'node:path';
65
import url from 'node:url';
76
import si from 'systeminformation';
8-
import { fsWalk, resultsPath, suitesPath } from './utils.js';
9-
10-
const projectPath = path.dirname(url.fileURLToPath(import.meta.url));
7+
import { benchesPath } from './utils/utils.js';
8+
import baseline_tcp_1KiB from './baseline_tcp_1KiB.js';
9+
import baseline_websocket_1KiB from './baseline_websocket_1KiB.js';
10+
import connection_1KiB from './connection_1KiB.js';
11+
import stream_1KiB from './stream_1KiB.js';
1112

1213
async function main(): Promise<void> {
13-
await fs.promises.mkdir(path.join(projectPath, 'results'), {
14+
await fs.promises.mkdir(path.join(benchesPath, 'results'), {
1415
recursive: true,
1516
});
16-
// Running all suites
17-
for await (const suitePath of fsWalk(suitesPath)) {
18-
// Skip over non-ts and non-js files
19-
const ext = path.extname(suitePath);
20-
if (ext !== '.ts' && ext !== '.js') {
21-
continue;
22-
}
23-
const suite: () => Promise<Summary> = (await import(suitePath)).default;
24-
await suite();
25-
}
26-
// Concatenating metrics
27-
const metricsPath = path.join(resultsPath, 'metrics.txt');
17+
await baseline_tcp_1KiB();
18+
await baseline_websocket_1KiB();
19+
await connection_1KiB();
20+
await stream_1KiB();
21+
const resultFilenames = await fs.promises.readdir(
22+
path.join(benchesPath, 'results'),
23+
);
24+
const metricsFile = await fs.promises.open(
25+
path.join(benchesPath, 'results', 'metrics.txt'),
26+
'w',
27+
);
2828
let concatenating = false;
29-
for await (const metricPath of fsWalk(resultsPath)) {
30-
// Skip over non-metrics files
31-
if (!metricPath.endsWith('_metrics.txt')) {
32-
continue;
33-
}
34-
const metricData = await fs.promises.readFile(metricPath);
35-
if (concatenating) {
36-
await fs.promises.appendFile(metricsPath, '\n');
29+
for (const resultFilename of resultFilenames) {
30+
if (/.+_metrics\.txt$/.test(resultFilename)) {
31+
const metricsData = await fs.promises.readFile(
32+
path.join(benchesPath, 'results', resultFilename),
33+
);
34+
if (concatenating) {
35+
await metricsFile.write('\n');
36+
}
37+
await metricsFile.write(metricsData);
38+
concatenating = true;
3739
}
38-
await fs.promises.appendFile(metricsPath, metricData);
39-
concatenating = true;
4040
}
41+
await metricsFile.close();
4142
const systemData = await si.get({
4243
cpu: '*',
4344
osInfo: 'platform, distro, release, kernel, arch',
4445
system: 'model, manufacturer',
4546
});
4647
await fs.promises.writeFile(
47-
path.join(projectPath, 'results', 'system.json'),
48+
path.join(benchesPath, 'results', 'system.json'),
4849
JSON.stringify(systemData, null, 2),
4950
);
5051
}

benches/suites/stream/stream_1KiB.ts renamed to benches/stream_1KiB.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import type { Host } from '../../../src/types.js';
1+
import type { Host } from '#types.js';
2+
import path from 'node:path';
23
import url from 'node:url';
34
import b from 'benny';
45
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
5-
import { suiteCommon, summaryName } from '../../utils.js';
6-
import * as events from '../../../src/events.js';
7-
import * as testsUtils from '../../../tests/utils.js';
8-
import WebSocketServer from '../../../src/WebSocketServer.js';
9-
import WebSocketClient from '../../../src/WebSocketClient.js';
6+
import { suiteCommon } from './utils/utils.js';
7+
import * as testsUtils from '../tests/utils.js';
8+
import * as events from '#events.js';
9+
import WebSocketServer from '#WebSocketServer.js';
10+
import WebSocketClient from '#WebSocketClient.js';
1011

1112
const filePath = url.fileURLToPath(import.meta.url);
1213

@@ -77,7 +78,7 @@ async function main() {
7778

7879
// Running benchmark
7980
const summary = await b.suite(
80-
summaryName(filePath),
81+
path.basename(filePath, path.extname(filePath)),
8182
b.add('send 1KiB of data over stream', async () => {
8283
await writer.write(data1KiB);
8384
}),
@@ -95,4 +96,5 @@ if (import.meta.url.startsWith('file:')) {
9596
void main();
9697
}
9798
}
99+
98100
export default main;

benches/utils.ts

Lines changed: 0 additions & 103 deletions
This file was deleted.

benches/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './utils.js';

benches/utils/utils.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import url from 'node:url';
4+
import b from 'benny';
5+
import { codeBlock } from 'common-tags';
6+
import packageJson from '../../package.json' assert { type: '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)