Skip to content

Commit 5f0f8cc

Browse files
committed
feat: add memory profiling support
1 parent 635c77c commit 5f0f8cc

14 files changed

Lines changed: 85 additions & 44 deletions

File tree

.github/workflows/codspeed.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,30 @@ 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
3445
pnpm moon run benchmark.js-plugin:bench
3546
pnpm --workspace-concurrency 1 -r bench-tinybench
3647
pnpm --workspace-concurrency 1 -r bench-benchmark-js
3748
pnpm --workspace-concurrency 1 -r bench-vitest
49+
upload-url: https://api.staging.preview.codspeed.io/upload
3850

3951
codspeed-walltime:
4052
name: Run CodSpeed walltime

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 BuildMode = "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 getBuildMode(): BuildMode {
43+
const runnerMode = getCodspeedRunnerMode();
44+
// Both "simulation" and "memory" map to "analysis" build 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(
@@ -55,6 +68,7 @@ export type {
5568
SetupInstrumentsRequestBody,
5669
SetupInstrumentsResponse,
5770
} from "./generated/openapi";
71+
export type { BuildMode };
5872
export { getV8Flags, tryIntrospect } from "./introspection";
5973
export { optimizeFunction, optimizeFunctionSync } from "./optimization";
6074
export * from "./utils";

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 { getBuildMode } 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 buildMode = getBuildMode();
99

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

12-
if (codspeedRunnerMode === "simulation") {
12+
if (buildMode === "analysis") {
1313
flags.push(
1414
...[
1515
"--hash-seed=1",
Submodule hooks updated from b3d4b78 to 69b88f7

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import {
66
import { Bench, Fn, FnOptions, Task } from "tinybench";
77
import { BaseBenchRunner } from "./shared";
88

9-
export function setupCodspeedSimulationBench(
9+
export function setupCodspeedAnalysisBench(
1010
bench: Bench,
1111
rootCallingFile: string
1212
): void {
13-
const runner = new SimulationBenchRunner(bench, rootCallingFile);
13+
const runner = new AnalysisBenchRunner(bench, rootCallingFile);
1414
runner.setupBenchMethods();
1515
}
1616

17-
class SimulationBenchRunner extends BaseBenchRunner {
17+
class AnalysisBenchRunner extends BaseBenchRunner {
1818
protected getModeName(): string {
19-
return "simulation mode";
19+
return "analysis mode";
2020
}
2121

2222
private taskCompletionMessage() {

packages/tinybench-plugin/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
getBuildMode,
23
getCodspeedRunnerMode,
34
getGitDir,
45
InstrumentHooks,
@@ -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 buildMode = getBuildMode();
44+
if (buildMode === "analysis") {
45+
setupCodspeedAnalysisBench(bench, rootCallingFile);
46+
} else if (buildMode === "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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe("codSpeedPlugin", () => {
107107
},
108108
},
109109
runner: expect.stringContaining(
110-
"packages/vitest-plugin/src/simulation.ts"
110+
"packages/vitest-plugin/src/analysis.ts"
111111
),
112112
},
113113
});

0 commit comments

Comments
 (0)