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 org.apache.directmemory.measures.Ram;
23  import org.apache.directmemory.memory.allocator.Allocator;
24  import org.apache.directmemory.memory.allocator.MergingByteBufferAllocator;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import static org.junit.Assert.assertNotNull;
29  
30  public class Starter
31  {
32  
33      private static Logger logger = LoggerFactory.getLogger( MallocTest.class );
34  
35      /**
36       * @param args
37       */
38      public static void main( String[] args )
39      {
40  
41          if ( args.length < 3 )
42          {
43              System.out.println( "DirectMemory (for real testers only!) - usage:" );
44              System.out.println(
45                  "	java -XX:MaxDirectMemorySize=XXXXm -XmxXXXXm -XmsXXXXm -jar dm-test.jar <buffers> <Mb for each buffer> <entries>" );
46              return;
47          }
48  
49          int buffers = new Integer( args[0] );
50          int mb = new Integer( args[1] );
51          int entries = new Integer( args[2] );
52  
53          logger.info( "buffers: " + buffers );
54          logger.info( "mb: " + mb );
55          logger.info( "entries: " + entries );
56  
57          Starter starter = new Starter();
58          starter.rawInsertMultipleBuffers( buffers, mb, entries );
59      }
60  
61      
62      private static void dump( MemoryManagerService<Object> mms )
63      {
64          logger.info( "off-heap - allocated: " + Ram.inMb( mms.capacity() ) );
65          logger.info( "off-heap - used:      " + Ram.inMb( mms.used() ) );
66          logger.info( "heap    - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
67          logger.info( "heap     - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
68          logger.info( "heap     - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
69          logger.info( "************************************************" );
70      }
71      
72      public void dump( Allocator mem )
73      {
74          logger.info( "off-heap - buffer: " + mem.getNumber() );
75          logger.info( "off-heap - allocated: " + Ram.inMb( mem.getCapacity() ) );
76          logger.info( "heap    - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
77          logger.info( "heap     - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
78          logger.info( "heap     - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
79          logger.info( "************************************************" );
80      }
81      
82      public void rawInsert( int megabytes, int howMany )
83      {
84          Allocator allocator = new MergingByteBufferAllocator( 1, megabytes * 1024 * 1024 );
85          assertNotNull( allocator );
86          int size = allocator.getCapacity() / ( howMany );
87          size -= size / 100 * 1;
88          logger.info( "payload size=" + size );
89          logger.info( "entries=" + howMany );
90  
91          logger.info( "starting..." );
92  
93          long start = System.currentTimeMillis();
94  
95          for ( int i = 0; i < howMany; i++ )
96          {
97              allocator.allocate( size );
98          }
99  
100         logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
101         logger.info( "---------------------------------" );
102         dump( allocator );
103     }
104 
105 
106     public void rawInsertMultipleBuffers( int buffers, int megabytes, int howMany )
107     {
108         MemoryManager.init( buffers, Ram.Mb( megabytes ) );
109         int size = (int) ( MemoryManager.capacity() / ( howMany ) );
110         size -= size / 100 * 1;
111         logger.info( "payload size=" + size );
112         logger.info( "entries=" + howMany );
113 
114         logger.info( "starting..." );
115 
116         long start = System.currentTimeMillis();
117 
118         byte[] payload = new byte[size];
119         for ( int i = 0; i < howMany; i++ )
120         {
121             MemoryManager.store( payload );
122         }
123 
124         logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
125         logger.info( "---------------------------------" );
126 
127         dump( MemoryManager.getMemoryManager() );
128     }
129 
130 
131 }