Skip to content

Commit ad8b539

Browse files
feat: use instrument-hooks for all instrumented integrations
1 parent ca24ce7 commit ad8b539

File tree

16 files changed

+117
-172
lines changed

16 files changed

+117
-172
lines changed

packages/benchmark.js-plugin/src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
Measurement,
2+
InstrumentHooks,
33
mongoMeasurement,
44
optimizeFunction,
55
optimizeFunctionSync,
@@ -90,7 +90,7 @@ export function withCodSpeed(item: unknown): unknown {
9090
}
9191

9292
function withCodSpeedBenchmark(bench: Benchmark): WithCodSpeedBenchmark {
93-
if (!Measurement.isInstrumented()) {
93+
if (!InstrumentHooks.isInstrumented()) {
9494
const rawRun = bench.run;
9595
bench.run = (options?: Benchmark.Options) => {
9696
console.warn(
@@ -120,7 +120,7 @@ function withCodSpeedBenchmark(bench: Benchmark): WithCodSpeedBenchmark {
120120
}
121121

122122
function withCodSpeedSuite(suite: Benchmark.Suite): WithCodSpeedSuite {
123-
if (!Measurement.isInstrumented()) {
123+
if (!InstrumentHooks.isInstrumented()) {
124124
const rawRun = suite.run;
125125
suite.run = (options?: Benchmark.Options) => {
126126
console.warn(
@@ -198,18 +198,20 @@ async function runBenchmarks({
198198
await mongoMeasurement.start(uri);
199199
global.gc?.();
200200
await (async function __codspeed_root_frame__() {
201-
Measurement.startInstrumentation();
201+
InstrumentHooks.startBenchmark();
202202
await benchPayload();
203-
Measurement.stopInstrumentation(uri);
203+
InstrumentHooks.stopBenchmark();
204+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
204205
})();
205206
await mongoMeasurement.stop(uri);
206207
} else {
207208
optimizeFunctionSync(benchPayload);
208209
await mongoMeasurement.start(uri);
209210
(function __codspeed_root_frame__() {
210-
Measurement.startInstrumentation();
211+
InstrumentHooks.startBenchmark();
211212
benchPayload();
212-
Measurement.stopInstrumentation(uri);
213+
InstrumentHooks.stopBenchmark();
214+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
213215
})();
214216
await mongoMeasurement.stop(uri);
215217
}
@@ -231,7 +233,7 @@ async function runBenchmarks({
231233
export async function setupInstruments(
232234
body: SetupInstrumentsRequestBody
233235
): Promise<SetupInstrumentsResponse> {
234-
if (!Measurement.isInstrumented()) {
236+
if (!InstrumentHooks.isInstrumented()) {
235237
console.warn("[CodSpeed] No instrumentation found, using default mongoUrl");
236238

237239
return { remoteAddr: body.mongoUrl };

packages/benchmark.js-plugin/tests/index.integ.test.ts

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const benchOptions: Benchmark.Options = {
2323

2424
describe("Benchmark", () => {
2525
it("simple benchmark", async () => {
26-
mockCore.Measurement.isInstrumented.mockReturnValue(false);
26+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(false);
2727
const bench = withCodSpeed(
2828
new Benchmark(
2929
"RegExp",
@@ -37,11 +37,12 @@ describe("Benchmark", () => {
3737
bench.on("complete", onComplete);
3838
await bench.run();
3939
expect(onComplete).toHaveBeenCalled();
40-
expect(mockCore.Measurement.startInstrumentation).not.toHaveBeenCalled();
41-
expect(mockCore.Measurement.stopInstrumentation).not.toHaveBeenCalled();
40+
expect(mockCore.InstrumentHooks.startBenchmark).not.toHaveBeenCalled();
41+
expect(mockCore.InstrumentHooks.stopBenchmark).not.toHaveBeenCalled();
42+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).not.toHaveBeenCalled();
4243
});
4344
it("check core methods are called", async () => {
44-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
45+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
4546

4647
const bench = withCodSpeed(
4748
new Benchmark(
@@ -56,13 +57,15 @@ describe("Benchmark", () => {
5657
bench.on("complete", onComplete);
5758
await bench.run();
5859
expect(onComplete).toHaveBeenCalled();
59-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
60-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
60+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
61+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalled();
62+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
63+
process.pid,
6164
"packages/benchmark.js-plugin/tests/index.integ.test.ts::RegExpSingle"
6265
);
6366
});
6467
it("check error handling", async () => {
65-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
68+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
6669
const bench = withCodSpeed(
6770
new Benchmark(
6871
"throwing",
@@ -79,7 +82,7 @@ describe("Benchmark", () => {
7982
async (instrumented) => {
8083
const logSpy = jest.spyOn(console, "log");
8184
const warnSpy = jest.spyOn(console, "warn");
82-
mockCore.Measurement.isInstrumented.mockReturnValue(instrumented);
85+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(instrumented);
8386
await withCodSpeed(
8487
new Benchmark(
8588
"RegExpSingle",
@@ -108,7 +111,7 @@ describe("Benchmark", () => {
108111
}
109112
);
110113
it("should call setup and teardown", async () => {
111-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
114+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
112115
const setup = jest.fn();
113116
const teardown = jest.fn();
114117
const bench = withCodSpeed(
@@ -128,7 +131,7 @@ describe("Benchmark", () => {
128131

129132
describe("Benchmark.Suite", () => {
130133
it("simple suite", async () => {
131-
mockCore.Measurement.isInstrumented.mockReturnValue(false);
134+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(false);
132135
const suite = withCodSpeed(new Benchmark.Suite());
133136
suite.add(
134137
"RegExp",
@@ -141,11 +144,12 @@ describe("Benchmark.Suite", () => {
141144
suite.on("complete", onComplete);
142145
await suite.run({ maxTime: 0.1, initCount: 1 });
143146
expect(onComplete).toHaveBeenCalled();
144-
expect(mockCore.Measurement.startInstrumentation).not.toHaveBeenCalled();
145-
expect(mockCore.Measurement.stopInstrumentation).not.toHaveBeenCalled();
147+
expect(mockCore.InstrumentHooks.startBenchmark).not.toHaveBeenCalled();
148+
expect(mockCore.InstrumentHooks.stopBenchmark).not.toHaveBeenCalled();
149+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).not.toHaveBeenCalled();
146150
});
147151
it("check core methods are called", async () => {
148-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
152+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
149153
const suite = withCodSpeed(new Benchmark.Suite()).add(
150154
"RegExp",
151155
function () {
@@ -156,13 +160,15 @@ describe("Benchmark.Suite", () => {
156160
const onComplete = jest.fn();
157161
suite.on("complete", onComplete);
158162
await suite.run({ maxTime: 0.1, initCount: 1 });
159-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
160-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
163+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
164+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalled();
165+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
166+
process.pid,
161167
"packages/benchmark.js-plugin/tests/index.integ.test.ts::RegExp"
162168
);
163169
});
164170
it("check suite name is in the uri", async () => {
165-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
171+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
166172
await withCodSpeed(new Benchmark.Suite("thesuite"))
167173
.add(
168174
"RegExp",
@@ -175,15 +181,18 @@ describe("Benchmark.Suite", () => {
175181
/o/.test("Hello World!");
176182
}, benchOptions)
177183
.run();
178-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
184+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalledTimes(2);
185+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
186+
process.pid,
179187
"packages/benchmark.js-plugin/tests/index.integ.test.ts::thesuite::RegExp"
180188
);
181-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
189+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
190+
process.pid,
182191
"packages/benchmark.js-plugin/tests/index.integ.test.ts::thesuite::unknown_1"
183192
);
184193
});
185194
it("check error handling", async () => {
186-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
195+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
187196
const bench = withCodSpeed(new Benchmark.Suite("thesuite")).add(
188197
"throwing",
189198
() => {
@@ -197,7 +206,7 @@ describe("Benchmark.Suite", () => {
197206
async (instrumented) => {
198207
const logSpy = jest.spyOn(console, "log");
199208
const warnSpy = jest.spyOn(console, "warn");
200-
mockCore.Measurement.isInstrumented.mockReturnValue(instrumented);
209+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(instrumented);
201210
await withCodSpeed(new Benchmark.Suite("thesuite"))
202211
.add(
203212
"RegExp",
@@ -229,35 +238,40 @@ describe("Benchmark.Suite", () => {
229238
}
230239
);
231240
it("check nested file path is in the uri when bench is registered in another file", async () => {
232-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
241+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
233242
const suite = withCodSpeed(new Benchmark.Suite("thesuite"));
234243
registerBenchmarks(suite);
235244
const onComplete = jest.fn();
236245
suite.on("complete", onComplete);
237246
await suite.run({ maxTime: 0.1, initCount: 1 });
238-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
239-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
247+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
248+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalled();
249+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
250+
process.pid,
240251
"packages/benchmark.js-plugin/tests/registerBenchmarks.ts::thesuite::RegExp"
241252
);
242253
});
243254
it("check that benchmarks with same name have different URIs when registered in different files", async () => {
244-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
255+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
245256
const suite = withCodSpeed(new Benchmark.Suite("thesuite"));
246257
registerBenchmarks(suite);
247258
registerOtherBenchmarks(suite);
248259
const onComplete = jest.fn();
249260
suite.on("complete", onComplete);
250261
await suite.run({ maxTime: 0.1, initCount: 1 });
251-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
252-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
262+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
263+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalledTimes(2);
264+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
265+
process.pid,
253266
"packages/benchmark.js-plugin/tests/registerBenchmarks.ts::thesuite::RegExp"
254267
);
255-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
268+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
269+
process.pid,
256270
"packages/benchmark.js-plugin/tests/registerOtherBenchmarks.ts::thesuite::RegExp"
257271
);
258272
});
259273
it("should call setupCore and teardownCore only once after run()", async () => {
260-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
274+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
261275
const suite = withCodSpeed(new Benchmark.Suite("thesuite"));
262276
registerBenchmarks(suite);
263277
registerOtherBenchmarks(suite);
@@ -271,7 +285,7 @@ describe("Benchmark.Suite", () => {
271285
expect(mockCore.teardownCore).toHaveBeenCalledTimes(1);
272286
});
273287
it("should call setup and teardown", async () => {
274-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
288+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
275289
const setup = jest.fn();
276290
const teardown = jest.fn();
277291

packages/core/binding.gyp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"-Wno-type-limits"
2424
],
2525
"sources": [
26-
"src/native_core/measurement/measurement.cc",
2726
"src/native_core/linux_perf/linux_perf.cc",
2827
"src/native_core/linux_perf/linux_perf_listener.cc",
2928
"src/native_core/instruments/hooks_wrapper.cc",

packages/core/src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ export function getCodspeedRunnerMode(): CodSpeedRunnerMode {
3333
}
3434

3535
export const setupCore = () => {
36-
native_core.Measurement.stopInstrumentation(
37-
`Metadata: codspeed-node ${__VERSION__}`
38-
);
36+
native_core.InstrumentHooks.setIntegration("codspeed-node", __VERSION__);
3937
linuxPerf.start();
4038
checkV8Flags();
4139
};
@@ -52,5 +50,4 @@ export { getV8Flags, tryIntrospect } from "./introspection";
5250
export { optimizeFunction, optimizeFunctionSync } from "./optimization";
5351
export * from "./utils";
5452
export * from "./walltime";
55-
export const Measurement = native_core.Measurement;
5653
export const InstrumentHooks = native_core.InstrumentHooks;

packages/core/src/native_core/index.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import path from "path";
22
import { InstrumentHooks } from "./instruments/hooks";
33
import { LinuxPerf } from "./linux_perf/linux_perf";
4-
import { Measurement } from "./measurement/measurement";
54
interface NativeCore {
6-
Measurement: Measurement;
75
InstrumentHooks: InstrumentHooks;
86
LinuxPerf: typeof LinuxPerf;
97
}
@@ -24,13 +22,6 @@ try {
2422
};
2523
} catch (e) {
2624
native_core = {
27-
Measurement: {
28-
isInstrumented: () => false,
29-
// eslint-disable-next-line @typescript-eslint/no-empty-function
30-
startInstrumentation: () => {},
31-
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
32-
stopInstrumentation: (at) => {},
33-
},
3425
LinuxPerf: class LinuxPerf {
3526
start() {
3627
return false;

packages/core/src/native_core/measurement/measurement.cc

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

packages/core/src/native_core/measurement/measurement.h

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

packages/core/src/native_core/measurement/measurement.ts

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

packages/core/src/native_core/native_core.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include "linux_perf/linux_perf.h"
2-
#include "measurement/measurement.h"
32
#include "instruments/hooks_wrapper.h"
43
#include <napi.h>
54

65
namespace codspeed_native {
76

87
Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
98
codspeed_native::LinuxPerf::Initialize(env, exports);
10-
codspeed_native::Measurement::Initialize(env, exports);
119
codspeed_native::instruments::hooks_wrapper::Initialize(env, exports);
1210

1311
return exports;

0 commit comments

Comments
 (0)