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 static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24
25 import java.io.IOException;
26 import java.util.Map;
27 import java.util.Random;
28 import java.util.zip.CRC32;
29 import java.util.zip.Checksum;
30
31 import org.apache.directmemory.measures.Ram;
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
42 import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
43 import com.google.common.collect.Maps;
44
45 @Ignore
46 public class BaseTest
47 extends AbstractBenchmark
48 {
49
50 MemoryManagerService<Object> mem;
51
52 @Before
53 public void initMMS()
54 {
55 mem = new MemoryManagerServiceImpl<Object>();
56 mem.init( 1, 1 * 1024 * 1024 );
57 }
58
59 @After
60 public void cleanup()
61 throws IOException
62 {
63 if ( mem != null )
64 {
65 mem.close();
66 }
67 }
68
69 @Test
70 public void smokeTest()
71 {
72 logger.info( "buffer size=" + mem.capacity() );
73 assertNotNull( mem );
74
75 Random rnd = new Random();
76
77 int size = 0;
78
79 logger.info( "size=" + size );
80
81 Pointer<Object> p = mem.store( new byte[size] );
82 assertNotNull( p );
83 assertEquals( size, p.getSize() );
84 assertEquals( size, mem.used() );
85 mem.free( p );
86 assertEquals( 0, mem.used() );
87 }
88
89 private static Logger logger = LoggerFactory.getLogger( MallocTest.class );
90
91 private static Random rnd = new Random();
92
93 final static Map<String, Byte> test = Maps.newHashMap();
94
95 private static int errors;
96
97 public long crc( String str )
98 {
99 final Checksum checksum = new CRC32();
100
101 final byte bytes[] = str.getBytes();
102 checksum.update( bytes, 0, bytes.length );
103 return checksum.getValue();
104 }
105
106 @BeforeClass
107 @AfterClass
108 public static void setup()
109 {
110 rnd = new Random();
111
112
113 logger.info( "test - size: " + test.size() );
114 logger.info( "test - errors: " + errors );
115 logger.info( "heap - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
116 logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
117 logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
118 logger.info( "************************************************" );
119 }
120
121 @BenchmarkOptions( benchmarkRounds = 10000, warmupRounds = 0, concurrency = 10 )
122 @Test
123 public void anyDuplicates()
124 {
125 String key = "test" + rnd.nextInt( 100000 );
126 if ( test.containsKey( key ) )
127 {
128 logger.error( "key " + key + " has already been used" );
129 errors++;
130 }
131 test.put( key, (byte) 0 );
132 }
133
134 @Test
135 public void aFewEntriesWithRead()
136 {
137 logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
138 assertNotNull( mem );
139 int howMany = 10000;
140 logger.info( "payload size is variable" );
141 logger.info( "entries=" + howMany );
142 String test = "this is a nicely crafted test";
143 for ( int i = 0; i < howMany; i++ )
144 {
145 final byte[] payload = ( test + " - " + i ).getBytes();
146 Pointer<Object> p = mem.store( payload );
147 final byte[] check = mem.retrieve( p );
148 assertNotNull( check );
149 assertEquals( test + " - " + i, new String( check ) );
150 long crc1 = crc32( payload );
151 long crc2 = crc32( check );
152 assertEquals( crc1, crc2 );
153 }
154
155 logger.info( "total used=" + Ram.inMb( mem.used() ) );
156 }
157
158 private static long crc32( byte[] payload )
159 {
160 final Checksum checksum = new CRC32();
161 checksum.update( payload, 0, payload.length );
162 return checksum.getValue();
163 }
164
165 @Test
166 public void aFewEntriesWithCheck()
167 {
168 logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
169 assertNotNull( mem );
170 int howMany = 10;
171 logger.info( "payload size is variable" );
172 logger.info( "entries=" + howMany );
173 String test = "this is a nicely crafted test";
174 Pointer<Object> lastP = null;
175 for ( int i = 0; i < howMany; i++ )
176 {
177 byte[] payload = ( test + " - " + i ).getBytes();
178 Pointer<Object> p = mem.store( payload );
179
180 logger.info( "p.end=" + p.getSize() );
181 if ( lastP != null )
182 {
183
184
185 }
186 assertEquals( p.getCapacity(), payload.length );
187 lastP = p;
188 logger.info( "---" );
189 }
190
191 logger.info( "total used=" + Ram.inMb( mem.used() ) );
192 }
193
194 @Test
195 public void checkExpiration()
196 throws InterruptedException
197 {
198 assertNotNull( mem );
199 int size = 400;
200 int howMany = 5000;
201
202 logger.info( "off-heap capacity=" + Ram.inMb( mem.capacity() ) );
203 logger.info( "payload size=" + Ram.inKb( size ) );
204 logger.info( "entries=" + howMany );
205
206 byte[] payload = new byte[size];
207 for ( int i = 0; i < howMany; i++ )
208 {
209 mem.store( payload, 2000 );
210 }
211
212 assertEquals( size * howMany, mem.used() );
213
214 logger.info( "entries with relative expiration=" + ( howMany / 2 ) );
215 for ( int i = 0; i < howMany / 2; i++ )
216 {
217 mem.store( payload, 100 );
218 }
219 assertEquals( size * howMany + size * howMany / 2, mem.used() );
220
221 logger.info( "entries with absolute expiration=" + ( howMany / 2 ) );
222 for ( int i = 0; i < howMany / 2; i++ )
223 {
224 mem.store( payload, 1 );
225 }
226 assertEquals( size * howMany * 2, mem.used() );
227 logger.info( "total used=" + Ram.inMb( mem.used() ) );
228
229 Thread.sleep( 1000 );
230
231 logger.info( "calling disposeExpiredAbsolute" );
232 mem.collectExpired();
233 logger.info( "total used=" + Ram.inMb( mem.used() ) );
234 assertEquals( size * howMany + size * howMany / 2, mem.used() );
235
236 logger.info( "calling disposeExpiredRelative" );
237 mem.collectExpired();
238 logger.info( "total used=" + Ram.inMb( mem.used() ) );
239 assertEquals( size * howMany, mem.used() );
240
241 Thread.sleep( 2000 );
242
243 logger.info( "calling disposeExpiredRelative again" );
244 mem.collectExpired();
245 logger.info( "total used=" + Ram.inMb( mem.used() ) );
246 assertEquals( 0, mem.used() );
247
248 }
249 }