Skip to content

Commit 1d6691d

Browse files
committed
ref(bench): centralize withCodSpeed workaround
1 parent 0957d88 commit 1d6691d

5 files changed

Lines changed: 54 additions & 34 deletions

File tree

bench/fixtures/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { fileURLToPath } from "node:url";
22
import { spawn } from "node:child_process";
33

4+
export { withCodSpeed } from "@codspeed/tinybench-plugin";
5+
6+
// CodSpeed walltime reads `task.result.latency` which
7+
// tinybench only sets for async tasks
8+
export function sync(fn: () => void): () => Promise<void> {
9+
return () => {
10+
fn();
11+
return Promise.resolve();
12+
};
13+
}
14+
415
export const fixture = (name: string) => {
516
return new URL(`./${name}/mod.ts`, import.meta.url);
617
};

bench/ops.bench.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Bench } from "tinybench";
2-
import { withCodSpeed } from "@codspeed/tinybench-plugin";
2+
import { sync, withCodSpeed } from "./fixtures/utils.ts";
33
import { close, fixed, grow, open, pack, rgba, text } from "../ops.ts";
44
import type { Op } from "../ops.ts";
55

@@ -97,14 +97,18 @@ let buf = new ArrayBuffer(32768);
9797
let bench = withCodSpeed(new Bench({ name: "ops" }));
9898

9999
bench
100-
.add("pack complex layout", () => {
101-
for (let i = 0; i < 1500; i++) pack(complexOps, buf, 0);
102-
return Promise.resolve();
103-
})
104-
.add("pack large list", () => {
105-
for (let i = 0; i < 250; i++) pack(listOps, buf, 0);
106-
return Promise.resolve();
107-
});
100+
.add(
101+
"pack complex layout",
102+
sync(() => {
103+
for (let i = 0; i < 1500; i++) pack(complexOps, buf, 0);
104+
}),
105+
)
106+
.add(
107+
"pack large list",
108+
sync(() => {
109+
for (let i = 0; i < 250; i++) pack(listOps, buf, 0);
110+
}),
111+
);
108112

109113
await bench.run();
110114
console.table(bench.table());

bench/render.bench.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Bench } from "tinybench";
2-
import { withCodSpeed } from "@codspeed/tinybench-plugin";
2+
import { sync, withCodSpeed } from "./fixtures/utils.ts";
33
import { createTerm } from "../term.ts";
44
import { close, fixed, grow, open, rgba, text } from "../ops.ts";
55
import type { Op } from "../ops.ts";
@@ -103,18 +103,22 @@ let uiOps: Op[] = [
103103
let bench = withCodSpeed(new Bench({ name: "render" }));
104104

105105
bench
106-
.add("render mixed frames", () => {
107-
for (let i = 0; i < 250; i++) {
108-
term.render(i % 2 === 0 ? dashboardOps : uiOps);
109-
}
110-
return Promise.resolve();
111-
})
112-
.add("render steady diff", () => {
113-
for (let i = 0; i < 250; i++) {
114-
term.render(dashboardOps);
115-
}
116-
return Promise.resolve();
117-
});
106+
.add(
107+
"render mixed frames",
108+
sync(() => {
109+
for (let i = 0; i < 250; i++) {
110+
term.render(i % 2 === 0 ? dashboardOps : uiOps);
111+
}
112+
}),
113+
)
114+
.add(
115+
"render steady diff",
116+
sync(() => {
117+
for (let i = 0; i < 250; i++) {
118+
term.render(dashboardOps);
119+
}
120+
}),
121+
);
118122

119123
await bench.run();
120124
console.table(bench.table());

bench/startup.bench.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Bench } from "tinybench";
2-
import { withCodSpeed } from "@codspeed/tinybench-plugin";
3-
import { spawnFixture } from "./fixtures/utils.ts";
2+
import { spawnFixture, withCodSpeed } from "./fixtures/utils.ts";
43

54
let bench = withCodSpeed(new Bench({ name: "startup" }));
65

bench/throughput.bench.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Bench } from "tinybench";
2-
import { withCodSpeed } from "@codspeed/tinybench-plugin";
2+
import { sync, withCodSpeed } from "./fixtures/utils.ts";
33
import { createInput } from "../input.ts";
44

55
function str(s: string): Uint8Array {
@@ -33,15 +33,17 @@ let input = await createInput({ escLatency: 25 });
3333

3434
let bench = withCodSpeed(new Bench({ name: "throughput" }));
3535

36-
bench.add("input throughput (mixed corpus, chunked read loop)", () => {
37-
let dispatched = 0;
38-
for (let off = 0; off < corpus.length; off += READ) {
39-
let { events } = input.scan(corpus.subarray(off, off + READ));
40-
dispatched += events.length;
41-
}
42-
if (dispatched === 0) throw new Error("expected events");
43-
return Promise.resolve();
44-
});
36+
bench.add(
37+
"input throughput (mixed corpus, chunked read loop)",
38+
sync(() => {
39+
let dispatched = 0;
40+
for (let off = 0; off < corpus.length; off += READ) {
41+
let { events } = input.scan(corpus.subarray(off, off + READ));
42+
dispatched += events.length;
43+
}
44+
if (dispatched === 0) throw new Error("expected events");
45+
}),
46+
);
4547

4648
await bench.run();
4749
console.table(bench.table());

0 commit comments

Comments
 (0)