Skip to content

Commit dc40126

Browse files
--wip-- [skip ci]
1 parent 094b54b commit dc40126

File tree

3 files changed

+23
-37
lines changed

3 files changed

+23
-37
lines changed

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

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,33 @@ namespace codspeed_native {
66
namespace instruments {
77
namespace hooks_wrapper {
88

9-
// Global instance to maintain state across calls
10-
static std::unique_ptr<InstrumentHooks, decltype(&instrument_hooks_deinit)>
11-
g_hooks{nullptr, &instrument_hooks_deinit};
12-
13-
// Initialize instrument hooks if not already done
14-
static InstrumentHooks *ensureInitialized() {
15-
if (!g_hooks) {
16-
InstrumentHooks *hooks = instrument_hooks_init();
17-
if (hooks) {
18-
g_hooks.reset(hooks);
19-
}
9+
static InstrumentHooks *hooks = nullptr;
10+
11+
void InitializeGlobal() {
12+
printf("In InitializeGlobal\n");
13+
if (!hooks) {
14+
printf("Foo\n");
15+
hooks = instrument_hooks_init();
16+
printf("Bar\n");
2017
}
21-
return g_hooks.get();
2218
}
2319

2420
Napi::Boolean IsInstrumented(const Napi::CallbackInfo &info) {
2521
Napi::Env env = info.Env();
26-
InstrumentHooks *hooks = ensureInitialized();
27-
28-
if (!hooks) {
29-
return Napi::Boolean::New(env, false);
30-
}
3122

3223
bool instrumented = instrument_hooks_is_instrumented(hooks);
3324
return Napi::Boolean::New(env, instrumented);
3425
}
3526

3627
Napi::Number StartBenchmark(const Napi::CallbackInfo &info) {
3728
Napi::Env env = info.Env();
38-
InstrumentHooks *hooks = ensureInitialized();
39-
40-
if (!hooks) {
41-
return Napi::Number::New(env, 1); // Return error code
42-
}
4329

4430
uint8_t result = instrument_hooks_start_benchmark(hooks);
4531
return Napi::Number::New(env, result);
4632
}
4733

4834
Napi::Number StopBenchmark(const Napi::CallbackInfo &info) {
4935
Napi::Env env = info.Env();
50-
InstrumentHooks *hooks = ensureInitialized();
51-
52-
if (!hooks) {
53-
return Napi::Number::New(env, 1); // Return error code
54-
}
5536

5637
uint8_t result = instrument_hooks_stop_benchmark(hooks);
5738
return Napi::Number::New(env, result);
@@ -72,11 +53,6 @@ Napi::Number SetExecutedBenchmark(const Napi::CallbackInfo &info) {
7253
return Napi::Number::New(env, 1);
7354
}
7455

75-
InstrumentHooks *hooks = ensureInitialized();
76-
if (!hooks) {
77-
return Napi::Number::New(env, 1);
78-
}
79-
8056
uint32_t pid = info[0].As<Napi::Number>().Uint32Value();
8157
std::string uri = info[1].As<Napi::String>().Utf8Value();
8258

@@ -100,11 +76,6 @@ Napi::Number SetIntegration(const Napi::CallbackInfo &info) {
10076
return Napi::Number::New(env, 1);
10177
}
10278

103-
InstrumentHooks *hooks = ensureInitialized();
104-
if (!hooks) {
105-
return Napi::Number::New(env, 1);
106-
}
107-
10879
std::string name = info[0].As<Napi::String>().Utf8Value();
10980
std::string version = info[1].As<Napi::String>().Utf8Value();
11081

@@ -116,6 +87,10 @@ Napi::Number SetIntegration(const Napi::CallbackInfo &info) {
11687
Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
11788
Napi::Object instrumentHooksObj = Napi::Object::New(env);
11889

90+
printf("In instrument hooks init\n");
91+
InitializeGlobal();
92+
printf("1\n");
93+
11994
instrumentHooksObj.Set(Napi::String::New(env, "isInstrumented"),
12095
Napi::Function::New(env, IsInstrumented));
12196
instrumentHooksObj.Set(Napi::String::New(env, "startBenchmark"),
@@ -127,8 +102,11 @@ Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
127102
instrumentHooksObj.Set(Napi::String::New(env, "setIntegration"),
128103
Napi::Function::New(env, SetIntegration));
129104

105+
printf("2\n");
106+
130107
exports.Set(Napi::String::New(env, "InstrumentHooks"), instrumentHooksObj);
131108

109+
printf("3\n");
132110
return exports;
133111
}
134112

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace codspeed_native {
77
namespace instruments {
88
namespace hooks_wrapper {
99

10+
// Global initialization
11+
// WARNING: InitializeGlobal() must be called before using any other functions
12+
// All API functions assume the global instance is already initialized
13+
void InitializeGlobal();
14+
1015
Napi::Boolean IsInstrumented(const Napi::CallbackInfo &info);
1116
Napi::Number StartBenchmark(const Napi::CallbackInfo &info);
1217
Napi::Number StopBenchmark(const Napi::CallbackInfo &info);

packages/core/src/native_core/native_core.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace codspeed_native {
66

77
Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
8+
printf("Initializing native core module...\n");
89
codspeed_native::LinuxPerf::Initialize(env, exports);
910
codspeed_native::instruments::hooks_wrapper::Initialize(env, exports);
1011

12+
printf("Native core module init done.\n");
13+
1114
return exports;
1215
}
1316

0 commit comments

Comments
 (0)