1 package org.apache.directmemory.memory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
23 import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
24 import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
25 import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
26 import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
27 import com.carrotsearch.junitbenchmarks.annotation.LabelType;
28 import com.google.common.collect.MapMaker;
29 import org.apache.directmemory.measures.Ram;
30 import org.apache.directmemory.memory.MemoryManager;
31 import org.apache.directmemory.memory.Pointer;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import java.util.Random;
40 import java.util.concurrent.ConcurrentMap;
41 import java.util.concurrent.atomic.AtomicInteger;
42
43 @AxisRange( min = 0, max = 1 )
44 @BenchmarkMethodChart()
45 @BenchmarkHistoryChart( labelWith = LabelType.CUSTOM_KEY, maxRuns = 5 )
46 @Ignore
47 public class Concurrent2Test
48 extends AbstractBenchmark
49 {
50
51 private final static int entries = 100000;
52
53 public static AtomicInteger count = new AtomicInteger();
54
55 private static AtomicInteger got = new AtomicInteger();
56
57 private static AtomicInteger missed = new AtomicInteger();
58
59 private static AtomicInteger good = new AtomicInteger();
60
61 private static AtomicInteger bad = new AtomicInteger();
62
63 private static AtomicInteger read = new AtomicInteger();
64
65 public static ConcurrentMap<String, Pointer<Object>> map =
66 new MapMaker().concurrencyLevel( 4 ).initialCapacity( 100000 ).makeMap();
67
68
69 @BenchmarkOptions( benchmarkRounds = 100000, warmupRounds = 0, concurrency = 100 )
70 @Test
71 public void store()
72 {
73 final String key = "test-" + count.incrementAndGet();
74 put( key );
75 }
76
77 @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 100 )
78 @Test
79 public void retrieveCatchThemAll()
80 {
81 String key = "test-" + ( rndGen.nextInt( entries ) + 1 );
82 Pointer<Object> p = map.get( key );
83 read.incrementAndGet();
84 if ( p != null )
85 {
86 got.incrementAndGet();
87 byte[] payload = MemoryManager.retrieve( p );
88 if ( key.equals( new String( payload ) ) )
89 {
90 good.incrementAndGet();
91 }
92 else
93 {
94 bad.incrementAndGet();
95 }
96 }
97 else
98 {
99 logger.info( "did not find key " + key );
100 missed.incrementAndGet();
101 }
102 }
103
104 @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 100 )
105 @Test
106 public void retrieveCatchHalfOfThem()
107 {
108 String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 );
109 Pointer<Object> p = map.get( key );
110 read.incrementAndGet();
111 if ( p != null )
112 {
113 got.incrementAndGet();
114 byte[] payload = MemoryManager.retrieve( p );
115 if ( key.equals( new String( payload ) ) )
116 {
117 good.incrementAndGet();
118 }
119 else
120 {
121 bad.incrementAndGet();
122 }
123 }
124 else
125 {
126 missed.incrementAndGet();
127 }
128 }
129
130 private void put( String key )
131 {
132 map.put( key, MemoryManager.store( key.getBytes() ) );
133 }
134
135 @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 10 )
136 @Test
137 public void write3Read7()
138 {
139 String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 );
140
141 int what = rndGen.nextInt( 10 );
142
143 switch ( what )
144 {
145 case 0:
146 case 1:
147 case 2:
148 put( key );
149 break;
150 default:
151 get( key );
152 break;
153
154 }
155
156 }
157
158 @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 10 )
159 @Test
160 public void write1Read9()
161 {
162 String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 );
163
164 int what = rndGen.nextInt( 10 );
165
166 switch ( what )
167 {
168 case 0:
169 put( key );
170 break;
171 default:
172 get( key );
173 break;
174
175 }
176
177 }
178
179 private void get( String key )
180 {
181 Pointer<Object> p = map.get( key );
182 read.incrementAndGet();
183 if ( p != null )
184 {
185 got.incrementAndGet();
186 byte[] payload = MemoryManager.retrieve( p );
187 if ( key.equals( new String( payload ) ) )
188 {
189 good.incrementAndGet();
190 }
191 else
192 {
193 bad.incrementAndGet();
194 }
195 }
196 else
197 {
198 missed.incrementAndGet();
199 }
200 }
201
202 Random rndGen = new Random();
203
204 private static Logger logger = LoggerFactory.getLogger( Concurrent2Test.class );
205
206 private static void dump( MemoryManagerService<Object> mms )
207 {
208 logger.info( "off-heap - allocated: " + Ram.inMb( mms.capacity() ) );
209 logger.info( "off-heap - used: " + Ram.inMb( mms.used() ) );
210 logger.info( "heap - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
211 logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
212 logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
213 logger.info( "************************************************" );
214 }
215
216 @BeforeClass
217 public static void init()
218 {
219 MemoryManager.init( 1, Ram.Mb( 512 ) );
220 }
221
222 @AfterClass
223 public static void dump()
224 {
225
226 dump( MemoryManager.getMemoryManager() );
227
228 logger.info( "************************************************" );
229 logger.info( "entries: " + entries );
230 logger.info( "inserted: " + map.size() );
231 logger.info( "reads: " + read );
232 logger.info( "count: " + count );
233 logger.info( "got: " + got );
234 logger.info( "missed: " + missed );
235 logger.info( "good: " + good );
236 logger.info( "bad: " + bad );
237 logger.info( "************************************************" );
238 }
239
240 }