66// the median and minimum ns/op. Java -> JS benchmarks are timed in Java with System.nanoTime.
77
88var JniBench = com . tns . tests . JniBench ;
9- var ROUNDS = 7 ;
9+ var ROUNDS = com . tns . tests . JniBenchConfig . getRounds ( ) || 7 ;
1010var FILTER = String ( JniBench . getFilter ( ) ) ;
11+ var VARIANTS = String ( com . tns . tests . JniBenchConfig . getVariants ( ) ) . split ( "," ) . filter ( function ( v ) { return v !== "" ; } ) . map ( Number ) ;
12+ // Several names may be given, separated by "|"; a benchmark runs when its name contains any.
13+ var FILTERS = FILTER ? FILTER . split ( "|" ) : [ ] ;
14+ function skipped ( name ) {
15+ return FILTERS . length > 0 && ! FILTERS . some ( function ( f ) { return name . indexOf ( f ) >= 0 ; } ) ;
16+ }
1117var SCALE = JniBench . getScale ( ) ;
1218
1319function median ( values ) {
1420 var sorted = values . slice ( ) . sort ( function ( a , b ) { return a - b ; } ) ;
1521 return sorted [ Math . floor ( sorted . length / 2 ) ] ;
1622}
1723
18- function report ( name , nsPerOp ) {
24+ function report ( name , nsPerOp , refNsPerOp ) {
1925 var result = {
2026 name : name ,
2127 median : Math . round ( median ( nsPerOp ) * 10 ) / 10 ,
2228 min : Math . round ( Math . min . apply ( null , nsPerOp ) * 10 ) / 10
2329 } ;
30+ if ( refNsPerOp ) {
31+ result . ref = Math . round ( median ( refNsPerOp ) * 10 ) / 10 ;
32+ result . refMin = Math . round ( Math . min . apply ( null , refNsPerOp ) * 10 ) / 10 ;
33+ }
2434 console . log ( "JNIBENCH " + JSON . stringify ( result ) ) ;
2535 return result ;
2636}
2737
2838// body(n) runs the operation n times; returning a value keeps V8 from dropping the loop.
2939function measureJs ( name , iterations , body ) {
30- if ( FILTER && name . indexOf ( FILTER ) < 0 ) return null ;
40+ if ( skipped ( name ) ) return null ;
3141 iterations *= SCALE ;
3242 var sink = body ( Math . max ( 1000 , iterations >> 2 ) ) ;
3343 var samples = [ ] ;
@@ -41,24 +51,38 @@ function measureJs(name, iterations, body) {
4151 return report ( name , samples ) ;
4252}
4353
54+ // Callback rows drift with the device's performance state independently of the other rows,
55+ // so each round also times a fixed reference callback (twoInts on its own object). Compare
56+ // launches by median / ref rather than by median alone.
57+ var REF_CALLBACK_KIND = 1 ;
58+ var REF_CALLBACK_ITERATIONS = 20000 ;
59+ var refCallback = null ;
60+
4461function measureCallback ( name , cb , kind , iterations ) {
45- if ( FILTER && name . indexOf ( FILTER ) < 0 ) return null ;
62+ if ( skipped ( name ) ) return null ;
4663 iterations *= SCALE ;
64+ var refIterations = REF_CALLBACK_ITERATIONS * SCALE ;
65+ if ( refCallback === null ) {
66+ refCallback = new JniBench . Callback ( { twoInts : function ( a , b ) { } } ) ;
67+ }
68+ JniBench . runCallbacks ( refCallback , REF_CALLBACK_KIND , refIterations >> 2 ) ;
4769 JniBench . runCallbacks ( cb , kind , Math . max ( 1000 , iterations >> 2 ) ) ;
4870 var samples = [ ] ;
71+ var ref = [ ] ;
4972 for ( var r = 0 ; r < ROUNDS ; r ++ ) {
73+ ref . push ( Number ( JniBench . runCallbacks ( refCallback , REF_CALLBACK_KIND , refIterations ) ) / refIterations ) ;
5074 var nanos = JniBench . runCallbacks ( cb , kind , iterations ) ;
5175 samples . push ( Number ( nanos ) / iterations ) ;
5276 }
53- return report ( name , samples ) ;
77+ return report ( name , samples , ref ) ;
5478}
5579
5680
5781// Splits an object's lifecycle into allocation (objects kept alive), release through a
5882// V8 GC (wrapper finalization and the Java-side strong -> weak bookkeeping), and the
5983// Java GC that follows. Each phase is reported per object.
6084function measureLifecycle ( name , count , create ) {
61- if ( FILTER && name . indexOf ( FILTER ) < 0 ) return null ;
85+ if ( skipped ( name ) ) return null ;
6286 count *= SCALE ;
6387 var javaRuntime = java . lang . Runtime . getRuntime ( ) ;
6488 var alloc = [ ] , release = [ ] , javaGc = [ ] ;
@@ -88,6 +112,56 @@ function measureLifecycle(name, count, create) {
88112}
89113
90114
115+
116+ // Aggregate ns per call while `threads` Java threads call into JS at once. Runs
117+ // asynchronously: the JS thread must go idle and release the isolate for the
118+ // callers to get in, so it polls for the result.
119+ function measureContention ( name , sink , threads , calls , mode , done ) {
120+ if ( skipped ( name ) ) {
121+ done ( null ) ;
122+ return ;
123+ }
124+ var Bench = com . tns . tests . ContentionBench ;
125+ calls *= SCALE ;
126+ // Variants alternate round by round inside one launch, so a change in the device's
127+ // performance state between launches cannot favour one of them.
128+ var variants = VARIANTS . length > 0 ? VARIANTS : [ null ] ;
129+ var samples = variants . map ( function ( ) { return [ ] ; } ) ;
130+ var round = - variants . length ;
131+ var total = ROUNDS * variants . length ;
132+ function variantOf ( r ) {
133+ return ( ( r % variants . length ) + variants . length ) % variants . length ;
134+ }
135+ function startRound ( ) {
136+ var spin = variants [ variantOf ( round ) ] ;
137+ if ( spin !== null ) {
138+ com . tns . Runtime . setAdmissionSpinMicros ( spin ) ;
139+ }
140+ Bench . start ( sink , threads , calls , mode ) ;
141+ setTimeout ( poll , 5 ) ;
142+ }
143+ function poll ( ) {
144+ var nanos = Number ( Bench . result ( ) ) ;
145+ if ( nanos < 0 ) {
146+ setTimeout ( poll , 5 ) ;
147+ return ;
148+ }
149+ if ( round >= 0 ) {
150+ samples [ variantOf ( round ) ] . push ( nanos / ( threads * calls ) ) ;
151+ }
152+ round ++ ;
153+ if ( round < total ) {
154+ gc ( ) ;
155+ setTimeout ( startRound , 20 ) ;
156+ } else {
157+ done ( variants . map ( function ( spin , i ) {
158+ return report ( spin === null ? name : name + " [spin=" + spin + "]" , samples [ i ] ) ;
159+ } ) ) ;
160+ }
161+ }
162+ startRound ( ) ;
163+ }
164+
91165function buildBenchmarks ( ) {
92166 var bench = new JniBench ( ) ;
93167 var other = bench . getOther ( ) ;
@@ -148,6 +222,22 @@ function buildBenchmarks() {
148222 return n ;
149223 } ) ;
150224 } ,
225+ function ( ) {
226+ if ( skipped ( "instance.getAlpha() [android.view.View]" ) ) return null ;
227+ var view = new android . view . View ( com . tns . NativeScriptApplication . getInstance ( ) ) ;
228+ return measureJs ( "instance.getAlpha() [android.view.View]" , 50000 , function ( n ) {
229+ var s = 0 ;
230+ for ( var i = 0 ; i < n ; i ++ ) s = view . getAlpha ( ) ;
231+ return s ;
232+ } ) ;
233+ } ,
234+ function ( ) {
235+ return measureJs ( "package member read android.view.View" , 200000 , function ( n ) {
236+ var s = null ;
237+ for ( var i = 0 ; i < n ; i ++ ) s = android . view . View ;
238+ return s ;
239+ } ) ;
240+ } ,
151241 function ( ) {
152242 return measureJs ( "instance.getInt()" , 50000 , function ( n ) {
153243 var s = 0 ;
@@ -231,6 +321,13 @@ function buildBenchmarks() {
231321 return s ;
232322 } ) ;
233323 } ,
324+ function ( ) {
325+ return measureJs ( "static.newObjectOfManyClasses() [16 classes]" , 20000 , function ( n ) {
326+ var s ;
327+ for ( var i = 0 ; i < n ; i ++ ) s = JniBench . newObjectOfManyClasses ( ) ;
328+ return s ;
329+ } ) ;
330+ } ,
234331 function ( ) {
235332 return measureJs ( "static.newObjectInJava() [new wrapper]" , 20000 , function ( n ) {
236333 var s ;
@@ -252,6 +349,37 @@ function buildBenchmarks() {
252349 return s ;
253350 } ) ;
254351 } ,
352+ function ( ) {
353+ return measureJs ( "static field get int [small class]" , 50000 , function ( n ) {
354+ var Fields = com . tns . tests . StaticFieldBench ;
355+ var s = 0 ;
356+ for ( var i = 0 ; i < n ; i ++ ) s = Fields . counter ;
357+ return s ;
358+ } ) ;
359+ } ,
360+ function ( ) {
361+ return measureJs ( "static field get final int [small class]" , 50000 , function ( n ) {
362+ var Fields = com . tns . tests . StaticFieldBench ;
363+ var s = 0 ;
364+ for ( var i = 0 ; i < n ; i ++ ) s = Fields . CONSTANT ;
365+ return s ;
366+ } ) ;
367+ } ,
368+ function ( ) {
369+ return measureJs ( "static field set int [small class]" , 50000 , function ( n ) {
370+ var Fields = com . tns . tests . StaticFieldBench ;
371+ for ( var i = 0 ; i < n ; i ++ ) Fields . counter = i ;
372+ return n ;
373+ } ) ;
374+ } ,
375+ function ( ) {
376+ return measureJs ( "static field get final int [android.R.id]" , 50000 , function ( n ) {
377+ var ids = android . R . id ;
378+ var s = 0 ;
379+ for ( var i = 0 ; i < n ; i ++ ) s = ids . content ;
380+ return s ;
381+ } ) ;
382+ } ,
255383 function ( ) {
256384 return measureJs ( "field.get int" , 50000 , function ( n ) {
257385 var s = 0 ;
@@ -289,13 +417,24 @@ function buildBenchmarks() {
289417 return new Extended ( ) ;
290418 } ) ;
291419 } ,
420+ function ( ) {
421+ if ( skipped ( "static.newInstance(extended class) [Java-created]" ) ) return null ;
422+ var Created = java . lang . Object . extend ( { toString : function ( ) { return "created" ; } } ) ;
423+ var createdClass = new Created ( ) . getClass ( ) ;
424+ var JavaCreated = com . tns . tests . JavaCreated ;
425+ return measureJs ( "static.newInstance(extended class) [Java-created]" , 10000 , function ( n ) {
426+ var o ;
427+ for ( var i = 0 ; i < n ; i ++ ) o = JavaCreated . newInstance ( createdClass ) ;
428+ return o ;
429+ } ) ;
430+ } ,
292431 function ( ) {
293432 return measureLifecycle ( "lifecycle JniBench.newObjectInJava()" , 20000 , function ( ) {
294433 return JniBench . newObjectInJava ( ) ;
295434 } ) ;
296435 } ,
297436 function ( ) {
298- if ( FILTER && "instance.self() [50k live wrappers]" . indexOf ( FILTER ) < 0 ) return null ;
437+ if ( skipped ( "instance.self() [50k live wrappers]" ) ) return null ;
299438 var live = new Array ( 50000 ) ;
300439 for ( var i = 0 ; i < live . length ; i ++ ) live [ i ] = new java . lang . Object ( ) ;
301440 // Pinned from a global: an optimized frame may drop a local after its last use.
@@ -322,24 +461,105 @@ function buildBenchmarks() {
322461 } ,
323462 function ( ) {
324463 return measureCallback ( "callback.oneObject(Object)" , callback , 4 , 20000 ) ;
464+ } ,
465+ function ( ) {
466+ return measureCallback ( "callback.oneObject(new Object)" , callback , 5 , 20000 ) ;
467+ } ,
468+ function ( ) {
469+ return measureCallback ( "callback.oneObject(new, 16 classes)" , callback , 6 , 20000 ) ;
470+ } ,
471+ function ( ) {
472+ return measureCallback ( "callback.oneObject(binding)" , callback , 7 , 20000 ) ;
325473 }
326474 ] ;
327475}
328476
477+ function buildContentionBenchmarks ( ) {
478+ var Bench = com . tns . tests . ContentionBench ;
479+ var sink = new Bench . Sink ( {
480+ call0 : function ( ) { } ,
481+ call3 : function ( a , b , c ) { }
482+ } ) ;
483+ var list = [ ] ;
484+ [ 1 , 2 , 4 ] . forEach ( function ( threads ) {
485+ [ [ "no args" , Bench . NO_ARGS ] , [ "3 existing objects" , Bench . EXISTING_OBJECTS ] , [ "3 new objects" , Bench . NEW_OBJECTS ] , [ "3 new objects of 16 classes" , Bench . NEW_OBJECTS_MANY_CLASSES ] ] . forEach ( function ( mode ) {
486+ list . push ( function ( done ) {
487+ measureContention ( "contention " + threads + " thread(s), " + mode [ 0 ] , sink , threads , 20000 , mode [ 1 ] , done ) ;
488+ } ) ;
489+ } ) ;
490+ } ) ;
491+ return list ;
492+ }
493+
494+ // The first touch of a class builds its JS constructor and installs every static member, once
495+ // per runtime, so each row is a single sample and must run before anything else touches the class.
496+ function measureFirstAccess ( ) {
497+ var rows = [
498+ [ "first access android.R" , function ( ) { return android . R ; } ] ,
499+ [ "first access android.R.attr" , function ( ) { return android . R . attr ; } ] ,
500+ [ "first access android.R.id" , function ( ) { return android . R . id ; } ] ,
501+ [ "first access android.R.style" , function ( ) { return android . R . style ; } ] ,
502+ [ "first access android.R.drawable" , function ( ) { return android . R . drawable ; } ] ,
503+ [ "first access android.R.string" , function ( ) { return android . R . string ; } ] ,
504+ [ "first read android.R.id.content" , function ( ) { return android . R . id . content ; } ] ,
505+ [ "first read android.R.id.text1" , function ( ) { return android . R . id . text1 ; } ] ,
506+ [ "first read android.R.attr.textColor" , function ( ) { return android . R . attr . textColor ; } ] ,
507+ [ "first read android.R.style.Theme" , function ( ) { return android . R . style . Theme ; } ] ,
508+ [ "first access android.view [package]" , function ( ) { return android . view ; } ] ,
509+ [ "first access android.view.View" , function ( ) { return android . view . View ; } ] ,
510+ [ "first access android.widget [package]" , function ( ) { return android . widget ; } ] ,
511+ [ "first access android.widget.TextView" , function ( ) { return android . widget . TextView ; } ] ,
512+ [ "first access android.content.Context" , function ( ) { return android . content . Context ; } ] ,
513+ [ "first access android.app.Activity" , function ( ) { return android . app . Activity ; } ]
514+ ] ;
515+ var results = [ ] ;
516+ rows . forEach ( function ( row ) {
517+ if ( skipped ( row [ 0 ] ) ) return ;
518+ gc ( ) ;
519+ var start = performance . now ( ) ;
520+ globalThis . __jniBenchSink = row [ 1 ] ( ) ;
521+ results . push ( report ( row [ 0 ] , [ ( performance . now ( ) - start ) * 1e6 ] ) ) ;
522+ } ) ;
523+ var name = "first calls of 20 android.view.View methods" ;
524+ if ( ! skipped ( name ) ) {
525+ var view = new android . view . View ( com . tns . NativeScriptApplication . getInstance ( ) ) ;
526+ gc ( ) ;
527+ var start = performance . now ( ) ;
528+ globalThis . __jniBenchSink = [ view . getAlpha ( ) , view . getRotation ( ) , view . getX ( ) , view . getY ( ) , view . getId ( ) ,
529+ view . getWidth ( ) , view . getHeight ( ) , view . getLeft ( ) , view . getTop ( ) , view . getRight ( ) ,
530+ view . getBottom ( ) , view . getPaddingLeft ( ) , view . getPaddingTop ( ) , view . getScaleX ( ) , view . getScaleY ( ) ,
531+ view . getTranslationX ( ) , view . getTranslationY ( ) , view . getVisibility ( ) , view . isEnabled ( ) , view . isFocused ( ) ] ;
532+ results . push ( report ( name , [ ( performance . now ( ) - start ) * 1e6 ] ) ) ;
533+ }
534+ return results ;
535+ }
536+
329537exports . run = function ( ) {
538+ var results = measureFirstAccess ( ) ;
330539 var benchmarks = buildBenchmarks ( ) ;
331- var results = [ ] ;
332540 var index = 0 ;
333541 // Yield to the looper between benchmarks so GC and queued work run outside timed rounds.
334542 function next ( ) {
335543 if ( index >= benchmarks . length ) {
336- console . log ( "JNIBENCH_DONE " + JSON . stringify ( results ) ) ;
544+ runContention ( ) ;
337545 return ;
338546 }
339547 gc ( ) ;
340548 var result = benchmarks [ index ++ ] ( ) ;
341549 if ( result ) results . push ( result ) ;
342550 setTimeout ( next , 50 ) ;
343551 }
552+ var contention = buildContentionBenchmarks ( ) ;
553+ var contentionIndex = 0 ;
554+ function runContention ( ) {
555+ if ( contentionIndex >= contention . length ) {
556+ console . log ( "JNIBENCH_DONE " + JSON . stringify ( results ) ) ;
557+ return ;
558+ }
559+ contention [ contentionIndex ++ ] ( function ( result ) {
560+ if ( result ) results . push . apply ( results , [ ] . concat ( result ) ) ;
561+ setTimeout ( runContention , 50 ) ;
562+ } ) ;
563+ }
344564 setTimeout ( next , 500 ) ;
345565} ;
0 commit comments