Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions test-app/app/src/main/assets/app/tests/benchmarks/jniBenchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function median(values) {
return sorted[Math.floor(sorted.length / 2)];
}

function report(name, nsPerOp, refNsPerOp) {
// ops, when given, is every operation the row executed in this launch (warm-up, reference
// calls and timed rounds): tools/perf/stat.sh divides counter deltas between two scales by it.
function report(name, nsPerOp, refNsPerOp, ops) {
var result = {
name: name,
median: Math.round(median(nsPerOp) * 10) / 10,
Expand All @@ -31,6 +33,9 @@ function report(name, nsPerOp, refNsPerOp) {
result.ref = Math.round(median(refNsPerOp) * 10) / 10;
result.refMin = Math.round(Math.min.apply(null, refNsPerOp) * 10) / 10;
}
if (ops) {
result.ops = ops;
}
console.log("JNIBENCH " + JSON.stringify(result));
return result;
}
Expand All @@ -39,7 +44,8 @@ function report(name, nsPerOp, refNsPerOp) {
function measureJs(name, iterations, body) {
if (skipped(name)) return null;
iterations *= SCALE;
var sink = body(Math.max(1000, iterations >> 2));
var warmup = Math.max(1000, iterations >> 2);
var sink = body(warmup);
var samples = [];
for (var r = 0; r < ROUNDS; r++) {
var start = performance.now();
Expand All @@ -48,7 +54,7 @@ function measureJs(name, iterations, body) {
samples.push((elapsed * 1e6) / iterations);
}
globalThis.__jniBenchSink = sink;
return report(name, samples);
return report(name, samples, null, warmup + ROUNDS * iterations);
}

// Callback rows drift with the device's performance state independently of the other rows,
Expand All @@ -65,16 +71,17 @@ function measureCallback(name, cb, kind, iterations) {
if (refCallback === null) {
refCallback = new JniBench.Callback({ twoInts: function (a, b) { } });
}
var warmup = Math.max(1000, iterations >> 2);
JniBench.runCallbacks(refCallback, REF_CALLBACK_KIND, refIterations >> 2);
JniBench.runCallbacks(cb, kind, Math.max(1000, iterations >> 2));
JniBench.runCallbacks(cb, kind, warmup);
var samples = [];
var ref = [];
for (var r = 0; r < ROUNDS; r++) {
ref.push(Number(JniBench.runCallbacks(refCallback, REF_CALLBACK_KIND, refIterations)) / refIterations);
var nanos = JniBench.runCallbacks(cb, kind, iterations);
samples.push(Number(nanos) / iterations);
}
return report(name, samples, ref);
return report(name, samples, ref, (refIterations >> 2) + warmup + ROUNDS * (refIterations + iterations));
}


Expand Down
9 changes: 9 additions & 0 deletions test-app/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ if (CCACHE_FOUND AND (USE_CCACHE))
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif (CCACHE_FOUND AND (USE_CCACHE))

# Benchmark builds only (tools/perf): BenchVariant.h reads debug.ns.benchvariant
# at load time so one APK can carry every label of an A/B. Never enable it for a
# build that ships.
option(NS_BENCH_VARIANTS "Read the run-time benchmark variant from debug.ns.benchvariant" OFF)
if (NS_BENCH_VARIANTS)
MESSAGE(STATUS "## NS_BENCH_VARIANTS is ON: benchmark build, do not ship")
add_definitions(-DNS_BENCH_VARIANTS)
endif ()

# Command info: https://cmake.org/cmake/help/v3.4/command/message.html
# we pass the android_ndk_root from gradle because for some reason
# "-DANDROID_STL=c++_static" is just not enough for clang++ to find some libraries in the ndk
Expand Down
13 changes: 12 additions & 1 deletion test-app/runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ if (onlyX86) {
println "OnlyX86 build triggered."
}

def useCCache = project.hasProperty("useCCache")
// -PuseCCache (CI and packaging), its -PuseCcache spelling, or NS_USE_CCACHE=1 in the environment.
def useCCache = project.hasProperty("useCCache") || project.hasProperty("useCcache") ||
System.getenv("NS_USE_CCACHE") == "1"
if (useCCache) {
println "Use CCache build triggered."
}

def benchVariants = project.hasProperty("benchVariants")
if (benchVariants) {
println "Benchmark variant switch (NS_BENCH_VARIANTS) enabled: do not ship this build."
}


def defaultNdkVersion = "29.0.14206865"

Expand Down Expand Up @@ -120,6 +127,10 @@ android {
arguments.add("-DUSE_CCACHE=true")
}

if (benchVariants) {
arguments.add("-DNS_BENCH_VARIANTS=ON")
}

cppFlags "-std=c++20"
arguments "-DANDROID_STL=c++_static", "-DANDROID_NDK_ROOT=${NDK_PATH}", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
}
Expand Down
43 changes: 43 additions & 0 deletions test-app/runtime/src/main/cpp/BenchVariant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef BENCHVARIANT_H_
#define BENCHVARIANT_H_

// Run-time variant switch for benchmark builds (tools/perf/README.md).
//
// Configured with -PbenchVariants (CMake NS_BENCH_VARIANTS=ON), the runtime
// reads the system property debug.ns.benchvariant once, when the library
// loads, so one APK can carry every label of an A/B as
// `if (BenchVariant() == N)` branches. Without the option BenchVariant() is a
// constant 0: the branches fold away and the property is never read.

#ifdef NS_BENCH_VARIANTS
#include <sys/system_properties.h>

#include <cstdlib>
#endif

namespace tns {

#ifdef NS_BENCH_VARIANTS

inline int g_benchVariant = 0;

inline int BenchVariant() { return g_benchVariant; }

// Called once from JNI_OnLoad, before any runtime exists.
inline void InitBenchVariant() {
char value[PROP_VALUE_MAX] = {0};
__system_property_get("debug.ns.benchvariant", value);
g_benchVariant = static_cast<int>(std::strtol(value, nullptr, 10));
}

#else

constexpr int BenchVariant() { return 0; }

inline void InitBenchVariant() {}

#endif

} // namespace tns

#endif // BENCHVARIANT_H_
13 changes: 13 additions & 0 deletions test-app/runtime/src/main/cpp/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <thread>

#include "ArgConverter.h"
#include "BenchVariant.h"
#include "BuiltinLoader.h"
#include "CallbackHandlers.h"
#include "Constants.h"
Expand Down Expand Up @@ -99,6 +100,13 @@ void Runtime::Init(JavaVM* vm, void* reserved) {
NATIVE_SCRIPT_RUNTIME_VERSION,
NATIVE_SCRIPT_RUNTIME_COMMIT_SHA);

#ifdef NS_BENCH_VARIANTS
InitBenchVariant();
// tools/perf checks this line to reject a launch that ran the wrong variant.
__android_log_print(ANDROID_LOG_INFO, "TNS.Runtime", "NS_BENCH_VARIANT=%d",
BenchVariant());
#endif

if (Runtime::s_jvm == nullptr) {
s_jvm = vm;

Expand Down Expand Up @@ -1044,6 +1052,11 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath,
globalTemplate->Set(
ArgConverter::ConvertToV8String(isolate, "__time"),
FunctionTemplate::New(isolate, CallbackHandlers::TimeCallback));
#ifdef NS_BENCH_VARIANTS
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__benchVariant"),
v8::Integer::New(isolate, BenchVariant()),
readOnlyFlags);
#endif

// queueMicrotask(callback) per spec:
// https://developer.mozilla.org/en-US/docs/Web/API/Window/queueMicrotask
Expand Down
Loading