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 com.carrotsearch.junitbenchmarks.AbstractBenchmark;
23  import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
24  import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
25  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
26  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
27  import com.carrotsearch.junitbenchmarks.annotation.LabelType;
28  import com.google.common.collect.MapMaker;
29  import org.apache.directmemory.measures.Ram;
30  import org.apache.directmemory.memory.Pointer;
31  import org.junit.AfterClass;
32  import org.junit.Before;
33  import org.junit.BeforeClass;
34  import org.junit.Ignore;
35  import org.junit.Test;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import java.util.Random;
40  import java.util.concurrent.ConcurrentMap;
41  import java.util.concurrent.atomic.AtomicInteger;
42  
43  @AxisRange( min = 0, max = 1 )
44  @BenchmarkMethodChart()
45  @BenchmarkHistoryChart( labelWith = LabelType.CUSTOM_KEY, maxRuns = 5 )
46  
47  @Ignore
48  public class ConcurrentTest
49      extends AbstractBenchmark
50  {
51  
52      private final static int entries = 100000;
53  
54      public static AtomicInteger count = new AtomicInteger();
55  
56      private static AtomicInteger got = new AtomicInteger();
57  
58      private static AtomicInteger missed = new AtomicInteger();
59  
60      private static AtomicInteger good = new AtomicInteger();
61  
62      private static AtomicInteger bad = new AtomicInteger();
63  
64      private static AtomicInteger read = new AtomicInteger();
65  
66      private static MemoryManagerService<Object> mem;
67  
68      public static ConcurrentMap<String, Pointer<Object>> map =
69          new MapMaker().concurrencyLevel( 4 ).initialCapacity( 100000 ).makeMap();
70  
71      @Before
72      public void initMMS()
73      {
74          mem = new MemoryManagerServiceImpl<Object>();
75          mem.init( 1, 512 * 1024 * 1024 );
76      }
77      
78      @BenchmarkOptions( benchmarkRounds = 100000, warmupRounds = 0, concurrency = 100 )
79      @Test
80      public void store()
81      {
82          final String key = "test-" + count.incrementAndGet();
83          map.put( key, mem.store( key.getBytes() ) );
84      }
85  
86      @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 100 )
87      @Test
88      public void retrieveCatchThemAll()
89      {
90          String key = "test-" + ( rndGen.nextInt( entries ) + 1 );
91          Pointer<Object> p = map.get( key );
92          read.incrementAndGet();
93          if ( p != null )
94          {
95              got.incrementAndGet();
96              byte[] payload = mem.retrieve( p );
97              if ( key.equals( new String( payload ) ) )
98              {
99                  good.incrementAndGet();
100             }
101             else
102             {
103                 bad.incrementAndGet();
104             }
105         }
106         else
107         {
108             logger.info( "did not find key " + key );
109             missed.incrementAndGet();
110         }
111     }
112 
113     @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 100 )
114     @Test
115     public void retrieveCatchHalfOfThem()
116     {
117         String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 );
118         Pointer<Object> p = map.get( key );
119         read.incrementAndGet();
120         if ( p != null )
121         {
122             got.incrementAndGet();
123             byte[] payload = mem.retrieve( p );
124             if ( key.equals( new String( payload ) ) )
125             {
126                 good.incrementAndGet();
127             }
128             else
129             {
130                 bad.incrementAndGet();
131             }
132         }
133         else
134         {
135             missed.incrementAndGet();
136         }
137     }
138 
139     private void put( String key )
140     {
141         map.put( key, mem.store( key.getBytes() ) );
142     }
143 
144     @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 10 )
145     @Test
146     public void write3Read7()
147     {
148         String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 );
149 
150         int what = rndGen.nextInt( 10 );
151 
152         switch ( what )
153         {
154             case 0:
155             case 1:
156             case 2:
157                 put( key );
158                 break;
159             default:
160                 get( key );
161                 break;
162 
163         }
164 
165     }
166 
167     @BenchmarkOptions( benchmarkRounds = 1000000, warmupRounds = 0, concurrency = 10 )
168     @Test
169     public void write1Read9()
170     {
171         String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 );
172 
173         int what = rndGen.nextInt( 10 );
174 
175         switch ( what )
176         {
177             case 0:
178                 put( key );
179                 break;
180             default:
181                 get( key );
182                 break;
183 
184         }
185 
186     }
187 
188     private void get( String key )
189     {
190         Pointer<Object> p = map.get( key );
191         read.incrementAndGet();
192         if ( p != null )
193         {
194             got.incrementAndGet();
195             byte[] payload = mem.retrieve( p );
196             if ( key.equals( new String( payload ) ) )
197             {
198                 good.incrementAndGet();
199             }
200             else
201             {
202                 bad.incrementAndGet();
203             }
204         }
205         else
206         {
207             missed.incrementAndGet();
208         }
209     }
210 
211     Random rndGen = new Random();
212 
213     private static Logger logger = LoggerFactory.getLogger( ConcurrentTest.class );
214 
215     @BeforeClass
216     @AfterClass
217     public static void dump()
218     {
219         logger.info( "off-heap allocated: " + Ram.inMb( mem.capacity() ) );
220         logger.info( "off-heap used:      " + Ram.inMb( mem.used() ) );
221         logger.info( "heap - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
222         logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
223         logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
224         logger.info( "************************************************" );
225         logger.info( "entries: " + entries );
226         logger.info( "inserted: " + map.size() );
227         logger.info( "reads: " + read );
228         logger.info( "count: " + count );
229         logger.info( "got: " + got );
230         logger.info( "missed: " + missed );
231         logger.info( "good: " + good );
232         logger.info( "bad: " + bad );
233         logger.info( "************************************************" );
234     }
235 
236 }
237 
238 
239