Skip to content

Commit 76868f8

Browse files
committed
feat: integrate CodSpeed Mongo Tracer
1 parent 9c3d572 commit 76868f8

9 files changed

Lines changed: 643 additions & 18 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Measurement,
3+
mongoMeasurement,
34
optimizeFunction,
45
optimizeFunctionSync,
56
setupCore,
@@ -183,6 +184,7 @@ async function runBenchmarks({
183184
benchPayload = bench.fn as CallableFunction;
184185
}
185186

187+
await mongoMeasurement.start(uri);
186188
if (isAsync) {
187189
await optimizeFunction(benchPayload);
188190
await (async function __codspeed_root_frame__() {
@@ -198,6 +200,8 @@ async function runBenchmarks({
198200
Measurement.stopInstrumentation(uri);
199201
})();
200202
}
203+
await mongoMeasurement.stop(uri);
204+
201205
console.log(` ✔ Measured ${uri}`);
202206
benchmarkCompletedListeners.forEach((listener) => listener());
203207
teardownCore();

packages/core/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
build
2-
prebuilds
2+
prebuilds
3+
generated

packages/core/moon.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ tasks:
22
clean:
33
args:
44
- build
5+
- generated/openapi
56
build:
67
deps:
78
- "build-native-addon"
@@ -14,3 +15,10 @@ tasks:
1415
- "binding.gyp"
1516
outputs:
1617
- "prebuilds"
18+
19+
build-tracer-client:
20+
inputs:
21+
- "./tracer.spec.json"
22+
outputs:
23+
- "src/generated/openapi"
24+
command: openapi --client axios --input ./tracer.spec.json --name MongoTracer --output ./src/generated/openapi

packages/core/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
"devDependencies": {
2323
"node-addon-api": "^5.1.0",
2424
"node-gyp": "^9.3.1",
25+
"openapi-typescript-codegen": "^0.23.0",
2526
"prebuildify": "^5.0.1"
2627
},
2728
"dependencies": {
29+
"axios": "^1.4.0",
2830
"node-gyp-build": "^4.6.0"
2931
}
3032
}

packages/core/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { tryIntrospect } from "./introspection";
2+
import { MongoMeasurement } from "./mongoMeasurement";
23
tryIntrospect();
34

45
import native_core from "./native_core";
@@ -10,12 +11,15 @@ const linuxPerf = new native_core.LinuxPerf();
1011

1112
export const isBound = native_core.isBound;
1213

14+
export let mongoMeasurement: MongoMeasurement;
15+
1316
export const setupCore = () => {
1417
initOptimization();
1518
native_core.Measurement.stopInstrumentation(
1619
`Metadata: codspeed-node ${__VERSION__}`
1720
);
1821
linuxPerf.start();
22+
mongoMeasurement = new MongoMeasurement();
1923
};
2024

2125
export const teardownCore = () => {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { MongoTracer } from "./generated/openapi";
2+
3+
export class MongoMeasurement {
4+
private tracerClient: MongoTracer | undefined;
5+
6+
constructor() {
7+
const serverUrl = process.env.CODSPEED_MONGO_INSTR_SERVER_ADDRESS;
8+
// TODO
9+
const mongoUriEnvName = process.env.CODSPEED_MONGO_INSTR_URI_ENV_NAME;
10+
if (mongoUriEnvName === undefined) {
11+
throw new Error("CODSPEED_MONGO_INSTR_URI_ENV_NAME is not defined");
12+
}
13+
const mongoUri = process.env[mongoUriEnvName];
14+
if (mongoUri === undefined) {
15+
throw new Error(`Environment variable ${mongoUriEnvName} is not defined`);
16+
}
17+
process.env[mongoUriEnvName] =
18+
"mongodb://localhost:27018?directConnection=true";
19+
20+
if (serverUrl !== undefined) {
21+
this.tracerClient = new MongoTracer({
22+
BASE: serverUrl,
23+
});
24+
}
25+
}
26+
27+
public async start(uri: string) {
28+
if (this.tracerClient !== undefined) {
29+
await this.tracerClient.instrumentation.start({
30+
uri,
31+
});
32+
}
33+
}
34+
35+
public async stop(uri: string) {
36+
if (this.tracerClient !== undefined) {
37+
await this.tracerClient.instrumentation.stop({
38+
uri,
39+
});
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)