From 3f712c462c6eeda2f82ceda36bdd2e11c7eade2f Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 17 Dec 2024 15:48:48 -0600 Subject: [PATCH] feat: use monotonic time for performance object --- NativeScript/runtime/Runtime.h | 2 ++ NativeScript/runtime/Runtime.mm | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/NativeScript/runtime/Runtime.h b/NativeScript/runtime/Runtime.h index a55080c4..5a7de0e1 100644 --- a/NativeScript/runtime/Runtime.h +++ b/NativeScript/runtime/Runtime.h @@ -77,6 +77,8 @@ class Runtime { std::unique_ptr moduleInternal_; int workerId_; CFRunLoopRef runtimeLoop_; + double startTime; + double realtimeOrigin; // TODO: refactor this. This is only needed because, during program // termination (UIApplicationMain not called) the Cache::Workers is released // (static initialization order fiasco diff --git a/NativeScript/runtime/Runtime.mm b/NativeScript/runtime/Runtime.mm index d34f9359..e9f1a0d1 100644 --- a/NativeScript/runtime/Runtime.mm +++ b/NativeScript/runtime/Runtime.mm @@ -154,6 +154,9 @@ void DisposeIsolateWhenPossible(Isolate* isolate) { v8Initialized_ = true; } + startTime = platform_->MonotonicallyIncreasingTime(); + realtimeOrigin = platform_->CurrentClockTimeMillis(); + // auto version = v8::V8::GetVersion(); Isolate::CreateParams create_params; @@ -389,16 +392,17 @@ void DisposeIsolateWhenPossible(Isolate* isolate) { Local nowFuncTemplate = FunctionTemplate::New(isolate, PerformanceNowCallback); performanceTemplate->Set(tns::ToV8String(isolate, "now"), nowFuncTemplate); + performanceTemplate->Set(tns::ToV8String(isolate, "timeOrigin"), + v8::Number::New(isolate, realtimeOrigin)); + Local performancePropertyName = ToV8String(isolate, "performance"); globalTemplate->Set(performancePropertyName, performanceTemplate); } void Runtime::PerformanceNowCallback(const FunctionCallbackInfo& args) { - std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); - std::chrono::milliseconds timestampMs = - std::chrono::duration_cast(now.time_since_epoch()); - double result = timestampMs.count(); - args.GetReturnValue().Set(result); + auto runtime = Runtime::GetRuntime(args.GetIsolate()); + args.GetReturnValue().Set( + (runtime->platform_->MonotonicallyIncreasingTime() - runtime->startTime) * 1000.0); } void Runtime::DefineNativeScriptVersion(Isolate* isolate, Local globalTemplate) {