From 4ff52971cfa4dcda11d01593b5d30d51d51121cc Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Wed, 30 Oct 2024 15:57:46 -0300 Subject: [PATCH 1/4] src,lib: optimize nodeTiming.uvMetricsInfo --- lib/internal/perf/nodetiming.js | 9 ++++++++- src/node_perf.cc | 26 +++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/internal/perf/nodetiming.js b/lib/internal/perf/nodetiming.js index d19bc021263a7f..97cf270695baaa 100644 --- a/lib/internal/perf/nodetiming.js +++ b/lib/internal/perf/nodetiming.js @@ -128,7 +128,14 @@ class PerformanceNodeTiming { __proto__: null, enumerable: true, configurable: true, - get: uvMetricsInfo, + get: () => { + const metrics = uvMetricsInfo(); + return { + loopCount: metrics[0], + events: metrics[1], + events_waiting: metrics[2], + }; + }, }, }); } diff --git a/src/node_perf.cc b/src/node_perf.cc index a868b0e82e0813..00a4d238b46b5c 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -14,6 +14,7 @@ namespace node { namespace performance { +using v8::Array; using v8::Context; using v8::DontDelete; using v8::Function; @@ -264,26 +265,17 @@ void LoopIdleTime(const FunctionCallbackInfo& args) { void UvMetricsInfo(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + Isolate* isolate = env->isolate(); uv_metrics_t metrics; - // uv_metrics_info always return 0 CHECK_EQ(uv_metrics_info(env->event_loop(), &metrics), 0); - - Local obj = Object::New(env->isolate()); - obj->Set(env->context(), - env->loop_count(), - Integer::NewFromUnsigned(env->isolate(), metrics.loop_count)) - .Check(); - obj->Set(env->context(), - env->events(), - Integer::NewFromUnsigned(env->isolate(), metrics.events)) - .Check(); - obj->Set(env->context(), - env->events_waiting(), - Integer::NewFromUnsigned(env->isolate(), metrics.events_waiting)) - .Check(); - - args.GetReturnValue().Set(obj); + Local data[] = { + Integer::New(isolate, metrics.loop_count), + Integer::New(isolate, metrics.events), + Integer::New(isolate, metrics.events_waiting), + }; + Local arr = Array::New(env->isolate(), data, arraysize(data)); + args.GetReturnValue().Set(arr); } void CreateELDHistogram(const FunctionCallbackInfo& args) { From 2dbc4ccd59218cba47d5de8c011741a5d17b4f51 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Wed, 30 Oct 2024 22:58:06 -0300 Subject: [PATCH 2/4] benchmark: add nodeTiming.uvmetricsinfo bench --- .../perf_hooks/nodetiming-uvmetricsinfo.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 benchmark/perf_hooks/nodetiming-uvmetricsinfo.js diff --git a/benchmark/perf_hooks/nodetiming-uvmetricsinfo.js b/benchmark/perf_hooks/nodetiming-uvmetricsinfo.js new file mode 100644 index 00000000000000..1d8d174de14fbc --- /dev/null +++ b/benchmark/perf_hooks/nodetiming-uvmetricsinfo.js @@ -0,0 +1,29 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('node:assert'); +const fs = require('node:fs/promises'); + +const { + performance, +} = require('perf_hooks'); + +const bench = common.createBenchmark(main, { + n: [1e6], + events: [1, 1000, 10000], +}); + +async function runEvents(events) { + for (let i = 0; i < events; ++i) { + assert.ok(await fs.statfs(__filename)); + } +} + +async function main({ n, events }) { + await runEvents(events); + bench.start(); + for (let i = 0; i < n; i++) { + assert.ok(performance.nodeTiming.uvMetricsInfo); + } + bench.end(n); +} From d3efcade880ef554a6b073a2bd46723b12f93133 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Wed, 30 Oct 2024 23:29:51 -0300 Subject: [PATCH 3/4] fixup! src,lib: optimize nodeTiming.uvMetricsInfo --- src/node_perf.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/node_perf.cc b/src/node_perf.cc index 00a4d238b46b5c..d8b26f5c845be1 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -270,9 +270,9 @@ void UvMetricsInfo(const FunctionCallbackInfo& args) { // uv_metrics_info always return 0 CHECK_EQ(uv_metrics_info(env->event_loop(), &metrics), 0); Local data[] = { - Integer::New(isolate, metrics.loop_count), - Integer::New(isolate, metrics.events), - Integer::New(isolate, metrics.events_waiting), + Integer::New(isolate, metrics.loop_count), + Integer::New(isolate, metrics.events), + Integer::New(isolate, metrics.events_waiting), }; Local arr = Array::New(env->isolate(), data, arraysize(data)); args.GetReturnValue().Set(arr); From 4df0ff2967131688d5762edf1f8ae9d50140c206 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Thu, 31 Oct 2024 01:15:02 -0300 Subject: [PATCH 4/4] fixup! fixup! src,lib: optimize nodeTiming.uvMetricsInfo --- lib/internal/perf/nodetiming.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/perf/nodetiming.js b/lib/internal/perf/nodetiming.js index 97cf270695baaa..a9e0c3f252ce5e 100644 --- a/lib/internal/perf/nodetiming.js +++ b/lib/internal/perf/nodetiming.js @@ -133,7 +133,7 @@ class PerformanceNodeTiming { return { loopCount: metrics[0], events: metrics[1], - events_waiting: metrics[2], + eventsWaiting: metrics[2], }; }, },