3131import io .reactivex .Observable ;
3232import io .reactivex .Single ;
3333import io .reactivex .disposables .Disposable ;
34+ import io .reactivex .functions .BiFunction ;
35+ import io .reactivex .functions .Consumer ;
36+ import io .reactivex .functions .Function ;
3437import io .reactivex .schedulers .Schedulers ;
3538
3639/**
@@ -73,56 +76,115 @@ public void setUp() throws Exception {
7376 @ Ignore
7477 @ Test
7578 public void testLocationGatherer () throws Exception {
76- testSensor (() -> new LocationGatherer (sensorConfig , locationManager ,
77- sensorEnableRequester , permissionChecker , gpsSensorChecker , sensorRequirementChecker ));
79+ testSensor (new GathererCreator () {
80+ @ Override
81+ public GPSGatherer create () {
82+ return new LocationGatherer (sensorConfig , locationManager ,
83+ sensorEnableRequester , permissionChecker , gpsSensorChecker , sensorRequirementChecker );
84+ }
85+ });
7886 }
7987
8088 @ Ignore
8189 @ Test
8290 public void testRawGPSMeasurementsGatherer () throws Exception {
83- testSensor (() -> new RawGPSMeasurementsGatherer (sensorConfig , locationManager ,
84- sensorEnableRequester , permissionChecker , rawGPSSensorChecker , sensorRequirementChecker ));
91+ testSensor (new GathererCreator () {
92+ @ Override
93+ public GPSGatherer create () {
94+ return new RawGPSMeasurementsGatherer (sensorConfig , locationManager ,
95+ sensorEnableRequester , permissionChecker , rawGPSSensorChecker , sensorRequirementChecker );
96+ }
97+ });
8598 }
8699
87100 @ Ignore
88101 @ Test
89102 public void testRawGPSNavigationGatherer () throws Exception {
90- testSensor (() -> new RawGPSNavigationGatherer (sensorConfig , locationManager ,
91- sensorEnableRequester , permissionChecker , rawGPSSensorChecker , sensorRequirementChecker ));
103+ testSensor (new GathererCreator () {
104+ @ Override
105+ public GPSGatherer create () {
106+ return new RawGPSNavigationGatherer (sensorConfig , locationManager ,
107+ sensorEnableRequester , permissionChecker , rawGPSSensorChecker , sensorRequirementChecker );
108+ }
109+ });
92110 }
93111
94112 @ Ignore
95113 @ Test
96114 public void testRawGPSStatusGaterer () throws Exception {
97- testSensor (() -> new RawGPSStatusGatherer (sensorConfig , locationManager ,
98- sensorEnableRequester , permissionChecker , rawGPSSensorChecker , sensorRequirementChecker ));
115+ testSensor (new GathererCreator () {
116+ @ Override
117+ public GPSGatherer create () {
118+ return new RawGPSStatusGatherer (sensorConfig , locationManager ,
119+ sensorEnableRequester , permissionChecker , rawGPSSensorChecker , sensorRequirementChecker );
120+ }
121+ });
99122 }
100123
101- private void testSensor (GathererCreator gathererCreator ) {
124+ private void testSensor (final GathererCreator gathererCreator ) {
102125
103- String sensor = gathererCreator .create ().getSensorType ().name ().toUpperCase ();
126+ final String sensor = gathererCreator .create ().getSensorType ().name ().toUpperCase ();
104127
105128 Log .d (sensor + " BENCHMARK" , "Nº\t real" );
106129
107130 Disposable subscribe = locationGatherer .recordStream ().subscribeOn (Schedulers .newThread ()).subscribe ();
108131
109132 Observable .rangeLong (0 , 10 )
110- .map (i -> grab1SecGatheringSamplesAndAverage (gathererCreator )
111- .map (count -> new long []{i , count })
112- ).map (Single ::blockingGet )
113- .subscribe (result -> Log .d (sensor + " BENCHMARK" ,
114- String .format ("%d\t %d" , result [0 ], result [1 ])));
133+ .map (new Function <Long , Single <BenchmarkStep >>() {
134+ @ Override
135+ public Single <BenchmarkStep > apply (final Long i ) throws Exception {
136+ return grab1SecGatheringSamplesAndAverage (gathererCreator )
137+ .map (new Function <Long , BenchmarkStep >() {
138+ @ Override
139+ public BenchmarkStep apply (Long count ) throws Exception {
140+ return new BenchmarkStep (i , count );
141+ }
142+ });
143+ }
144+ })
145+ .map (new Function <Single <BenchmarkStep >, BenchmarkStep >() {
146+ @ Override
147+ public BenchmarkStep apply (Single <BenchmarkStep > benchmarkStepSingle ) throws Exception {
148+ return benchmarkStepSingle .blockingGet ();
149+ }
150+ })
151+ .subscribe (new Consumer <BenchmarkStep >() {
152+ @ Override
153+ public void accept (BenchmarkStep benchmarkStep ) throws Exception {
154+ Log .d (sensor + " BENCHMARK" ,
155+ String .format ("%d\t %d" , benchmarkStep .number , benchmarkStep .count ));
156+ }
157+ });
115158
116159 subscribe .dispose ();
117160 }
118161
119- private Single <Long > grab1SecGatheringSamplesAndAverage (GathererCreator gathererCreator ) {
162+ private Single <Long > grab1SecGatheringSamplesAndAverage (final GathererCreator gathererCreator ) {
120163 return Observable .range (0 , 3 )
121- .map (__ -> gatherDuring10Sec (gathererCreator ))
122- .map (Single ::blockingGet )
123- .reduce (new long [2 ], (aggregate , result ) ->
124- new long []{aggregate [0 ]+1 , aggregate [1 ] + result })
125- .map (result -> result [0 ] == 0 ? 0 : result [1 ] / result [0 ]);
164+ .map (new Function <Integer , Single <Long >>() {
165+ @ Override
166+ public Single <Long > apply (Integer integer ) throws Exception {
167+ return gatherDuring10Sec (gathererCreator );
168+ }
169+ })
170+ .map (new Function <Single <Long >, Long >() {
171+ @ Override
172+ public Long apply (Single <Long > longSingle ) throws Exception {
173+ return longSingle .blockingGet ();
174+ }
175+ })
176+ .reduce (new long [2 ], new BiFunction <long [], Long , long []>() {
177+ @ Override
178+ public long [] apply (long [] aggregate , Long result ) throws Exception {
179+ return new long []{aggregate [0 ]+1 , aggregate [1 ] + result };
180+ }
181+ })
182+ .map (new Function <long [], Long >() {
183+ @ Override
184+ public Long apply (long [] result ) throws Exception {
185+ return result [0 ] == 0 ? 0 : result [1 ] / result [0 ];
186+ }
187+ });
126188 }
127189
128190 private Single <Long > gatherDuring10Sec (GathererCreator gathererCreator ) {
@@ -133,11 +195,15 @@ private Single<Long> gatherDuring10Sec(GathererCreator gathererCreator) {
133195 .count ();
134196 }
135197
136- /*
137- ------------------------
138- Gatherer creators
139- ------------------------
140- */
198+ private class BenchmarkStep {
199+ private final long number ;
200+ private final long count ;
201+
202+ private BenchmarkStep (long number , long count ) {
203+ this .number = number ;
204+ this .count = count ;
205+ }
206+ }
141207
142208 private interface GathererCreator {
143209 GPSGatherer create ();
0 commit comments