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.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         // logger.info("off-heap allocated: " + Ram.inMb(mem.capacity()));
106         // logger.info("off-heap used:      " + Ram.inMb(mem.used()));
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         // logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
119         assertNotNull( mem );
120         int howMany = 100000;
121         // logger.info( "payload size is variable" );
122         // logger.info( "entries=" + howMany );
123         // String test = "this is a nicely crafted test";
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         // logger.info( "total used=" + Ram.inMb( mem.used() ) );
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         // logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
150         assertNotNull( mem );
151         int howMany = 10;
152         // logger.info( "payload size is variable" );
153         // logger.info( "entries=" + howMany );
154         // String test = "this is a nicely crafted test";
155         // Pointer<Object> lastP = null;
156         for ( int i = 0; i < howMany; i++ )
157         {
158             byte[] payload = ( test + " - " + i ).getBytes();
159             Pointer<Object> p = mem.store( payload );
160             // logger.info( "p.start=" + p.getStart() );
161             // logger.info( "p.end=" + p.getEnd() );
162             assertEquals( p.getCapacity(), payload.length );
163             // lastP = p;
164             // logger.info( "---" );
165         }
166 
167         // logger.info( "total used=" + Ram.inMb( mem.used() ) );
168     }
169 }