1 package org.apache.directmemory.preliminary;
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.google.common.collect.MapMaker;
25 import com.google.common.collect.Maps;
26 import org.apache.directmemory.measures.Ram;
27 import org.junit.Test;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.nio.ByteBuffer;
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentMap;
34 import java.util.concurrent.TimeUnit;
35
36 import static org.junit.Assert.assertNotNull;
37
38 public class PreliminarBenchmark
39 extends AbstractBenchmark
40 {
41
42 private static Logger logger = LoggerFactory.getLogger( PreliminarBenchmark.class );
43
44 final static byte payload[] = new byte[1024];
45
46
47
48 public void cleanup()
49 {
50 dump( "Before cleanup" );
51 Runtime.getRuntime().gc();
52 dump( "After cleanup" );
53 logger.info( "************************************************" );
54 }
55
56 private void dump( String message )
57 {
58 logger.info( message );
59 logger.info( "Memory - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
60 logger.info( "Memory - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
61 logger.info( "Memory - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
62 }
63
64 @BenchmarkOptions( benchmarkRounds = 5, warmupRounds = 0 )
65 @Test
66 public void justMap()
67 {
68 final Map<String, byte[]> test = Maps.newHashMap();
69 long ops = 100000;
70 for ( int i = 0; i < ops; i++ )
71 {
72 final String key = "test-" + i;
73 test.put( key, payload.clone() );
74 }
75 logger.info( "stored " + Ram.inMb( payload.length * ops ) );
76 }
77
78 @BenchmarkOptions( benchmarkRounds = 1, warmupRounds = 0 )
79 @Test
80 public void oneMillionSmallWithDirectBuffersOneAllocation()
81 {
82
83 logger.info( "payload is " + payload.length + " bytes" );
84 final byte payload[] = new byte[500];
85 int ops = 1000000;
86
87 pumpWithOneAllocation( ops, payload );
88
89 }
90
91 @BenchmarkOptions( benchmarkRounds = 1, warmupRounds = 0 )
92 @Test
93 public void lessButLargerWithDirectBuffersOneAllocation()
94 {
95
96 logger.info( "payload is " + payload.length + " bytes" );
97 final byte payload[] = new byte[2048];
98 int ops = 210000;
99
100 pumpWithOneAllocation( ops, payload );
101
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 @BenchmarkOptions( benchmarkRounds = 5, warmupRounds = 0 )
124 @Test
125 public void withDirectBuffers150k()
126 {
127
128 logger.info( "payload is " + payload.length + " bytes" );
129 int ops = 150000;
130
131 pump( ops );
132 }
133
134 @BenchmarkOptions( benchmarkRounds = 5, warmupRounds = 0 )
135 @Test
136 public void withDirectBuffers180k()
137 {
138
139 logger.info( "payload is " + payload.length + " bytes" );
140 int ops = 180000;
141
142 pump( ops );
143 }
144
145 @BenchmarkOptions( benchmarkRounds = 5, warmupRounds = 0 )
146 @Test
147 public void withDirectBuffers150kAgain()
148 {
149
150 logger.info( "payload is " + payload.length + " bytes" );
151 int ops = 150000;
152
153 pump( ops );
154 }
155
156 @BenchmarkOptions( benchmarkRounds = 1, warmupRounds = 0 )
157 @Test
158 public void testAllocation()
159 {
160
161 logger.info( "payload is " + payload.length + " bytes" );
162 logger.info( "allocating " + Ram.inMb( payload.length * 200000 ) );
163 ByteBuffer buf = ByteBuffer.allocateDirect( payload.length * 200000 );
164 assertNotNull( buf );
165 logger.info( "done" );
166 }
167
168
169 private void pumpWithOneAllocation( int ops, byte[] payload )
170 {
171
172 ConcurrentMap<String, ByteBuffer> test =
173 new MapMaker().concurrencyLevel( 4 ).maximumSize( ops ).expireAfterWrite( 10, TimeUnit.MINUTES ).makeMap();
174
175 logger.info( Ram.inMb( ops * payload.length ) + " in " + ops + " slices to store" );
176
177 ByteBuffer bulk = ByteBuffer.allocateDirect( ops * payload.length );
178
179 double started = System.currentTimeMillis();
180
181 for ( int i = 0; i < ops; i++ )
182 {
183 bulk.position( i * payload.length );
184 final ByteBuffer buf = bulk.duplicate();
185 buf.put( payload );
186 test.put( "test-" + i, buf );
187 }
188
189 double finished = System.currentTimeMillis();
190
191 logger.info( "done in " + ( finished - started ) / 1000 + " seconds" );
192
193 for ( ByteBuffer buf : test.values() )
194 {
195 buf.clear();
196 }
197 }
198
199 @BenchmarkOptions( benchmarkRounds = 5, warmupRounds = 0 )
200 @Test
201 public void withDirectBuffers100k()
202 {
203
204 logger.info( "payload is " + payload.length + " bytes" );
205 int ops = 100000;
206
207 pump( ops );
208 }
209
210 private void pump( int ops )
211 {
212 ConcurrentMap<String, ByteBuffer> test =
213 new MapMaker().concurrencyLevel( 4 ).maximumSize( ops ).expireAfterWrite( 10, TimeUnit.MINUTES ).makeMap();
214
215 logger.info( Ram.inMb( ops * payload.length ) + " to store" );
216
217 double started = System.currentTimeMillis();
218
219 for ( int i = 0; i < ops; i++ )
220 {
221 ByteBuffer buf = ByteBuffer.allocateDirect( payload.length );
222 buf.put( payload );
223 test.put( "test-" + i, buf );
224 }
225
226 double finished = System.currentTimeMillis();
227
228 logger.info( "done in " + ( finished - started ) / 1000 + " seconds" );
229
230 for ( ByteBuffer buf : test.values() )
231 {
232 buf.clear();
233 }
234 }
235
236 }