File tree Expand file tree Collapse file tree 4 files changed +94
-4
lines changed
rxjava-core/src/perf/java/rx/usecases Expand file tree Collapse file tree 4 files changed +94
-4
lines changed Original file line number Diff line number Diff line change
1
+ package rx .usecases ;
2
+
3
+ import java .util .Iterator ;
4
+
5
+ import org .openjdk .jmh .annotations .GenerateMicroBenchmark ;
6
+
7
+ public class PerfBaseline {
8
+
9
+ @ GenerateMicroBenchmark
10
+ public void forLoopConsumption (UseCaseInput input ) throws InterruptedException {
11
+ for (int i = 0 ; i < input .size ; i ++) {
12
+ input .observer .onNext (i );
13
+ }
14
+ }
15
+
16
+ @ GenerateMicroBenchmark
17
+ public void observableConsumption (UseCaseInput input ) throws InterruptedException {
18
+ input .observable .subscribe (input .observer );
19
+ input .awaitCompletion ();
20
+ }
21
+
22
+ @ GenerateMicroBenchmark
23
+ public void iterableViaForLoopConsumption (UseCaseInput input ) throws InterruptedException {
24
+ for (int i : input .iterable ) {
25
+ input .observer .onNext (i );
26
+ }
27
+ }
28
+
29
+ @ GenerateMicroBenchmark
30
+ public void iterableViaHasNextConsumption (UseCaseInput input ) throws InterruptedException {
31
+ Iterator <Integer > iterator = input .iterable .iterator ();
32
+ while (iterator .hasNext ()) {
33
+ input .observer .onNext (iterator .next ());
34
+ }
35
+ }
36
+ }
Original file line number Diff line number Diff line change 22
22
public class PerfObserveOn {
23
23
24
24
@ GenerateMicroBenchmark
25
- public void observeOn (UseCaseInput input ) throws InterruptedException {
25
+ public void observeOnComputation (UseCaseInput input ) throws InterruptedException {
26
26
input .observable .observeOn (Schedulers .computation ()).subscribe (input .observer );
27
27
input .awaitCompletion ();
28
28
}
29
29
30
+ @ GenerateMicroBenchmark
31
+ public void observeOnNewThread (UseCaseInput input ) throws InterruptedException {
32
+ input .observable .observeOn (Schedulers .newThread ()).subscribe (input .observer );
33
+ input .awaitCompletion ();
34
+ }
35
+
36
+ @ GenerateMicroBenchmark
37
+ public void observeOnImmediate (UseCaseInput input ) throws InterruptedException {
38
+ input .observable .observeOn (Schedulers .immediate ()).subscribe (input .observer );
39
+ input .awaitCompletion ();
40
+ }
41
+
30
42
}
Original file line number Diff line number Diff line change 19
19
20
20
import rx .Observable ;
21
21
import rx .functions .Func1 ;
22
- import rx .schedulers .Schedulers ;
23
22
24
23
public class PerfTransforms {
25
24
26
25
@ GenerateMicroBenchmark
27
- public void mapTransformation (UseCaseInput input ) throws InterruptedException {
26
+ public void mapPassThru (UseCaseInput input ) throws InterruptedException {
27
+ input .observable .map (new Func1 <Integer , Integer >() {
28
+
29
+ @ Override
30
+ public Integer call (Integer i ) {
31
+ return i ;
32
+ }
33
+
34
+ }).subscribe (input .observer );
35
+ input .awaitCompletion ();
36
+ }
37
+
38
+ @ GenerateMicroBenchmark
39
+ public void mapIntStringInt (UseCaseInput input ) throws InterruptedException {
28
40
input .observable .map (new Func1 <Integer , String >() {
29
41
30
42
@ Override
@@ -44,7 +56,7 @@ public Integer call(String i) {
44
56
}
45
57
46
58
@ GenerateMicroBenchmark
47
- public void flatMapTransforms (UseCaseInput input ) throws InterruptedException {
59
+ public void flatMapInt (UseCaseInput input ) throws InterruptedException {
48
60
input .observable .flatMap (new Func1 <Integer , Observable <Integer >>() {
49
61
50
62
@ Override
Original file line number Diff line number Diff line change 15
15
*/
16
16
package rx .usecases ;
17
17
18
+ import java .util .Iterator ;
18
19
import java .util .concurrent .CountDownLatch ;
19
20
20
21
import org .openjdk .jmh .annotations .Param ;
@@ -36,6 +37,7 @@ public class UseCaseInput {
36
37
@ Param ({ "1" , "1024" })
37
38
public int size ;
38
39
40
+ public Iterable <Integer > iterable ;
39
41
public Observable <Integer > observable ;
40
42
public Observer <Integer > observer ;
41
43
@@ -52,6 +54,34 @@ public void call(Subscriber<? super Integer> o) {
52
54
o .onCompleted ();
53
55
}
54
56
});
57
+
58
+ iterable = new Iterable <Integer >() {
59
+
60
+ @ Override
61
+ public Iterator <Integer > iterator () {
62
+ return new Iterator <Integer >() {
63
+
64
+ int i =0 ;
65
+
66
+ @ Override
67
+ public boolean hasNext () {
68
+ return i < size ;
69
+ }
70
+
71
+ @ Override
72
+ public Integer next () {
73
+ return i ++;
74
+ }
75
+
76
+ @ Override
77
+ public void remove () {
78
+
79
+ }
80
+
81
+ };
82
+ }
83
+
84
+ };
55
85
56
86
latch = new CountDownLatch (1 );
57
87
You can’t perform that action at this time.
0 commit comments