View Javadoc

1   package org.apache.directmemory.memory;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.Random;
27  import java.util.concurrent.ConcurrentMap;
28  
29  import org.apache.directmemory.measures.Ram;
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Ignore;
33  import org.junit.Test;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import com.google.common.collect.MapMaker;
38  
39  @Ignore
40  public class MallocWithUnsafeTest
41  {
42  
43      Random rnd = new Random();
44  
45      private static Logger logger = LoggerFactory.getLogger( MallocWithUnsafeTest.class );
46  
47      @After
48      public void dump()
49          throws IOException
50      {
51          logger.info( "off-heap allocated: " + Ram.inMb( mem.capacity() ) );
52          logger.info( "off-heap used:      " + Ram.inMb( mem.used() ) );
53          logger.info( "heap - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
54          logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
55          logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
56          logger.info( "************************************************" );
57  
58          if ( mem != null )
59          {
60              mem.close();
61          }
62      }
63  
64      MemoryManagerService<Object> mem;
65  
66      @Before
67      public void initMMS()
68      {
69          mem = new UnsafeMemoryManagerServiceImpl<Object>();
70          mem.init( 1, 512 * 1024 * 1024 );
71      }
72  
73      @Test
74      public void oneMillionEntries()
75      {
76          assertNotNull( mem );
77          int howMany = 1000000;
78          int size = (int) mem.capacity() / ( howMany );
79          size -= size / 100 * 1;
80          logger.info( "payload size=" + size );
81          logger.info( "entries=" + howMany );
82  
83          logger.info( "starting..." );
84  
85          long start = System.currentTimeMillis();
86  
87          byte[] payload = new byte[size];
88          for ( int i = 0; i < howMany; i++ )
89          {
90              mem.store( payload );
91          }
92  
93          logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
94      }
95  
96      @Test
97      public void twoMillionEntries()
98      {
99  
100         assertNotNull( mem );
101         int howMany = 2000000;
102         int size = (int) mem.capacity() / ( howMany );
103         size -= size / 100 * 1;
104         logger.info( "payload size=" + size );
105         logger.info( "entries=" + howMany );
106 
107         logger.info( "starting..." );
108         long start = System.currentTimeMillis();
109 
110         byte[] payload = new byte[size];
111         for ( int i = 0; i < howMany; i++ )
112         {
113             mem.store( payload );
114         }
115 
116         logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
117     }
118 
119     @Test
120     public void fiveMillionEntries()
121     {
122 
123         assertNotNull( mem );
124         int howMany = 5000000;
125         int size = (int) mem.capacity() / ( howMany );
126         size -= size / 100 * 1;
127         logger.info( "payload size=" + size );
128         logger.info( "entries=" + howMany );
129 
130         logger.info( "starting..." );
131         long start = System.currentTimeMillis();
132 
133         byte[] payload = new byte[size];
134         for ( int i = 0; i < howMany; i++ )
135         {
136             mem.store( payload );
137         }
138 
139         logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
140     }
141 
142     @Test
143     public void withMap()
144     {
145 
146         ConcurrentMap<Long, Pointer<Object>> map =
147             new MapMaker().concurrencyLevel( 4 ).initialCapacity( 500000 ).makeMap();
148 
149         String str = "This is the string to store into the off-heap memory";
150 
151         int size = str.length();
152         int howMany = 1000000;
153         byte[] payload = str.getBytes();
154 
155         logger.info( "adding " + howMany + " strings of " + size + " bytes..." );
156         for ( long i = 0; i < howMany; i++ )
157         {
158             Pointer<Object> p = mem.store( payload );
159             map.put( i, p );
160         }
161         logger.info( "...done" );
162 
163     }
164 
165     @Before
166     public void before()
167     {
168         mem.clear();
169     }
170 
171     @Test
172     public void oneMillionEntriesWithRead()
173     {
174 
175         logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
176         assertNotNull( mem );
177         int size = 400;
178         int howMany = 1000000;
179         logger.info( "payload size=" + Ram.inKb( size ) );
180         logger.info( "entries=" + howMany );
181         String test = "this is a nicely crafted test";
182         byte[] payload = test.getBytes();
183         for ( int i = 0; i < howMany; i++ )
184         {
185             Pointer<Object> p = mem.store( payload );
186             byte[] check = mem.retrieve( p );
187             assertNotNull( check );
188             assertEquals( test, new String( check ) );
189         }
190 
191         logger.info( "total used=" + Ram.inMb( mem.used() ) );
192     }
193 }