View Javadoc

1   package org.apache.directmemory.preliminary;
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 java.nio.ByteBuffer;
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentMap;
25  
26  import org.apache.directmemory.measures.Ram;
27  import org.junit.Before;
28  import org.junit.Ignore;
29  import org.junit.Test;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
34  import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
35  import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
36  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
37  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
38  import com.carrotsearch.junitbenchmarks.annotation.LabelType;
39  import com.google.common.collect.MapMaker;
40  import com.google.common.collect.Maps;
41  
42  @AxisRange( min = 0, max = 1 )
43  @BenchmarkMethodChart( filePrefix = "latest-microbench" )
44  @BenchmarkOptions( benchmarkRounds = 1, warmupRounds = 0 )
45  @BenchmarkHistoryChart( labelWith = LabelType.CUSTOM_KEY, maxRuns = 5 )
46  @Ignore
47  public class MicroBenchmark
48      extends AbstractBenchmark
49  {
50  
51      private static Logger logger = LoggerFactory.getLogger( MicroBenchmark.class );
52  
53      private final int many = 3000000;
54  
55      private final int less = 300000;
56  
57      @Before
58      public void cleanup()
59      {
60          dump( "Before cleanup" );
61          // Runtime.getRuntime().gc();
62          // dump("After cleanup");
63          logger.info( "************************************************" );
64      }
65  
66      private void dump( String message )
67      {
68          logger.info( message );
69          logger.info( "Memory - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
70          logger.info( "Memory - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
71          logger.info( "Memory - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
72      }
73  
74      @Test
75      public void manySmallInHeapWithHashmap()
76      {
77          final Map<String, byte[]> test = Maps.newHashMap();
78          final byte payload[] = new byte[450];
79          long ops = many;
80          for ( int i = 0; i < ops; i++ )
81          {
82              final String key = "test-" + i;
83              test.put( key, payload.clone() );
84          }
85          logger.info( "many=" + ops );
86          logger.info( "payload.length=" + payload.length );
87          logger.info( "stored " + Ram.inMb( payload.length * ops ) );
88      }
89  
90      @Test
91      public void manySmallInHeapWithMapMaker()
92      {
93          final byte payload[] = new byte[450];
94          int ops = many;
95  
96          logger.info( "many=" + ops );
97          logger.info( "payload.length=" + payload.length );
98          pumpTheHeap( ops, payload );
99  
100     }
101 
102     @Test
103     public void manySmallOffHeap()
104     {
105 
106         final byte payload[] = new byte[450];
107         int ops = many;
108 
109         logger.info( "many=" + ops );
110         logger.info( "payload.length=" + payload.length );
111         pumpOffHeap( ops, payload );
112 
113     }
114 
115     @Test
116     public void lessButLargerOffHeap()
117     {
118 
119         final byte payload[] = new byte[5120];
120         int ops = less;
121 
122         logger.info( "less=" + ops );
123         logger.info( "payload.length=" + payload.length );
124         pumpOffHeap( ops, payload );
125 
126     }
127 
128     @Test
129     public void lessButLargerInHeap()
130     {
131 
132         final byte payload[] = new byte[5120];
133         int ops = less;
134 
135         logger.info( "less=" + ops );
136         logger.info( "payload.length=" + payload.length );
137         pumpTheHeap( ops, payload );
138 
139     }
140 
141     /*
142      * ExecutorService executor = Executors.newCachedThreadPool(); Callable<Object> task = new Callable<Object>() {
143      * public Object call() { return something.blockingMethod(); } } Future<Object> future = executor.submit(task); try
144      * { Object result = future.get(5, TimeUnit.SECONDS); } catch (TimeoutException ex) { // handle the timeout }
145      * finally { future.cancel(); // may or may not desire this }
146      */
147 
148     private void pumpOffHeap( int ops, byte[] payload )
149     {
150 
151         ConcurrentMap<String, ByteBuffer> test = new MapMaker().concurrencyLevel( 4 ).makeMap();
152 
153         logger.info( Ram.inMb( ops * payload.length ) + " in " + ops + " slices to store" );
154 
155         ByteBuffer bulk = ByteBuffer.allocateDirect( ops * payload.length );
156 
157         double started = System.currentTimeMillis();
158 
159         for ( int i = 0; i < ops; i++ )
160         {
161             bulk.position( i * payload.length );
162             final ByteBuffer buf = bulk.duplicate();
163             buf.put( payload );
164             test.put( "test-" + i, buf );
165         }
166 
167         double finished = System.currentTimeMillis();
168 
169         logger.info( "done in " + ( finished - started ) / 1000 + " seconds" );
170 
171         for ( ByteBuffer buf : test.values() )
172         {
173             buf.clear();
174         }
175     }
176 
177     private void pumpTheHeap( int ops, byte[] payload )
178     {
179 
180         ConcurrentMap<String, byte[]> test = new MapMaker().concurrencyLevel( 4 ).makeMap();
181 
182         logger.info( Ram.inMb( ops * payload.length ) + " in " + ops + " slices to store" );
183 
184         double started = System.currentTimeMillis();
185 
186         for ( int i = 0; i < ops; i++ )
187         {
188             test.put( "test-" + i, payload.clone() );
189         }
190 
191         double finished = System.currentTimeMillis();
192 
193         logger.info( "done in " + ( finished - started ) / 1000 + " seconds" );
194     }
195 
196 }