Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ export const setupCore = () => {
native_core.InstrumentHooks.setIntegration("codspeed-node", __VERSION__);
linuxPerf.start();
checkV8Flags();

// Collect Node.js runtime environment to detect changes that could
// cause performance differences across runs
const hooks = native_core.InstrumentHooks;
hooks.setEnvironment("Node.js", "version", process.versions.node);
hooks.setEnvironment("Node.js", "v8", process.versions.v8);
hooks.setEnvironment("Node.js", "arch", process.arch);
hooks.writeEnvironment(process.pid);
};

export const teardownCore = () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/native_core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ try {
setIntegration: (_name: string, _version: string) => {
return 0;
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setEnvironment: (_sectionName: string, _key: string, _value: string) => {
return 0;
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
writeEnvironment: (_pid: number) => {
return 0;
},
__codspeed_root_frame__: <T>(callback: () => T): T => {
return callback();
},
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/native_core/instruments/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ export interface InstrumentHooks {
*/
setIntegration(name: string, version: string): number;

/**
* Register a key-value pair under a named environment section.
* @param sectionName Section name (e.g. "Node.js")
* @param key Key name (e.g. "version")
* @param value Value (e.g. "22.0.0")
* @returns 0 on success, non-zero on error
*/
setEnvironment(sectionName: string, key: string, value: string): number;

/**
* Flush all registered environment sections to disk.
* @param pid Process ID
* @returns 0 on success, non-zero on error
*/
writeEnvironment(pid: number): number;

/**
* Execute a callback function with __codspeed_root_frame__ in its stack trace
* @param callback Function to execute
Expand Down
52 changes: 52 additions & 0 deletions packages/core/src/native_core/instruments/hooks_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ Napi::Number SetIntegration(const Napi::CallbackInfo &info) {
return Napi::Number::New(env, result);
}

Napi::Number SetEnvironment(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

if (info.Length() != 3) {
Napi::TypeError::New(
env, "Expected 3 arguments: sectionName, key, and value")
.ThrowAsJavaScriptException();
return Napi::Number::New(env, 1);
}

if (!info[0].IsString() || !info[1].IsString() || !info[2].IsString()) {
Napi::TypeError::New(
env,
"Expected string (sectionName), string (key), and string (value)")
.ThrowAsJavaScriptException();
return Napi::Number::New(env, 1);
}

std::string section_name = info[0].As<Napi::String>().Utf8Value();
std::string key = info[1].As<Napi::String>().Utf8Value();
std::string value = info[2].As<Napi::String>().Utf8Value();

uint8_t result = instrument_hooks_set_environment(
hooks, section_name.c_str(), key.c_str(), value.c_str());
return Napi::Number::New(env, result);
}

Napi::Number WriteEnvironment(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

if (info.Length() != 1) {
Napi::TypeError::New(env, "Expected 1 argument: pid")
.ThrowAsJavaScriptException();
return Napi::Number::New(env, 1);
}

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Expected number (pid)")
.ThrowAsJavaScriptException();
return Napi::Number::New(env, 1);
}

uint32_t pid = info[0].As<Napi::Number>().Uint32Value();

uint8_t result = instrument_hooks_write_environment(hooks, pid);
return Napi::Number::New(env, result);
}

Napi::Value __attribute__ ((noinline)) __codspeed_root_frame__(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

Expand Down Expand Up @@ -117,6 +165,10 @@ Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
Napi::Function::New(env, SetExecutedBenchmark));
instrumentHooksObj.Set(Napi::String::New(env, "setIntegration"),
Napi::Function::New(env, SetIntegration));
instrumentHooksObj.Set(Napi::String::New(env, "setEnvironment"),
Napi::Function::New(env, SetEnvironment));
instrumentHooksObj.Set(Napi::String::New(env, "writeEnvironment"),
Napi::Function::New(env, WriteEnvironment));
instrumentHooksObj.Set(Napi::String::New(env, "__codspeed_root_frame__"),
Napi::Function::New(env, __codspeed_root_frame__));

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/native_core/instruments/hooks_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Napi::Number StartBenchmark(const Napi::CallbackInfo &info);
Napi::Number StopBenchmark(const Napi::CallbackInfo &info);
Napi::Number SetExecutedBenchmark(const Napi::CallbackInfo &info);
Napi::Number SetIntegration(const Napi::CallbackInfo &info);
Napi::Number SetEnvironment(const Napi::CallbackInfo &info);
Napi::Number WriteEnvironment(const Napi::CallbackInfo &info);
Napi::Object Initialize(Napi::Env env, Napi::Object exports);

} // namespace hooks_wrapper
Expand Down
Loading