Skip to content

Commit aebe2fc

Browse files
joyeecheungRafaelGSS
authored andcommitted
perf_hooks: implement performance.now() with fast API calls
PR-URL: #50492 Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Bryan English <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 4b26f14 commit aebe2fc

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

‎benchmark/perf_hooks/now.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
constassert=require('assert');
4+
constcommon=require('../common.js');
5+
6+
constbench=common.createBenchmark(main,{
7+
n: [1e6],
8+
});
9+
10+
functionmain({ n }){
11+
constarr=[];
12+
for(leti=0;i<n;++i){
13+
arr.push(performance.now());
14+
}
15+
16+
bench.start();
17+
for(leti=0;i<n;i++){
18+
arr[i]=performance.now();
19+
}
20+
bench.end(n);
21+
22+
assert.strictEqual(arr.length,n);
23+
}

‎lib/internal/perf/utils.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const{
55
NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN,
66
},
77
milestones,
8+
now,
89
}=internalBinding('performance');
910

1011
functiongetTimeOrigin(){
@@ -13,12 +14,6 @@ function getTimeOrigin(){
1314
returnmilestones[NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN]/1e6;
1415
}
1516

16-
// Returns the time relative to the process start time in milliseconds.
17-
functionnow(){
18-
consthr=process.hrtime();
19-
return(hr[0]*1000+hr[1]/1e6)-getTimeOrigin();
20-
}
21-
2217
// Returns the milestone relative to the process start time in milliseconds.
2318
functiongetMilestoneTimestamp(milestoneIdx){
2419
constns=milestones[milestoneIdx];

‎src/node_external_reference.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ using CFunctionCallbackWithOneByteString =
1515
using CFunctionCallback = void (*)(v8::Local<v8::Value> receiver);
1616
using CFunctionCallbackReturnDouble =
1717
double (*)(v8::Local<v8::Object> receiver);
18+
using CFunctionCallbackValueReturnDouble =
19+
double (*)(v8::Local<v8::Value> receiver);
1820
using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> receiver,
1921
int64_t);
2022
using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> receiver,
@@ -38,6 +40,7 @@ class ExternalReferenceRegistry{
3840
V(CFunctionCallback) \
3941
V(CFunctionCallbackWithOneByteString) \
4042
V(CFunctionCallbackReturnDouble) \
43+
V(CFunctionCallbackValueReturnDouble) \
4144
V(CFunctionCallbackWithInt64) \
4245
V(CFunctionCallbackWithBool) \
4346
V(CFunctionCallbackWithString) \

‎src/node_perf.cc‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args){
291291
performance::NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
292292
}
293293

294+
staticdoublePerformanceNowImpl(){
295+
returnstatic_cast<double>(uv_hrtime() - performance_process_start) /
296+
NANOS_PER_MILLIS;
297+
}
298+
299+
staticdoubleFastPerformanceNow(v8::Local<v8::Value> receiver){
300+
returnPerformanceNowImpl();
301+
}
302+
303+
staticvoidSlowPerformanceNow(const FunctionCallbackInfo<Value>& args){
304+
args.GetReturnValue().Set(PerformanceNowImpl());
305+
}
306+
307+
static v8::CFunction fast_performance_now(
308+
v8::CFunction::Make(FastPerformanceNow));
309+
294310
staticvoidCreatePerIsolateProperties(IsolateData* isolate_data,
295311
Local<ObjectTemplate> target){
296312
Isolate* isolate = isolate_data->isolate();
@@ -311,6 +327,8 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
311327
SetMethod(isolate, target, "getTimeOriginTimestamp", GetTimeOriginTimeStamp);
312328
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
313329
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
330+
SetFastMethodNoSideEffect(
331+
isolate, target, "now", SlowPerformanceNow, &fast_performance_now);
314332
}
315333

316334
voidCreatePerContextProperties(Local<Object> target,
@@ -376,6 +394,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry){
376394
registry->Register(GetTimeOriginTimeStamp);
377395
registry->Register(CreateELDHistogram);
378396
registry->Register(MarkBootstrapComplete);
397+
registry->Register(SlowPerformanceNow);
398+
registry->Register(FastPerformanceNow);
399+
registry->Register(fast_performance_now.GetTypeInfo());
379400
HistogramBase::RegisterExternalReferences(registry);
380401
IntervalHistogram::RegisterExternalReferences(registry);
381402
}

0 commit comments

Comments
(0)