Skip to content

Commit 7c01712

Browse files
feat: dump node process information when running
1 parent 4f97cbe commit 7c01712

File tree

6 files changed

+91
-1
lines changed

6 files changed

+91
-1
lines changed

packages/core/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ export const setupCore = () => {
5858
native_core.InstrumentHooks.setIntegration("codspeed-node", __VERSION__);
5959
linuxPerf.start();
6060
checkV8Flags();
61+
62+
// Collect Node.js runtime environment to detect changes that could
63+
// cause performance differences across runs
64+
const hooks = native_core.InstrumentHooks;
65+
hooks.setEnvironment("Node.js", "version", process.versions.node);
66+
hooks.setEnvironment("Node.js", "v8", process.versions.v8);
67+
hooks.setEnvironment("Node.js", "arch", process.arch);
68+
hooks.writeEnvironment(process.pid);
6169
};
6270

6371
export const teardownCore = () => {

packages/core/src/native_core/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ try {
5151
setIntegration: (_name: string, _version: string) => {
5252
return 0;
5353
},
54+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
55+
setEnvironment: (
56+
_sectionName: string,
57+
_key: string,
58+
_value: string
59+
) => {
60+
return 0;
61+
},
62+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
63+
writeEnvironment: (_pid: number) => {
64+
return 0;
65+
},
5466
__codspeed_root_frame__: <T>(callback: () => T): T => {
5567
return callback();
5668
},

packages/core/src/native_core/instruments/hooks.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ export interface InstrumentHooks {
3232
*/
3333
setIntegration(name: string, version: string): number;
3434

35+
/**
36+
* Register a key-value pair under a named environment section.
37+
* @param sectionName Section name (e.g. "Node.js")
38+
* @param key Key name (e.g. "version")
39+
* @param value Value (e.g. "22.0.0")
40+
* @returns 0 on success, non-zero on error
41+
*/
42+
setEnvironment(sectionName: string, key: string, value: string): number;
43+
44+
/**
45+
* Flush all registered environment sections to disk.
46+
* @param pid Process ID
47+
* @returns 0 on success, non-zero on error
48+
*/
49+
writeEnvironment(pid: number): number;
50+
3551
/**
3652
* Execute a callback function with __codspeed_root_frame__ in its stack trace
3753
* @param callback Function to execute

packages/core/src/native_core/instruments/hooks_wrapper.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,54 @@ Napi::Number SetIntegration(const Napi::CallbackInfo &info) {
8181
return Napi::Number::New(env, result);
8282
}
8383

84+
Napi::Number SetEnvironment(const Napi::CallbackInfo &info) {
85+
Napi::Env env = info.Env();
86+
87+
if (info.Length() != 3) {
88+
Napi::TypeError::New(
89+
env, "Expected 3 arguments: sectionName, key, and value")
90+
.ThrowAsJavaScriptException();
91+
return Napi::Number::New(env, 1);
92+
}
93+
94+
if (!info[0].IsString() || !info[1].IsString() || !info[2].IsString()) {
95+
Napi::TypeError::New(
96+
env,
97+
"Expected string (sectionName), string (key), and string (value)")
98+
.ThrowAsJavaScriptException();
99+
return Napi::Number::New(env, 1);
100+
}
101+
102+
std::string section_name = info[0].As<Napi::String>().Utf8Value();
103+
std::string key = info[1].As<Napi::String>().Utf8Value();
104+
std::string value = info[2].As<Napi::String>().Utf8Value();
105+
106+
uint8_t result = instrument_hooks_set_environment(
107+
hooks, section_name.c_str(), key.c_str(), value.c_str());
108+
return Napi::Number::New(env, result);
109+
}
110+
111+
Napi::Number WriteEnvironment(const Napi::CallbackInfo &info) {
112+
Napi::Env env = info.Env();
113+
114+
if (info.Length() != 1) {
115+
Napi::TypeError::New(env, "Expected 1 argument: pid")
116+
.ThrowAsJavaScriptException();
117+
return Napi::Number::New(env, 1);
118+
}
119+
120+
if (!info[0].IsNumber()) {
121+
Napi::TypeError::New(env, "Expected number (pid)")
122+
.ThrowAsJavaScriptException();
123+
return Napi::Number::New(env, 1);
124+
}
125+
126+
uint32_t pid = info[0].As<Napi::Number>().Uint32Value();
127+
128+
uint8_t result = instrument_hooks_write_environment(hooks, pid);
129+
return Napi::Number::New(env, result);
130+
}
131+
84132
Napi::Value __attribute__ ((noinline)) __codspeed_root_frame__(const Napi::CallbackInfo &info) {
85133
Napi::Env env = info.Env();
86134

@@ -117,6 +165,10 @@ Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
117165
Napi::Function::New(env, SetExecutedBenchmark));
118166
instrumentHooksObj.Set(Napi::String::New(env, "setIntegration"),
119167
Napi::Function::New(env, SetIntegration));
168+
instrumentHooksObj.Set(Napi::String::New(env, "setEnvironment"),
169+
Napi::Function::New(env, SetEnvironment));
170+
instrumentHooksObj.Set(Napi::String::New(env, "writeEnvironment"),
171+
Napi::Function::New(env, WriteEnvironment));
120172
instrumentHooksObj.Set(Napi::String::New(env, "__codspeed_root_frame__"),
121173
Napi::Function::New(env, __codspeed_root_frame__));
122174

packages/core/src/native_core/instruments/hooks_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Napi::Number StartBenchmark(const Napi::CallbackInfo &info);
1717
Napi::Number StopBenchmark(const Napi::CallbackInfo &info);
1818
Napi::Number SetExecutedBenchmark(const Napi::CallbackInfo &info);
1919
Napi::Number SetIntegration(const Napi::CallbackInfo &info);
20+
Napi::Number SetEnvironment(const Napi::CallbackInfo &info);
21+
Napi::Number WriteEnvironment(const Napi::CallbackInfo &info);
2022
Napi::Object Initialize(Napi::Env env, Napi::Object exports);
2123

2224
} // namespace hooks_wrapper

0 commit comments

Comments
 (0)