11package com .lewdev .probabilitylib ;
22
3- import java .util .HashMap ;
4- import java .util .Map ;
53import java .util .concurrent .TimeUnit ;
64
75import org .openjdk .jmh .annotations .Benchmark ;
86import 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 ;
109import org .openjdk .jmh .annotations .Mode ;
10+ import org .openjdk .jmh .annotations .OutputTimeUnit ;
1111import org .openjdk .jmh .annotations .Scope ;
1212import org .openjdk .jmh .annotations .Setup ;
1313import 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 ;
1615import org .openjdk .jmh .infra .Blackhole ;
1716import org .openjdk .jmh .runner .Runner ;
1817import org .openjdk .jmh .runner .RunnerException ;
1918import org .openjdk .jmh .runner .options .Options ;
2019import org .openjdk .jmh .runner .options .OptionsBuilder ;
2120
2221@ BenchmarkMode (Mode .AverageTime )
22+ @ OutputTimeUnit (TimeUnit .NANOSECONDS )
2323@ 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" })
2725public class BenchmarkProbability {
2826
2927 public static void main (String [] args ) throws RunnerException {
@@ -35,52 +33,51 @@ public static void main(String[] args) throws RunnerException {
3533 new Runner (opt ).run ();
3634 }
3735
38- private final int elements = 10_000 ;
36+ public int elements = 1_000 ;
3937
40- ProbabilityMap < Integer > map = new ProbabilityMap <>() ;
41- ProbabilityCollection < Integer > collection = new ProbabilityCollection <>() ;
38+ public int toAdd = elements + 1 ;
39+ public int toAddProb = 10 ;
4240
43- private Map <Integer , Integer > addAllTest = new HashMap <>();
41+ private ProbabilityMap <Integer > map ;
42+ private ProbabilityCollection <Integer > collection ;
4443
45- @ Setup
44+ @ Setup ( Level . Iteration )
4645 public void setup () {
46+ this .map = new ProbabilityMap <>();
47+ this .collection = new ProbabilityCollection <>();
48+
4749 for (int i = 0 ; i < elements ; i ++) {
4850 map .add (i , 1 );
4951 collection .add (i , 1 );
5052 }
51-
52- for (int i = elements ; i < elements * 2 ; i ++) {
53- addAllTest .put (i , 1 );
54- }
5553 }
5654
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 ;
6162 }
6263
6364 @ Benchmark
64- public void mapAddAll () {
65- map .addAll ( addAllTest );
65+ public void mapAddSingle () {
66+ this . map .add ( toAdd , toAddProb );
6667 }
6768
6869 @ Benchmark
6970 public void collectionAddSingle () {
70- this .collection .add (25000 , 1 );
71+ this .collection .add (toAdd , toAddProb );
7172 }
7273
7374 @ Benchmark
7475 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 ());
7877 }
7978
8079 @ Benchmark
8180 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 ());
8582 }
8683}
0 commit comments