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.Test;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
41 import com.google.common.collect.Maps;
42
43 public class BaseUnsafeTest
44 extends AbstractBenchmark
45 {
46
47 MemoryManagerService<Object> mem;
48
49 @Before
50 public void initMMS()
51 {
52 mem = new UnsafeMemoryManagerServiceImpl<Object>();
53 mem.init( 1, 1 * 1024 * 1024 );
54 }
55
56 @After
57 public void cleanup()
58 throws IOException
59 {
60 if ( mem != null )
61 {
62 mem.close();
63 }
64 }
65
66 @Test
67 public void smokeTest()
68 {
69 logger.info( "buffer size=" + mem.capacity() );
70 assertNotNull( mem );
71
72 Random rnd = new Random();
73
74 int size = rnd.nextInt( 10 ) * (int) mem.capacity() / 100;
75
76 logger.info( "size=" + size );
77
78 Pointer<Object> p = mem.store( new byte[size] );
79 assertNotNull( p );
80 assertEquals( size, mem.used() );
81 assertEquals( size, p.getSize() );
82 mem.free( p );
83 assertEquals( 0, mem.used() );
84 }
85
86 private static Logger logger = LoggerFactory.getLogger( MallocTest.class );
87
88 final static Map<String, Byte> test = Maps.newHashMap();
89
90 private static int errors;
91
92 public long crc( String str )
93 {
94 final Checksum checksum = new CRC32();
95
96 final byte bytes[] = str.getBytes();
97 checksum.update( bytes, 0, bytes.length );
98 return checksum.getValue();
99 }
100
101 @BeforeClass
102 @AfterClass
103 public static void setup()
104 {
105
106
107 logger.info( "test - size: " + test.size() );
108 logger.info( "test - errors: " + errors );
109 logger.info( "heap - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
110 logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
111 logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
112 logger.info( "************************************************" );
113 }
114
115 @Test
116 public void aFewEntriesWithRead()
117 {
118
119 assertNotNull( mem );
120 int howMany = 100000;
121
122
123
124 for ( int i = 0; i < howMany; i++ )
125 {
126 final byte[] payload = ( test + " - " + i ).getBytes();
127 Pointer<Object> p = mem.store( payload );
128 final byte[] check = mem.retrieve( p );
129 assertNotNull( check );
130 assertEquals( test + " - " + i, new String( check ) );
131 long crc1 = crc32( payload );
132 long crc2 = crc32( check );
133 assertEquals( crc1, crc2 );
134 }
135
136
137 }
138
139 private static long crc32( byte[] payload )
140 {
141 final Checksum checksum = new CRC32();
142 checksum.update( payload, 0, payload.length );
143 return checksum.getValue();
144 }
145
146 @Test
147 public void aFewEntriesWithCheck()
148 {
149
150 assertNotNull( mem );
151 int howMany = 10;
152
153
154
155
156 for ( int i = 0; i < howMany; i++ )
157 {
158 byte[] payload = ( test + " - " + i ).getBytes();
159 Pointer<Object> p = mem.store( payload );
160
161
162 assertEquals( p.getCapacity(), payload.length );
163
164
165 }
166
167
168 }
169 }