Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src,lib: optimize nodeTiming.uvMetricsInfo #55614

Closed
wants to merge 4 commits into from
Closed
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
29 changes: 29 additions & 0 deletions benchmark/perf_hooks/nodetiming-uvmetricsinfo.js
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 8 additions & 1 deletion lib/internal/perf/nodetiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
eventsWaiting: metrics[2],
};
},
},
});
}
Expand Down
26 changes: 9 additions & 17 deletions src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace node {
namespace performance {

using v8::Array;
using v8::Context;
using v8::DontDelete;
using v8::Function;
Expand Down Expand Up @@ -264,26 +265,17 @@ void LoopIdleTime(const FunctionCallbackInfo<Value>& args) {

void UvMetricsInfo(const FunctionCallbackInfo<Value>& 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<Object> 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<Value> data[] = {
Integer::New(isolate, metrics.loop_count),
Integer::New(isolate, metrics.events),
Integer::New(isolate, metrics.events_waiting),
};
Local<Array> arr = Array::New(env->isolate(), data, arraysize(data));
args.GetReturnValue().Set(arr);
RafaelGSS marked this conversation as resolved.
Show resolved Hide resolved
}

void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
Expand Down
Loading