Skip to content

Commit 3db72b5

Browse files
committed
test(bench): time array element access and object-returning callbacks
Adds JNI benchmark rows for reading and writing Java array elements (int[] and Object[]) and for a Java -> JS callback that returns an object, which the existing rows did not exercise.
1 parent 719f538 commit 3db72b5

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎test-app/app/src/main/assets/app/tests/benchmarks/jniBenchmarks.js‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,15 @@ function buildBenchmarks() {
177177
twoInts: function (a, b) { },
178178
oneString: function (s) { },
179179
intReturn: function (a) { return a + 1; },
180-
oneObject: function (o) { }
180+
oneObject: function (o) { },
181+
objectReturn: function () { return other; }
181182
});
183+
var ints = Array.create("int", 16);
184+
var objects = Array.create(java.lang.Object, 16);
185+
for (var k = 0; k < 16; k++) {
186+
ints[k] = k;
187+
objects[k] = new java.lang.Object();
188+
}
182189

183190
return [
184191
function () {
@@ -407,6 +414,26 @@ function buildBenchmarks() {
407414
return s;
408415
});
409416
},
417+
function () {
418+
return measureJs("array get int[i]", 50000, function (n) {
419+
var s = 0;
420+
for (var i = 0; i < n; i++) s += ints[i & 15];
421+
return s;
422+
});
423+
},
424+
function () {
425+
return measureJs("array set int[i]", 50000, function (n) {
426+
for (var i = 0; i < n; i++) ints[i & 15] = i;
427+
return n;
428+
});
429+
},
430+
function () {
431+
return measureJs("array get Object[i]", 50000, function (n) {
432+
var s;
433+
for (var i = 0; i < n; i++) s = objects[i & 15];
434+
return s;
435+
});
436+
},
410437
function () {
411438
return measureLifecycle("lifecycle new java.lang.Object()", 20000, function () {
412439
return new java.lang.Object();
@@ -470,6 +497,9 @@ function buildBenchmarks() {
470497
},
471498
function () {
472499
return measureCallback("callback.oneObject(binding)", callback, 7, 20000);
500+
},
501+
function () {
502+
return measureCallback("callback.objectReturn() -> Object", callback, 8, 20000);
473503
}
474504
];
475505
}

‎test-app/app/src/main/java/com/tns/tests/JniBench.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public interface Callback {
1919
int intReturn(int a);
2020

2121
void oneObject(Object o);
22+
23+
Object objectReturn();
2224
}
2325

2426
public int intField = 7;
@@ -251,6 +253,12 @@ public static long runCallbacks(Callback cb, int kind, int n) {
251253
// The callback is itself a binding: the argument carries its object id.
252254
for (int i = 0; i < n; i++) cb.oneObject(cb);
253255
break;
256+
case 8: {
257+
Object sink = null;
258+
for (int i = 0; i < n; i++) sink = cb.objectReturn();
259+
if (sink == null) throw new IllegalStateException("objectReturn returned null");
260+
break;
261+
}
254262
default:
255263
throw new IllegalArgumentException("kind " + kind);
256264
}

0 commit comments

Comments
 (0)