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  
29  import org.apache.directmemory.measures.Ram;
30  import org.junit.AfterClass;
31  import org.junit.BeforeClass;
32  import org.junit.Test;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
37  import com.google.common.collect.Maps;
38  
39  public class MemoryManagerTest
40      extends AbstractBenchmark
41  {
42  
43      @BeforeClass
44      public static void init()
45      {
46          logger.info( "init" );
47          MemoryManager.init( 1, Ram.Mb( 100 ) );
48      }
49  
50      @AfterClass
51      public static void cleanup()
52          throws IOException
53      {
54          MemoryManager.close();
55      }
56  
57      @Test
58      public void smokeTest()
59      {
60          Random rnd = new Random();
61          int size = rnd.nextInt( 10 ) * (int) MemoryManager.capacity() / 100;
62          logger.info( "payload size=" + Ram.inKb( size ) );
63          Pointer<Object> p = MemoryManager.store( new byte[size] );
64          logger.info( "stored" );
65          assertNotNull( p );
66          // assertEquals(size,p.end);
67          assertEquals( size, p.getCapacity() );
68          assertEquals( size, MemoryManager.getMemoryManager().used() );
69          MemoryManager.free( p );
70          assertEquals( 0, MemoryManager.getMemoryManager().used() );
71          logger.info( "end" );
72      }
73  
74      byte[] payload = "012345678901234567890123456789012345678901234567890123456789".getBytes();
75  
76      @Test
77      public void fillupTest()
78      {
79          MemoryManager.clear();
80          logger.info( "payload size=" + Ram.inKb( payload.length ) );
81          long howMany = ( MemoryManager.capacity() / payload.length );
82          howMany = ( howMany * 90 ) / 100;
83  
84          for ( int i = 0; i < howMany; i++ )
85          {
86              Pointer<Object> p = MemoryManager.store( payload );
87              assertNotNull( p );
88          }
89  
90          logger.info( "" + howMany + " items stored" );
91      }
92  
93      @Test
94      public void readTest()
95      {
96          for ( Pointer<Object> ptr : MemoryManager.getMemoryManager().getPointers() )
97          {
98              if ( !ptr.isFree() )
99              {
100                 byte[] res = MemoryManager.retrieve( ptr );
101                 assertNotNull( res );
102                 assertEquals( new String( payload ), new String( res ) );
103             }
104         }
105     }
106 
107     private static Logger logger = LoggerFactory.getLogger( MallocTest.class );
108 
109     final static Map<String, Byte> test = Maps.newHashMap();
110 
111 }