Skip to content

Commit

Permalink
perf_hooks: implement GetTimeOriginTimeStamp with fast API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasShabiCS committed Feb 9, 2024
1 parent dd4767f commit 69e1fe6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
45 changes: 45 additions & 0 deletions benchmark/perf_hooks/time-origin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const assert = require('assert');
const common = require('../common.js');

const bench = common.createBenchmark(main, {
n: [1e6],
method: ['timeOrigin', 'toJSON'],
});

function main({ method, n }) {
switch (method) {
case 'timeOrigin':
benchTimeOrigin(n);
break;
case 'toJSON':
benchToJSON(n);
break;
default:
throw new Error(`Unsupported method ${method}`);
}
}

function benchTimeOrigin(n) {
const arr = [];
for (let i = 0; i < n; ++i) {
arr.push(performance.timeOrigin);
}

bench.start();
for (let i = 0; i < n; i++) {
arr[i] = performance.timeOrigin;
}
bench.end(n);

assert.strictEqual(arr.length, n);
}

function benchToJSON(n) {
bench.start();
for (let i = 0; i < n; i++) {
performance.toJSON();
}
bench.end(n);
}
36 changes: 26 additions & 10 deletions src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::PropertyAttribute;
Expand Down Expand Up @@ -254,7 +253,7 @@ void Notify(const FunctionCallbackInfo<Value>& args) {
void LoopIdleTime(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
uint64_t idle_time = uv_metrics_idle_time(env->event_loop());
args.GetReturnValue().Set(1.0 * idle_time / 1e6);
args.GetReturnValue().Set(1.0 * idle_time / NANOS_PER_MILLIS);
}

void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -278,12 +277,6 @@ void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(histogram->object());
}

void GetTimeOriginTimeStamp(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
args.GetReturnValue().Set(Number::New(
args.GetIsolate(), env->time_origin_timestamp() / MICROS_PER_MILLIS));
}

void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
CHECK_EQ(realm->kind(), Realm::Kind::kPrincipal);
Expand All @@ -307,6 +300,23 @@ static void SlowPerformanceNow(const FunctionCallbackInfo<Value>& args) {
static v8::CFunction fast_performance_now(
v8::CFunction::Make(FastPerformanceNow));

static double GetTimeOriginTimeStampImpl() {
return static_cast<double>(performance_process_start_timestamp) /
MICROS_PER_MILLIS;
}

static double FastGetTimeOriginTimeStamp(v8::Local<v8::Value> receiver) {
return GetTimeOriginTimeStampImpl();
}

static void SlowGetTimeOriginTimeStamp(
const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(GetTimeOriginTimeStampImpl());
}

static v8::CFunction fast_get_timeorigin_timestamp(
v8::CFunction::Make(FastGetTimeOriginTimeStamp));

static void CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
Expand All @@ -324,11 +334,15 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
RemoveGarbageCollectionTracking);
SetMethod(isolate, target, "notify", Notify);
SetMethod(isolate, target, "loopIdleTime", LoopIdleTime);
SetMethod(isolate, target, "getTimeOriginTimestamp", GetTimeOriginTimeStamp);
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
SetFastMethodNoSideEffect(
isolate, target, "now", SlowPerformanceNow, &fast_performance_now);
SetFastMethodNoSideEffect(isolate,
target,
"getTimeOriginTimestamp",
SlowGetTimeOriginTimeStamp,
&fast_get_timeorigin_timestamp);
}

void CreatePerContextProperties(Local<Object> target,
Expand Down Expand Up @@ -391,12 +405,14 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(RemoveGarbageCollectionTracking);
registry->Register(Notify);
registry->Register(LoopIdleTime);
registry->Register(GetTimeOriginTimeStamp);
registry->Register(CreateELDHistogram);
registry->Register(MarkBootstrapComplete);
registry->Register(SlowPerformanceNow);
registry->Register(FastPerformanceNow);
registry->Register(fast_performance_now.GetTypeInfo());
registry->Register(SlowGetTimeOriginTimeStamp);
registry->Register(FastGetTimeOriginTimeStamp);
registry->Register(fast_get_timeorigin_timestamp.GetTypeInfo());
HistogramBase::RegisterExternalReferences(registry);
IntervalHistogram::RegisterExternalReferences(registry);
}
Expand Down

0 comments on commit 69e1fe6

Please sign in to comment.