Skip to content

Commit ac539c9

Browse files
committed
feat: add some benches status display
1 parent 4c6749a commit ac539c9

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

packages/benchmark.js/rollup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default defineConfig([
2626
},
2727
{ file: pkg.module, format: "es", sourcemap: true },
2828
],
29-
plugins: [...jsPlugins],
29+
plugins: jsPlugins(pkg.version),
3030
external: ["@codspeed/core"],
3131
},
3232
]);

packages/benchmark.js/src/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { get as getStackTrace } from "stack-trace";
44
import path, { dirname } from "path";
55
import { findUpSync, Options } from "find-up";
66

7+
declare const __VERSION__: string;
8+
79
export function withCodSpeed(suite: Benchmark): Benchmark;
810
export function withCodSpeed(suite: Benchmark.Suite): Benchmark.Suite;
911
export function withCodSpeed(item: unknown): unknown {
@@ -16,30 +18,52 @@ export function withCodSpeed(item: unknown): unknown {
1618

1719
function withCodSpeedBenchmark(bench: Benchmark): Benchmark {
1820
if (!measurement.isInstrumented()) {
21+
const rawRun = bench.run;
22+
bench.run = (options?: Benchmark.Options) => {
23+
console.warn(
24+
`[CodSpeed] bench detected but no instrumentation found, falling back to benchmark.js`
25+
);
26+
return rawRun.bind(bench)(options);
27+
};
1928
return bench;
2029
}
2130
initCore();
2231
const callingFile = getCallingFile();
2332
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2433
bench.run = function (options?: Benchmark.Options): Benchmark {
34+
console.log(
35+
`[CodSpeed] running with @codspeed/benchmark.js v${__VERSION__}`
36+
);
2537
const uri = callingFile + "::" + (bench.name ?? "unknown");
2638
const fn = bench.fn as CallableFunction;
2739
optimizeFunctionSync(fn);
2840
measurement.startInstrumentation();
2941
fn();
3042
measurement.stopInstrumentation(uri);
43+
console.log(` ✔ Measured ${uri}`);
44+
console.log("[CodSpeed] Done running 1 bench.");
3145
return bench;
3246
};
3347
return bench;
3448
}
3549

3650
function withCodSpeedSuite(suite: Benchmark.Suite): Benchmark.Suite {
3751
if (!measurement.isInstrumented()) {
52+
const rawRun = suite.run;
53+
suite.run = (options?: Benchmark.Options) => {
54+
console.warn(
55+
`[CodSpeed] ${suite.length} benches detected but no instrumentation found, falling back to benchmark.js`
56+
);
57+
return rawRun.bind(suite)(options);
58+
};
3859
return suite;
3960
}
4061
const callingFile = getCallingFile();
4162
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4263
suite.run = function (options?: Benchmark.Options): Benchmark.Suite {
64+
console.log(
65+
`[CodSpeed] running with @codspeed/benchmark.js v${__VERSION__}`
66+
);
4367
const suiteName = suite.name;
4468
const benches = this as unknown as Benchmark[];
4569
let baseUri = callingFile;
@@ -52,7 +76,9 @@ function withCodSpeedSuite(suite: Benchmark.Suite): Benchmark.Suite {
5276
measurement.startInstrumentation();
5377
(bench.fn as CallableFunction)();
5478
measurement.stopInstrumentation(uri);
79+
console.log(` ✔ Measured ${uri}`);
5580
}
81+
console.log(`[CodSpeed] Done running ${suite.length} benches.`);
5682
return suite;
5783
};
5884
return suite;

packages/core/rollup.config.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import json from "@rollup/plugin-json";
33
import commonjs from "@rollup/plugin-commonjs";
44
import esbuild from "rollup-plugin-esbuild";
55
import { defineConfig } from "rollup";
6-
import { declarationsPlugin } from "../../rollup.options";
6+
import { declarationsPlugin, jsPlugins } from "../../rollup.options";
77

88
import pkg from "./package.json" assert { type: "json" };
99
const entrypoint = "src/index.ts";
@@ -30,15 +30,6 @@ export default defineConfig([
3030
},
3131
{ file: pkg.module, format: "es", sourcemap: true },
3232
],
33-
plugins: [
34-
json(),
35-
esbuild({
36-
define: {
37-
__VERSION__: '"' + pkg.version + '"',
38-
},
39-
}),
40-
commonjs(),
41-
resolve(),
42-
],
33+
plugins: jsPlugins(pkg.version),
4334
},
4435
]);

packages/tinybench/rollup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default defineConfig([
2626
},
2727
{ file: pkg.module, format: "es", sourcemap: true },
2828
],
29-
plugins: [...jsPlugins],
29+
plugins: jsPlugins(pkg.version),
3030
external: ["@codspeed/core"],
3131
},
3232
]);

packages/tinybench/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,32 @@ import path, { dirname } from "path";
44
import { findUpSync, Options } from "find-up";
55
import { Bench } from "tinybench";
66

7+
declare const __VERSION__: string;
8+
79
export function withCodSpeed(bench: Bench): Bench {
810
if (!measurement.isInstrumented()) {
11+
const rawRun = bench.run;
12+
bench.run = async () => {
13+
console.warn(
14+
`[CodSpeed] ${bench.tasks.length} benches detected but no instrumentation found, falling back to tinybench`
15+
);
16+
return await rawRun.bind(bench)();
17+
};
918
return bench;
1019
}
1120
initCore();
1221
const callingFile = getCallingFile();
1322
bench.run = async () => {
23+
console.log(`[CodSpeed] running with @codspeed/tinybench v${__VERSION__}`);
1424
for (const task of bench.tasks) {
1525
const uri = callingFile + "::" + task.name;
1626
await optimizeFunction(task.fn);
1727
measurement.startInstrumentation();
1828
await task.fn();
1929
measurement.stopInstrumentation(uri);
30+
console.log(` ✔ Measured ${uri}`);
2031
}
32+
console.log(`[CodSpeed] Done running ${bench.tasks.length} benches.`);
2133
return bench.tasks;
2234
};
2335
return bench;

rollup.options.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ import dts from "rollup-plugin-dts";
66

77
export const declarationsPlugin = [dts()];
88

9-
export const jsPlugins = [json(), esbuild(), commonjs(), resolve()];
9+
export const jsPlugins = (version) => [
10+
json(),
11+
esbuild({
12+
define: {
13+
__VERSION__: '"' + version + '"',
14+
},
15+
}),
16+
commonjs(),
17+
resolve(),
18+
];

0 commit comments

Comments
 (0)