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.Pointer;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.io.IOException;
39 import java.util.Random;
40 import java.util.concurrent.ConcurrentMap;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNotNull;
44
45 @AxisRange( min = 0, max = 1 )
46 @BenchmarkMethodChart( )
47 @BenchmarkOptions( benchmarkRounds = 1, warmupRounds = 0 )
48 @BenchmarkHistoryChart( labelWith = LabelType.CUSTOM_KEY, maxRuns = 5 )
49 @Ignore
50 public class MallocTest
51 extends AbstractBenchmark
52 {
53
54 Random rnd = new Random();
55
56 private static Logger logger = LoggerFactory.getLogger( MallocTest.class );
57
58 @After
59 public void dump()
60 throws IOException
61 {
62 logger.info( "off-heap allocated: " + Ram.inMb( mem.capacity() ) );
63 logger.info( "off-heap used: " + Ram.inMb( mem.used() ) );
64 logger.info( "heap - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
65 logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
66 logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
67 logger.info( "************************************************" );
68
69 if ( mem != null )
70 {
71 mem.close();
72 }
73 }
74
75 MemoryManagerService<Object> mem;
76
77 @Before
78 public void initMMS()
79 {
80 mem = new MemoryManagerServiceImpl<Object>();
81 mem.init( 1, 512 * 1024 * 1024 );
82 }
83
84 @Test
85 public void oneMillionEntries()
86 {
87 assertNotNull( mem );
88 int howMany = 1000000;
89 int size = (int) mem.capacity() / ( howMany );
90 size -= size / 100 * 1;
91 logger.info( "payload size=" + size );
92 logger.info( "entries=" + howMany );
93
94 logger.info( "starting..." );
95
96 long start = System.currentTimeMillis();
97
98 byte[] payload = new byte[size];
99 for ( int i = 0; i < howMany; i++ )
100 {
101 mem.store( payload );
102 }
103
104 logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
105 }
106
107 @Test
108 public void twoMillionEntries()
109 {
110
111 assertNotNull( mem );
112 int howMany = 2000000;
113 int size = (int) mem.capacity() / ( howMany );
114 size -= size / 100 * 1;
115 logger.info( "payload size=" + size );
116 logger.info( "entries=" + howMany );
117
118 logger.info( "starting..." );
119 long start = System.currentTimeMillis();
120
121 byte[] payload = new byte[size];
122 for ( int i = 0; i < howMany; i++ )
123 {
124 mem.store( payload );
125 }
126
127 logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
128 }
129
130 @Test
131 public void fiveMillionEntries()
132 {
133
134 assertNotNull( mem );
135 int howMany = 5000000;
136 int size = (int) mem.capacity() / ( howMany );
137 size -= size / 100 * 1;
138 logger.info( "payload size=" + size );
139 logger.info( "entries=" + howMany );
140
141 logger.info( "starting..." );
142 long start = System.currentTimeMillis();
143
144 byte[] payload = new byte[size];
145 for ( int i = 0; i < howMany; i++ )
146 {
147 mem.store( payload );
148 }
149
150 logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
151 }
152
153 @Test
154 public void withMap()
155 {
156
157 ConcurrentMap<Long, Pointer<Object>> map =
158 new MapMaker().concurrencyLevel( 4 ).initialCapacity( 500000 ).makeMap();
159
160 String str = "This is the string to store into the off-heap memory";
161
162 int size = str.length();
163 int howMany = 1000000;
164 byte[] payload = str.getBytes();
165
166 logger.info( "adding " + howMany + " strings of " + size + " bytes..." );
167 for ( long i = 0; i < howMany; i++ )
168 {
169 Pointer<Object> p = mem.store( payload );
170 map.put( i, p );
171 }
172 logger.info( "...done" );
173
174 }
175
176 @Before
177 public void before()
178 {
179 mem.clear();
180 }
181
182 @Test
183 public void oneMillionEntriesWithRead()
184 {
185
186 logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
187 assertNotNull( mem );
188 int size = 400;
189 int howMany = 1000000;
190 logger.info( "payload size=" + Ram.inKb( size ) );
191 logger.info( "entries=" + howMany );
192 String test = "this is a nicely crafted test";
193 byte[] payload = test.getBytes();
194 for ( int i = 0; i < howMany; i++ )
195 {
196 Pointer<Object> p = mem.store( payload );
197 byte[] check = mem.retrieve( p );
198 assertNotNull( check );
199 assertEquals( test, new String( check ) );
200 }
201
202 logger.info( "total used=" + Ram.inMb( mem.used() ) );
203 }
204 }