Skip to content

Commit 094b54b

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

File tree

16 files changed

+121
-172
lines changed

16 files changed

+121
-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: 47 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,14 @@ 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(
43+
mockCore.InstrumentHooks.setExecutedBenchmark
44+
).not.toHaveBeenCalled();
4245
});
4346
it("check core methods are called", async () => {
44-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
47+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
4548

4649
const bench = withCodSpeed(
4750
new Benchmark(
@@ -56,13 +59,15 @@ describe("Benchmark", () => {
5659
bench.on("complete", onComplete);
5760
await bench.run();
5861
expect(onComplete).toHaveBeenCalled();
59-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
60-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
62+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
63+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalled();
64+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
65+
process.pid,
6166
"packages/benchmark.js-plugin/tests/index.integ.test.ts::RegExpSingle"
6267
);
6368
});
6469
it("check error handling", async () => {
65-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
70+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
6671
const bench = withCodSpeed(
6772
new Benchmark(
6873
"throwing",
@@ -79,7 +84,7 @@ describe("Benchmark", () => {
7984
async (instrumented) => {
8085
const logSpy = jest.spyOn(console, "log");
8186
const warnSpy = jest.spyOn(console, "warn");
82-
mockCore.Measurement.isInstrumented.mockReturnValue(instrumented);
87+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(instrumented);
8388
await withCodSpeed(
8489
new Benchmark(
8590
"RegExpSingle",
@@ -108,7 +113,7 @@ describe("Benchmark", () => {
108113
}
109114
);
110115
it("should call setup and teardown", async () => {
111-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
116+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
112117
const setup = jest.fn();
113118
const teardown = jest.fn();
114119
const bench = withCodSpeed(
@@ -128,7 +133,7 @@ describe("Benchmark", () => {
128133

129134
describe("Benchmark.Suite", () => {
130135
it("simple suite", async () => {
131-
mockCore.Measurement.isInstrumented.mockReturnValue(false);
136+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(false);
132137
const suite = withCodSpeed(new Benchmark.Suite());
133138
suite.add(
134139
"RegExp",
@@ -141,11 +146,14 @@ describe("Benchmark.Suite", () => {
141146
suite.on("complete", onComplete);
142147
await suite.run({ maxTime: 0.1, initCount: 1 });
143148
expect(onComplete).toHaveBeenCalled();
144-
expect(mockCore.Measurement.startInstrumentation).not.toHaveBeenCalled();
145-
expect(mockCore.Measurement.stopInstrumentation).not.toHaveBeenCalled();
149+
expect(mockCore.InstrumentHooks.startBenchmark).not.toHaveBeenCalled();
150+
expect(mockCore.InstrumentHooks.stopBenchmark).not.toHaveBeenCalled();
151+
expect(
152+
mockCore.InstrumentHooks.setExecutedBenchmark
153+
).not.toHaveBeenCalled();
146154
});
147155
it("check core methods are called", async () => {
148-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
156+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
149157
const suite = withCodSpeed(new Benchmark.Suite()).add(
150158
"RegExp",
151159
function () {
@@ -156,13 +164,15 @@ describe("Benchmark.Suite", () => {
156164
const onComplete = jest.fn();
157165
suite.on("complete", onComplete);
158166
await suite.run({ maxTime: 0.1, initCount: 1 });
159-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
160-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
167+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
168+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalled();
169+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
170+
process.pid,
161171
"packages/benchmark.js-plugin/tests/index.integ.test.ts::RegExp"
162172
);
163173
});
164174
it("check suite name is in the uri", async () => {
165-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
175+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
166176
await withCodSpeed(new Benchmark.Suite("thesuite"))
167177
.add(
168178
"RegExp",
@@ -175,15 +185,18 @@ describe("Benchmark.Suite", () => {
175185
/o/.test("Hello World!");
176186
}, benchOptions)
177187
.run();
178-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
188+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalledTimes(2);
189+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
190+
process.pid,
179191
"packages/benchmark.js-plugin/tests/index.integ.test.ts::thesuite::RegExp"
180192
);
181-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
193+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
194+
process.pid,
182195
"packages/benchmark.js-plugin/tests/index.integ.test.ts::thesuite::unknown_1"
183196
);
184197
});
185198
it("check error handling", async () => {
186-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
199+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
187200
const bench = withCodSpeed(new Benchmark.Suite("thesuite")).add(
188201
"throwing",
189202
() => {
@@ -197,7 +210,7 @@ describe("Benchmark.Suite", () => {
197210
async (instrumented) => {
198211
const logSpy = jest.spyOn(console, "log");
199212
const warnSpy = jest.spyOn(console, "warn");
200-
mockCore.Measurement.isInstrumented.mockReturnValue(instrumented);
213+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(instrumented);
201214
await withCodSpeed(new Benchmark.Suite("thesuite"))
202215
.add(
203216
"RegExp",
@@ -229,35 +242,40 @@ describe("Benchmark.Suite", () => {
229242
}
230243
);
231244
it("check nested file path is in the uri when bench is registered in another file", async () => {
232-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
245+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
233246
const suite = withCodSpeed(new Benchmark.Suite("thesuite"));
234247
registerBenchmarks(suite);
235248
const onComplete = jest.fn();
236249
suite.on("complete", onComplete);
237250
await suite.run({ maxTime: 0.1, initCount: 1 });
238-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
239-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
251+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
252+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalled();
253+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
254+
process.pid,
240255
"packages/benchmark.js-plugin/tests/registerBenchmarks.ts::thesuite::RegExp"
241256
);
242257
});
243258
it("check that benchmarks with same name have different URIs when registered in different files", async () => {
244-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
259+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
245260
const suite = withCodSpeed(new Benchmark.Suite("thesuite"));
246261
registerBenchmarks(suite);
247262
registerOtherBenchmarks(suite);
248263
const onComplete = jest.fn();
249264
suite.on("complete", onComplete);
250265
await suite.run({ maxTime: 0.1, initCount: 1 });
251-
expect(mockCore.Measurement.startInstrumentation).toHaveBeenCalled();
252-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
266+
expect(mockCore.InstrumentHooks.startBenchmark).toHaveBeenCalled();
267+
expect(mockCore.InstrumentHooks.stopBenchmark).toHaveBeenCalledTimes(2);
268+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
269+
process.pid,
253270
"packages/benchmark.js-plugin/tests/registerBenchmarks.ts::thesuite::RegExp"
254271
);
255-
expect(mockCore.Measurement.stopInstrumentation).toHaveBeenCalledWith(
272+
expect(mockCore.InstrumentHooks.setExecutedBenchmark).toHaveBeenCalledWith(
273+
process.pid,
256274
"packages/benchmark.js-plugin/tests/registerOtherBenchmarks.ts::thesuite::RegExp"
257275
);
258276
});
259277
it("should call setupCore and teardownCore only once after run()", async () => {
260-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
278+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
261279
const suite = withCodSpeed(new Benchmark.Suite("thesuite"));
262280
registerBenchmarks(suite);
263281
registerOtherBenchmarks(suite);
@@ -271,7 +289,7 @@ describe("Benchmark.Suite", () => {
271289
expect(mockCore.teardownCore).toHaveBeenCalledTimes(1);
272290
});
273291
it("should call setup and teardown", async () => {
274-
mockCore.Measurement.isInstrumented.mockReturnValue(true);
292+
mockCore.InstrumentHooks.isInstrumented.mockReturnValue(true);
275293
const setup = jest.fn();
276294
const teardown = jest.fn();
277295

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)