Skip to content

Commit

Permalink
feat: use monotonic time for performance object
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Dec 17, 2024
1 parent 863a790 commit 3f712c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NativeScript/runtime/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class Runtime {
std::unique_ptr<ModuleInternal> 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
Expand Down
14 changes: 9 additions & 5 deletions NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -389,16 +392,17 @@ void DisposeIsolateWhenPossible(Isolate* isolate) {
Local<FunctionTemplate> nowFuncTemplate = FunctionTemplate::New(isolate, PerformanceNowCallback);
performanceTemplate->Set(tns::ToV8String(isolate, "now"), nowFuncTemplate);

performanceTemplate->Set(tns::ToV8String(isolate, "timeOrigin"),
v8::Number::New(isolate, realtimeOrigin));

Local<v8::String> performancePropertyName = ToV8String(isolate, "performance");
globalTemplate->Set(performancePropertyName, performanceTemplate);
}

void Runtime::PerformanceNowCallback(const FunctionCallbackInfo<Value>& args) {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::milliseconds timestampMs =
std::chrono::duration_cast<std::chrono::milliseconds>(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<ObjectTemplate> globalTemplate) {
Expand Down

0 comments on commit 3f712c4

Please sign in to comment.