Skip to content

Commit 5cd317b

Browse files
committed
perf_hooks: performance milestone time origin timestamp improvement
1 parent bf39716 commit 5cd317b

File tree

6 files changed

+77
-27
lines changed

6 files changed

+77
-27
lines changed

benchmark/perf_hooks/time-origin.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('../common.js');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e6],
8+
method: ['timeOrigin', 'toJSON'],
9+
});
10+
11+
function main({ method, n }) {
12+
switch (method) {
13+
case 'timeOrigin':
14+
benchTimeOrigin(n);
15+
break;
16+
case 'toJSON':
17+
benchToJSON(n);
18+
break;
19+
default:
20+
throw new Error(`Unsupported method ${method}`);
21+
}
22+
}
23+
24+
function benchTimeOrigin(n) {
25+
const arr = [];
26+
for (let i = 0; i < n; ++i) {
27+
arr.push(performance.timeOrigin);
28+
}
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
arr[i] = performance.timeOrigin;
33+
}
34+
bench.end(n);
35+
36+
assert.strictEqual(arr.length, n);
37+
}
38+
39+
function benchToJSON(n) {
40+
bench.start();
41+
for (let i = 0; i < n; i++) {
42+
performance.toJSON();
43+
}
44+
bench.end(n);
45+
}

lib/internal/perf/performance.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const {
2222
defineEventHandler,
2323
} = require('internal/event_target');
2424

25-
const { now } = require('internal/perf/utils');
25+
const { now, getTimeOriginTimestamp } = require('internal/perf/utils');
2626

2727
const { markResourceTiming } = require('internal/perf/resource_timing');
2828

@@ -46,10 +46,6 @@ const { inspect } = require('util');
4646
const { validateInternalField } = require('internal/validators');
4747
const { convertToInt } = require('internal/webidl');
4848

49-
const {
50-
getTimeOriginTimestamp,
51-
} = internalBinding('performance');
52-
5349
const kPerformanceBrand = Symbol('performance');
5450

