src: add COUNT_GENERIC_USAGE utility for tests · nodejs/node@b8fd0df · GitHub
Skip to content

Commit b8fd0df

Browse files
joyeecheungaduh95
authored andcommitted
src: add COUNT_GENERIC_USAGE utility for tests
PR-URL: #60434 Fixes: #60423 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Richard Lau <richard.lau@ibm.com>
1 parent 365aed4 commit b8fd0df

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/api/environment.cc

Lines changed: 7 additions & 2 deletions

src/node_debug.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "v8-fast-api-calls.h"
99
#include "v8.h"
1010

11+
#include <string>
1112
#include <string_view>
1213
#include <unordered_map>
1314
#endif // DEBUG
@@ -23,9 +24,20 @@ using v8::Number;
2324
using v8::Object;
2425
using v8::Value;
2526

27+
thread_local std::unordered_map<std::string, int> generic_usage_counters;
2628
thread_local std::unordered_map<FastStringKey, int, FastStringKey::Hash>
2729
v8_fast_api_call_counts;
2830

31+
void CountGenericUsage(const char* counter_name) {
32+
if (generic_usage_counters.find(counter_name) == generic_usage_counters.end())
33+
generic_usage_counters[counter_name] = 0;
34+
generic_usage_counters[counter_name]++;
35+
}
36+
37+
int GetGenericUsageCount(const char* counter_name) {
38+
return generic_usage_counters[counter_name];
39+
}
40+
2941
void TrackV8FastApiCall(FastStringKey key) {
3042
v8_fast_api_call_counts[key]++;
3143
}
@@ -34,6 +46,17 @@ int GetV8FastApiCallCount(FastStringKey key) {
3446
return v8_fast_api_call_counts[key];
3547
}
3648

49+
void GetGenericUsageCount(const FunctionCallbackInfo<Value>& args) {
50+
Environment* env = Environment::GetCurrent(args);
51+
if (!args[0]->IsString()) {
52+
env->ThrowError("getGenericUsageCount must be called with a string");
53+
return;
54+
}
55+
Utf8Value utf8_key(env->isolate(), args[0]);
56+
args.GetReturnValue().Set(
57+
GetGenericUsageCount(utf8_key.ToStringView().data()));
58+
}
59+
3760
void GetV8FastApiCallCount(const FunctionCallbackInfo<Value>& args) {
3861
Environment* env = Environment::GetCurrent(args);
3962
if (!args[0]->IsString()) {
@@ -89,6 +112,7 @@ void Initialize(Local<Object> target,
89112
Local<Context> context,
90113
void* priv) {
91114
SetMethod(context, target, "getV8FastApiCallCount", GetV8FastApiCallCount);
115+
SetMethod(context, target, "getGenericUsageCount", GetGenericUsageCount);
92116
SetFastMethod(context, target, "isEven", SlowIsEven, &fast_is_even);
93117
SetFastMethod(context, target, "isOdd", SlowIsOdd, &fast_is_odd);
94118
}

src/node_debug.h

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)