Skip to content

Commit 590b165

Browse files
committed
feat: add memory profiling support
1 parent 4b39225 commit 590b165

File tree

14 files changed

+87
-45
lines changed

14 files changed

+87
-45
lines changed

.github/workflows/codspeed.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,22 @@ jobs:
2323
- run: pnpm install --frozen-lockfile --prefer-offline
2424
- run: pnpm moon run :build
2525

26-
- name: Run benchmarks
27-
# use version from `main` branch to always test the latest version, in real projects, use a tag, like `@v2`
26+
- name: Run simulation benchmarks
27+
uses: CodSpeedHQ/action@main
28+
with:
29+
mode: simulation
30+
run: |
31+
pnpm moon run tinybench-plugin:bench
32+
pnpm moon run vitest-plugin:bench
33+
pnpm moon run benchmark.js-plugin:bench
34+
pnpm --workspace-concurrency 1 -r bench-tinybench
35+
pnpm --workspace-concurrency 1 -r bench-benchmark-js
36+
pnpm --workspace-concurrency 1 -r bench-vitest
37+
38+
- name: Run memory benchmarks
2839
uses: CodSpeedHQ/action@main
2940
with:
30-
mode: instrumentation
41+
mode: memory
3142
run: |
3243
pnpm moon run tinybench-plugin:bench
3344
pnpm moon run vitest-plugin:bench

