Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .moon/toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ node:
pnpm:
version: "8.6.3"

aliasPackageNames: "name-only"
dedupeOnLockfileChange: false
dependencyVersionFormat: "workspace"
inferTasksFromScripts: true
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.rollup.cache
dist
generated
2 changes: 1 addition & 1 deletion examples/with-typescript-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"bench-benchmark-js": "node --loader esbuild-register/loader -r esbuild-register bench/benchmark.js/index.bench.ts",
"bench-tinybench": "node --loader esbuild-register/loader -r esbuild-register bench/tinybench/index.bench.ts",
"bench-vitest": "vitest bench"
"bench-vitest": "vitest bench --run"
},
"devDependencies": {
"@codspeed/benchmark.js-plugin": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"devDependencies": {
"@commitlint/cli": "^17.5.1",
"@commitlint/config-conventional": "^17.4.4",
"@moonrepo/cli": "^1.15.4",
"@moonrepo/cli": "^1.19.3",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
Expand Down
22 changes: 22 additions & 0 deletions packages/benchmark.js-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {
Measurement,
mongoMeasurement,
optimizeFunction,
optimizeFunctionSync,
setupCore,
SetupInstrumentsRequestBody,
SetupInstrumentsResponse,
teardownCore,
tryIntrospect,
} from "@codspeed/core";
Expand Down Expand Up @@ -192,18 +195,22 @@ async function runBenchmarks({

if (isAsync) {
await optimizeFunction(benchPayload);
await mongoMeasurement.start(uri);
await (async function __codspeed_root_frame__() {
Measurement.startInstrumentation();
await benchPayload();
Measurement.stopInstrumentation(uri);
})();
await mongoMeasurement.stop(uri);
} else {
optimizeFunctionSync(benchPayload);
await mongoMeasurement.start(uri);
(function __codspeed_root_frame__() {
Measurement.startInstrumentation();
benchPayload();
Measurement.stopInstrumentation(uri);
})();
await mongoMeasurement.stop(uri);
}

if (typeof bench.options.teardown === "function") {
Expand All @@ -216,3 +223,18 @@ async function runBenchmarks({
teardownCore();
console.log(`[CodSpeed] Done running ${benches.length} benches.`);
}

/**
* Dynamically setup the CodSpeed instruments.
*/
export async function setupInstruments(
body: SetupInstrumentsRequestBody
): Promise<SetupInstrumentsResponse> {
if (!Measurement.isInstrumented()) {
console.warn("[CodSpeed] No instrumentation found, using default mongoUrl");

return { remoteAddr: body.mongoUrl };
}

return await mongoMeasurement.setupInstruments(body);
}
3 changes: 2 additions & 1 deletion packages/core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
prebuilds
prebuilds
generated
19 changes: 18 additions & 1 deletion packages/core/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ tasks:
clean:
args:
- build
- generated/openapi
build:
deps:
- "build-native-addon"
- build-native-addon
- build-tracer-client

build-native-addon:
command: prebuildify --napi --strip
Expand All @@ -14,3 +16,18 @@ tasks:
- "binding.gyp"
outputs:
- "prebuilds"

build-tracer-client:
inputs:
- "./tracer.spec.json"
outputs:
- "src/generated/openapi"
command: openapi --client axios --input ./tracer.spec.json --name MongoTracer --output ./src/generated/openapi

typecheck:
deps:
- build-tracer-client

lint:
deps:
- build-tracer-client
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
"@types/find-up": "^4.0.0",
"node-addon-api": "^5.1.0",
"node-gyp": "^9.3.1",
"openapi-typescript-codegen": "^0.23.0",
"prebuildify": "^5.0.1"
},
"dependencies": {
"axios": "^1.4.0",
"find-up": "^6.3.0",
"form-data": "^4.0.0",
"node-gyp-build": "^4.6.0"
}
}
7 changes: 7 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkV8Flags } from "./introspection";
import { MongoMeasurement } from "./mongoMeasurement";
import native_core from "./native_core";
import { initOptimization } from "./optimization";

Expand All @@ -8,6 +9,8 @@ const linuxPerf = new native_core.LinuxPerf();

export const isBound = native_core.isBound;

export const mongoMeasurement = new MongoMeasurement();

export const setupCore = () => {
initOptimization();
native_core.Measurement.stopInstrumentation(
Expand All @@ -21,6 +24,10 @@ export const teardownCore = () => {
linuxPerf.stop();
};

export type {
SetupInstrumentsRequestBody,
SetupInstrumentsResponse,
} from "./generated/openapi";
export { getV8Flags, tryIntrospect } from "./introspection";
export { optimizeFunction, optimizeFunctionSync } from "./optimization";
export * from "./utils";
Expand Down
46 changes: 46 additions & 0 deletions packages/core/src/mongoMeasurement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
MongoTracer,
SetupInstrumentsRequestBody,
SetupInstrumentsResponse,
} from "./generated/openapi";

export type { SetupInstrumentsRequestBody };

export class MongoMeasurement {
private tracerClient: MongoTracer | undefined;

constructor() {
const serverUrl = process.env.CODSPEED_MONGO_INSTR_SERVER_ADDRESS;

if (serverUrl !== undefined) {
this.tracerClient = new MongoTracer({
BASE: serverUrl,
});
}
}

public async setupInstruments(
body: SetupInstrumentsRequestBody
): Promise<SetupInstrumentsResponse> {
if (this.tracerClient === undefined) {
throw new Error("MongoDB Instrumentation is not enabled");
}
return await this.tracerClient.instruments.setup(body);
}

public async start(uri: string) {
if (this.tracerClient !== undefined) {
await this.tracerClient.instrumentation.start({
uri,
});
}
}

public async stop(uri: string) {
if (this.tracerClient !== undefined) {
await this.tracerClient.instrumentation.stop({
uri,
});
}
}
}
Loading