1
1
package com .lewdev .probabilitylib ;
2
2
3
- import java .util .HashMap ;
4
- import java .util .Map ;
5
3
import java .util .concurrent .TimeUnit ;
6
4
7
5
import org .openjdk .jmh .annotations .Benchmark ;
8
6
import org .openjdk .jmh .annotations .BenchmarkMode ;
9
- import org .openjdk .jmh .annotations .Measurement ;
7
+ import org .openjdk .jmh .annotations .Fork ;
8
+ import org .openjdk .jmh .annotations .Level ;
10
9
import org .openjdk .jmh .annotations .Mode ;
10
+ import org .openjdk .jmh .annotations .OutputTimeUnit ;
11
11
import org .openjdk .jmh .annotations .Scope ;
12
12
import org .openjdk .jmh .annotations .Setup ;
13
13
import org .openjdk .jmh .annotations .State ;
14
- import org .openjdk .jmh .annotations .Timeout ;
15
- import org .openjdk .jmh .annotations .Warmup ;
14
+ import org .openjdk .jmh .annotations .TearDown ;
16
15
import org .openjdk .jmh .infra .Blackhole ;
17
16
import org .openjdk .jmh .runner .Runner ;
18
17
import org .openjdk .jmh .runner .RunnerException ;
19
18
import org .openjdk .jmh .runner .options .Options ;
20
19
import org .openjdk .jmh .runner .options .OptionsBuilder ;
21
20
22
21
@ BenchmarkMode (Mode .AverageTime )
22
+ @ OutputTimeUnit (TimeUnit .NANOSECONDS )
23
23
@ State (Scope .Benchmark )
24
- @ Warmup (iterations = 5 , time = 5 )
25
- @ Timeout (time = 25 , timeUnit = TimeUnit .SECONDS )
26
- @ Measurement (iterations = 10 , time = 2 )
24
+ @ Fork (value = 2 , jvmArgs = {"-Xms2G" , "-Xmx2G" })
27
25
public class BenchmarkProbability {
28
26
29
27
public static void main (String [] args ) throws RunnerException {
@@ -35,52 +33,51 @@ public static void main(String[] args) throws RunnerException {
35
33
new Runner (opt ).run ();
36
34
}
37
35
38
- private final int elements = 10_000 ;
36
+ public int elements = 1_000 ;
39
37
40
- ProbabilityMap < Integer > map = new ProbabilityMap <>() ;
41
- ProbabilityCollection < Integer > collection = new ProbabilityCollection <>() ;
38
+ public int toAdd = elements + 1 ;
39
+ public int toAddProb = 10 ;
42
40
43
- private Map <Integer , Integer > addAllTest = new HashMap <>();
41
+ private ProbabilityMap <Integer > map ;
42
+ private ProbabilityCollection <Integer > collection ;
44
43
45
- @ Setup
44
+ @ Setup ( Level . Iteration )
46
45
public void setup () {
46
+ this .map = new ProbabilityMap <>();
47
+ this .collection = new ProbabilityCollection <>();
48
+
47
49
for (int i = 0 ; i < elements ; i ++) {
48
50
map .add (i , 1 );
49
51
collection .add (i , 1 );
50
52
}
51
-
52
- for (int i = elements ; i < elements * 2 ; i ++) {
53
- addAllTest .put (i , 1 );
54
- }
55
53
}
56
54
57
- @ Benchmark
58
- public void mapAddSingle (Blackhole bh ) {
59
- boolean added = this .map .add (25000 , 1 );
60
- bh .consume (added );
55
+ @ TearDown (Level .Iteration )
56
+ public void tearDown () {
57
+ this .map .clear ();
58
+ this .collection .clear ();
59
+
60
+ this .map = null ;
61
+ this .collection = null ;
61
62
}
62
63
63
64
@ Benchmark
64
- public void mapAddAll () {
65
- map .addAll ( addAllTest );
65
+ public void mapAddSingle () {
66
+ this . map .add ( toAdd , toAddProb );
66
67
}
67
68
68
69
@ Benchmark
69
70
public void collectionAddSingle () {
70
- this .collection .add (25000 , 1 );
71
+ this .collection .add (toAdd , toAddProb );
71
72
}
72
73
73
74
@ Benchmark
74
75
public void mapGet (Blackhole bh ) {
75
- for (int i = 0 ; i < elements * 2 ; i ++) {
76
- bh .consume (map .get ());
77
- }
76
+ bh .consume (this .map .get ());
78
77
}
79
78
80
79
@ Benchmark
81
80
public void collectionGet (Blackhole bh ) {
82
- for (int i = 0 ; i < elements * 2 ; i ++) {
83
- bh .consume (collection .get ());
84
- }
81
+ bh .consume (this .collection .get ());
85
82
}
86
83
}
0 commit comments