packages/core/binding.gyp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"-Wno-unused-variable",
1515
"-Wno-unused-parameter",
1616
"-Wno-unused-but-set-variable",
17-
"-Wno-type-limits"
17+
"-Wno-type-limits",
18+
"-Wno-format",
19+
"-Wno-format-security"
1820
],
1921
"cflags_cc": [
2022
"-Wno-maybe-uninitialized",

packages/core/src/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export const isBound = native_core.isBound;
1010

1111
export const mongoMeasurement = new MongoMeasurement();
1212

13-
type CodSpeedRunnerMode = "disabled" | "simulation" | "walltime";
13+
type CodSpeedRunnerMode = "disabled" | "simulation" | "memory" | "walltime";
14+
15+
type InstrumentMode = "disabled" | "analysis" | "walltime";
1416

1517
export function getCodspeedRunnerMode(): CodSpeedRunnerMode {
1618
const isCodSpeedEnabled = process.env.CODSPEED_ENV !== undefined;
@@ -25,6 +27,8 @@ export function getCodspeedRunnerMode(): CodSpeedRunnerMode {
2527
codspeedRunnerMode === "simulation"
2628
) {
2729
return "simulation";
30+
} else if (codspeedRunnerMode === "memory") {
31+
return "memory";
2832
} else if (codspeedRunnerMode === "walltime") {
2933
return "walltime";
3034
}
@@ -35,6 +39,15 @@ export function getCodspeedRunnerMode(): CodSpeedRunnerMode {
3539
return "disabled";
3640
}
3741

42+
export function getInstrumentMode(): InstrumentMode {
43+
const runnerMode = getCodspeedRunnerMode();
44+
// Both "simulation" and "memory" map to "analysis" instrument mode
45+
if (runnerMode === "simulation" || runnerMode === "memory") {
46+
return "analysis";
47+
}
48+
return runnerMode; // "disabled" or "walltime"
49+
}
50+
3851
export const setupCore = () => {
3952
if (!native_core.isBound) {
4053
throw new Error(
@@ -59,4 +72,5 @@ export { getV8Flags, tryIntrospect } from "./introspection";
5972
export { optimizeFunction, optimizeFunctionSync } from "./optimization";
6073
export * from "./utils";
6174
export * from "./walltime";
75+
export type { InstrumentMode };
6276
export const InstrumentHooks = native_core.InstrumentHooks;

packages/core/src/introspection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { writeFileSync } from "fs";
2-
import { getCodspeedRunnerMode } from ".";
2+
import { getInstrumentMode } from ".";
33

44
const CUSTOM_INTROSPECTION_EXIT_CODE = 0;
55

66
export const getV8Flags = () => {
77
const nodeVersionMajor = parseInt(process.version.slice(1).split(".")[0]);
8-
const codspeedRunnerMode = getCodspeedRunnerMode();
8+
const instrumentMode = getInstrumentMode();
99

1010
const flags = ["--interpreted-frames-native-stack", "--allow-natives-syntax"];
1111

12-
if (codspeedRunnerMode === "simulation") {
12+
if (instrumentMode === "analysis") {
1313
flags.push(
1414
...[
1515
"--hash-seed=1",

packages/tinybench-plugin/src/simulation.ts renamed to packages/tinybench-plugin/src/analysis.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import {
2+
getCodspeedRunnerMode,
23
InstrumentHooks,
34
mongoMeasurement,
45
optimizeFunction,
56
} from "@codspeed/core";
67
import { Bench, Fn, FnOptions, Task } from "tinybench";
78
import { BaseBenchRunner } from "./shared";
89

9-
export function setupCodspeedSimulationBench(
10+
export function setupCodspeedAnalysisBench(
1011
bench: Bench,
1112
rootCallingFile: string
1213
): void {
13-
const runner = new SimulationBenchRunner(bench, rootCallingFile);
14+
const runner = new AnalysisBenchRunner(bench, rootCallingFile);
1415
runner.setupBenchMethods();
1516
}
1617

17-
class SimulationBenchRunner extends BaseBenchRunner {
18+
class AnalysisBenchRunner extends BaseBenchRunner {
1819
protected getModeName(): string {
19-
return "simulation mode";
20+
const runnerMode = getCodspeedRunnerMode();
21+
return `${runnerMode} mode`;
2022
}
2123

2224
private taskCompletionMessage() {

packages/tinybench-plugin/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
getCodspeedRunnerMode,
33
getGitDir,
4+
getInstrumentMode,
45
InstrumentHooks,
56
mongoMeasurement,
67
SetupInstrumentsRequestBody,
@@ -11,7 +12,7 @@ import path from "path";
1112
import { get as getStackTrace } from "stack-trace";
1213
import { Bench } from "tinybench";
1314
import { fileURLToPath } from "url";
14-
import { setupCodspeedSimulationBench } from "./simulation";
15+
import { setupCodspeedAnalysisBench } from "./analysis";
1516
import { getOrCreateUriMap } from "./uri";
1617
import { setupCodspeedWalltimeBench } from "./walltime";
1718

@@ -39,9 +40,10 @@ export function withCodSpeed(bench: Bench): Bench {
3940
return rawAdd.bind(bench)(name, fn, opts);
4041
};
4142

42-
if (codspeedRunnerMode === "simulation") {
43-
setupCodspeedSimulationBench(bench, rootCallingFile);
44-
} else if (codspeedRunnerMode === "walltime") {
43+
const instrumentMode = getInstrumentMode();
44+
if (instrumentMode === "analysis") {
45+
setupCodspeedAnalysisBench(bench, rootCallingFile);
46+
} else if (instrumentMode === "walltime") {
4547
setupCodspeedWalltimeBench(bench, rootCallingFile);
4648
}
4749

packages/tinybench-plugin/src/index.unit.test.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { Bench } from "tinybench";
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import { withCodSpeed } from ".";
44

5-
const mockSimulation = vi.hoisted(() => ({
6-
setupCodspeedSimulationBench: vi.fn(),
5+
const mockAnalysis = vi.hoisted(() => ({
6+
setupCodspeedAnalysisBench: vi.fn(),
77
}));
88

9-
vi.mock("./simulation", () => ({
10-
...mockSimulation,
9+
vi.mock("./analysis", () => ({
10+
...mockAnalysis,
1111
}));
1212

1313
const mockWalltime = vi.hoisted(() => ({
@@ -38,23 +38,33 @@ describe("withCodSpeed behavior without different codspeed modes", () => {
3838
expect(shouldBeCalled.mock.calls.length).toBeGreaterThan(1000);
3939
});
4040

41-
it("should run in simulation mode when CODSPEED_RUNNER_MODE=instrumentation", async () => {
41+
it("should run in analysis mode when CODSPEED_RUNNER_MODE=instrumentation", async () => {
4242
process.env.CODSPEED_ENV = "true";
4343
process.env.CODSPEED_RUNNER_MODE = "instrumentation";
4444

4545
withCodSpeed(new Bench());
4646

47-
expect(mockSimulation.setupCodspeedSimulationBench).toHaveBeenCalled();
47+
expect(mockAnalysis.setupCodspeedAnalysisBench).toHaveBeenCalled();
4848
expect(mockWalltime.setupCodspeedWalltimeBench).not.toHaveBeenCalled();
4949
});
5050

51-
it("should run in simulation mode when CODSPEED_RUNNER_MODE=simulation", async () => {
51+
it("should run in analysis mode when CODSPEED_RUNNER_MODE=simulation", async () => {
5252
process.env.CODSPEED_ENV = "true";
5353
process.env.CODSPEED_RUNNER_MODE = "simulation";
5454

5555
withCodSpeed(new Bench());
5656

57-
expect(mockSimulation.setupCodspeedSimulationBench).toHaveBeenCalled();
57+
expect(mockAnalysis.setupCodspeedAnalysisBench).toHaveBeenCalled();
58+
expect(mockWalltime.setupCodspeedWalltimeBench).not.toHaveBeenCalled();
59+
});
60+
61+
it("should run in analysis mode when CODSPEED_RUNNER_MODE=memory", async () => {
62+
process.env.CODSPEED_ENV = "true";
63+
process.env.CODSPEED_RUNNER_MODE = "memory";
64+
65+
withCodSpeed(new Bench());
66+
67+
expect(mockAnalysis.setupCodspeedAnalysisBench).toHaveBeenCalled();
5868
expect(mockWalltime.setupCodspeedWalltimeBench).not.toHaveBeenCalled();
5969
});
6070

@@ -64,7 +74,7 @@ describe("withCodSpeed behavior without different codspeed modes", () => {
6474

6575
withCodSpeed(new Bench());
6676

67-
expect(mockSimulation.setupCodspeedSimulationBench).not.toHaveBeenCalled();
77+
expect(mockAnalysis.setupCodspeedAnalysisBench).not.toHaveBeenCalled();
6878
expect(mockWalltime.setupCodspeedWalltimeBench).toHaveBeenCalled();
6979
});
7080
});

packages/vitest-plugin/rollup.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export default defineConfig([
2121
external: ["@codspeed/core", /^vitest/],
2222
},
2323
{
24-
input: "src/simulation.ts",
25-
output: { file: "dist/simulation.mjs", format: "es" },
24+
input: "src/analysis.ts",
25+
output: { file: "dist/analysis.mjs", format: "es" },
2626
plugins: jsPlugins(pkg.version),
2727
external: ["@codspeed/core", /^vitest/],
2828
},

packages/vitest-plugin/src/__tests__/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe("codSpeedPlugin", () => {
126126
"--max-old-space-size=4096",
127127
],
128128
runner: expect.stringContaining(
129-
"packages/vitest-plugin/src/simulation.ts"
129+
"packages/vitest-plugin/src/analysis.ts"
130130
),
131131
},
132132
});
@@ -167,7 +167,7 @@ describe("codSpeedPlugin", () => {
167167
},
168168
},
169169
runner: expect.stringContaining(
170-
"packages/vitest-plugin/src/simulation.ts"
170+
"packages/vitest-plugin/src/analysis.ts"
171171
),
172172
},
173173
});

0 commit comments

Comments
 (0)