Skip to content

Commit 6d38abd

Browse files
Merge pull request #762 from NativeScript/high-res-time
Add high resolution, low overhead __time
2 parents 86fc854 + 5ca3492 commit 6d38abd

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

runtime/src/main/jni/CallbackHandlers.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sstream>
1414
#include <fstream>
1515
#include <cstdio>
16+
#include <chrono>
1617
#include "MethodCache.h"
1718
#include "SimpleProfiler.h"
1819
#include "Runtime.h"
@@ -631,6 +632,12 @@ void CallbackHandlers::LogMethodCallback(const v8::FunctionCallbackInfo<v8::Valu
631632
}
632633
}
633634

635+
void CallbackHandlers::TimeCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
636+
auto nano = std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now());
637+
double duration = nano.time_since_epoch().count() / 1000000.0;
638+
args.GetReturnValue().Set(duration);
639+
}
640+
634641
void CallbackHandlers::DumpReferenceTablesMethodCallback(
635642
const v8::FunctionCallbackInfo<v8::Value>& args) {
636643
DumpReferenceTablesMethod();

runtime/src/main/jni/CallbackHandlers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class CallbackHandlers {
5656

5757
static void LogMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
5858

59+
static void TimeCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
60+
5961
static void DumpReferenceTablesMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
6062

6163
static void DumpReferenceTablesMethod();

runtime/src/main/jni/Runtime.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
504504
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__disableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::DisableVerboseLoggingMethodCallback));
505505
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__exit"), FunctionTemplate::New(isolate, CallbackHandlers::ExitMethodCallback));
506506
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__runtimeVersion"), ArgConverter::ConvertToV8String(isolate, NATIVE_SCRIPT_RUNTIME_VERSION), readOnlyFlags);
507+
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__time"), FunctionTemplate::New(isolate, CallbackHandlers::TimeCallback));
507508

508509
/*
509510
* Attach `Worker` object constructor only to the main thread (isolate)'s global object

test-app/app/src/main/assets/app/mainpage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ require("./tests/field-access-test");
4545
require("./tests/byte-buffer-test");
4646
require("./tests/dex-interface-implementation");
4747
require("./tests/testInterfaceImplementation");
48+
require("./tests/testRuntimeImplementedAPIs");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
describe("Runtime exposes", function () {
2+
it("__time a low overhead, high resolution, time in ms.", function() {
3+
var dateTimeStart = Date.now();
4+
var timeStart = __time();
5+
var acc = 0;
6+
var s = android.os.SystemClock.elapsedRealtime();
7+
for (var i = 0; i < 1000; i++) {
8+
var c = android.os.SystemClock.elapsedRealtime();
9+
acc += (c - s);
10+
s = c;
11+
}
12+
var dateTimeEnd = Date.now();
13+
var timeEnd = __time();
14+
var dateDelta = dateTimeEnd - dateTimeStart;
15+
var timeDelta = timeEnd - timeStart;
16+
expect(Math.abs(dateDelta - timeDelta) < dateDelta * 0.25).toBe(true);
17+
});
18+
});

0 commit comments

Comments
 (0)