5551
class Performance extends EventTarget {

lib/internal/perf/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
constants: {
55
NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN,
6+
NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN_TIMESTAMP,
67
},
78
milestones,
89
now,
@@ -22,7 +23,12 @@ function getMilestoneTimestamp(milestoneIdx) {
2223
return ns / 1e6 - getTimeOrigin();
2324
}
2425

26+
function getTimeOriginTimestamp() {
27+
return milestones[NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN_TIMESTAMP] / 1e3;
28+
}
29+
2530
module.exports = {
2631
now,
2732
getMilestoneTimestamp,
33+
getTimeOriginTimestamp,
2834
};

src/env.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,10 @@ Environment::Environment(IsolateData* isolate_data,
871871
destroy_async_id_list_.reserve(512);
872872

873873
performance_state_ = std::make_unique<performance::PerformanceState>(
874-
isolate, time_origin_, MAYBE_FIELD_PTR(env_info, performance_state));
874+
isolate,
875+
time_origin_,
876+
time_origin_timestamp_,
877+
MAYBE_FIELD_PTR(env_info, performance_state));
875878

876879
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
877880
TRACING_CATEGORY_NODE1(environment)) != 0) {
@@ -1828,7 +1831,7 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
18281831
immediate_info_.Deserialize(ctx);
18291832
timeout_info_.Deserialize(ctx);
18301833
tick_info_.Deserialize(ctx);
1831-
performance_state_->Deserialize(ctx, time_origin_);
1834+
performance_state_->Deserialize(ctx, time_origin_, time_origin_timestamp_);
18321835
exit_info_.Deserialize(ctx);
18331836
stream_base_state_.Deserialize(ctx);
18341837
should_abort_on_uncaught_toggle_.Deserialize(ctx);

src/node_perf.cc

+14-18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ using v8::Integer;
2424
using v8::Isolate;
2525
using v8::Local;
2626
using v8::MaybeLocal;
27-
using v8::Number;
2827
using v8::Object;
2928
using v8::ObjectTemplate;
3029
using v8::PropertyAttribute;
@@ -43,6 +42,7 @@ uint64_t performance_v8_start;
4342

4443
PerformanceState::PerformanceState(Isolate* isolate,
4544
uint64_t time_origin,
45+
double time_origin_timestamp,
4646
const PerformanceState::SerializeInfo* info)
4747
: root(isolate,
4848
sizeof(performance_state_internal),
@@ -63,7 +63,7 @@ PerformanceState::PerformanceState(Isolate* isolate,
6363
// For deserialized performance states, we will do the
6464
// initialization in the deserialize callback.
6565
ResetMilestones();
66-
Initialize(time_origin);
66+
Initialize(time_origin, time_origin_timestamp);
6767
}
6868
}
6969

@@ -86,23 +86,27 @@ PerformanceState::SerializeInfo PerformanceState::Serialize(
8686
return info;
8787
}
8888

89-
void PerformanceState::Initialize(uint64_t time_origin) {
90-
// We are only reusing the milestone array to store the time origin, so do
91-
// not use the Mark() method. The time origin milestone is not exposed
92-
// to user land.
89+
void PerformanceState::Initialize(uint64_t time_origin,
90+
double time_origin_timestamp) {
91+
// We are only reusing the milestone array to store the time origin
92+
// and time origin timestamp, so do not use the Mark() method.
93+
// The time origin milestone is not exposed to user land.
9394
this->milestones[NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN] =
9495
static_cast<double>(time_origin);
96+
this->milestones[NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN_TIMESTAMP] =
97+
time_origin_timestamp;
9598
}
9699

97100
void PerformanceState::Deserialize(v8::Local<v8::Context> context,
98-
uint64_t time_origin) {
101+
uint64_t time_origin,
102+
double time_origin_timestamp) {
99103
// Resets the pointers.
100104
root.Deserialize(context);
101105
milestones.Deserialize(context);
102106
observers.Deserialize(context);
103107

104-
// Re-initialize the time origin i.e. the process start time.
105-
Initialize(time_origin);
108+
// Re-initialize the time origin and timestamp i.e. the process start time.
109+
Initialize(time_origin, time_origin_timestamp);
106110
}
107111

108112
std::ostream& operator<<(std::ostream& o,
@@ -254,7 +258,7 @@ void Notify(const FunctionCallbackInfo<Value>& args) {
254258
void LoopIdleTime(const FunctionCallbackInfo<Value>& args) {
255259
Environment* env = Environment::GetCurrent(args);
256260
uint64_t idle_time = uv_metrics_idle_time(env->event_loop());
257-
args.GetReturnValue().Set(1.0 * idle_time / 1e6);
261+
args.GetReturnValue().Set(1.0 * idle_time / NANOS_PER_MILLIS);
258262
}
259263

260264
void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
@@ -278,12 +282,6 @@ void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
278282
args.GetReturnValue().Set(histogram->object());
279283
}
280284

281-
void GetTimeOriginTimeStamp(const FunctionCallbackInfo<Value>& args) {
282-
Environment* env = Environment::GetCurrent(args);
283-
args.GetReturnValue().Set(Number::New(
284-
args.GetIsolate(), env->time_origin_timestamp() / MICROS_PER_MILLIS));
285-
}
286-
287285
void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args) {
288286
Realm* realm = Realm::GetCurrent(args);
289287
CHECK_EQ(realm->kind(), Realm::Kind::kPrincipal);
@@ -324,7 +322,6 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
324322
RemoveGarbageCollectionTracking);
325323
SetMethod(isolate, target, "notify", Notify);
326324
SetMethod(isolate, target, "loopIdleTime", LoopIdleTime);
327-
SetMethod(isolate, target, "getTimeOriginTimestamp", GetTimeOriginTimeStamp);
328325
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
329326
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
330327
SetFastMethodNoSideEffect(
@@ -391,7 +388,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
391388
registry->Register(RemoveGarbageCollectionTracking);
392389
registry->Register(Notify);
393390
registry->Register(LoopIdleTime);
394-
registry->Register(GetTimeOriginTimeStamp);
395391
registry->Register(CreateELDHistogram);
396392
registry->Register(MarkBootstrapComplete);
397393
registry->Register(SlowPerformanceNow);

src/node_perf_common.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern const double performance_process_start_timestamp;
2525
extern uint64_t performance_v8_start;
2626

2727
#define NODE_PERFORMANCE_MILESTONES(V) \
28+
V(TIME_ORIGIN_TIMESTAMP, "timeOriginTimestamp") \
2829
V(TIME_ORIGIN, "timeOrigin") \
2930
V(ENVIRONMENT, "environment") \
3031
V(NODE_START, "nodeStart") \
@@ -64,10 +65,13 @@ class PerformanceState {
6465

6566
explicit PerformanceState(v8::Isolate* isolate,
6667
uint64_t time_origin,
68+
double time_origin_timestamp,
6769
const SerializeInfo* info);
6870
SerializeInfo Serialize(v8::Local<v8::Context> context,
6971
v8::SnapshotCreator* creator);
70-
void Deserialize(v8::Local<v8::Context> context, uint64_t time_origin);
72+
void Deserialize(v8::Local<v8::Context> context,
73+
uint64_t time_origin,
74+
double time_origin_timestamp);
7175
friend std::ostream& operator<<(std::ostream& o, const SerializeInfo& i);
7276

7377
AliasedUint8Array root;
@@ -81,7 +85,7 @@ class PerformanceState {
8185
uint64_t ts = PERFORMANCE_NOW());
8286

8387
private:
84-
void Initialize(uint64_t time_origin);
88+
void Initialize(uint64_t time_origin, double time_origin_timestamp);
8589
void ResetMilestones();
8690
struct performance_state_internal {
8791
// doubles first so that they are always sizeof(double)-aligned

0 commit comments

Comments
 (0)