1   /**
2    * Copyright 2011 The Apache Software Foundation
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, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.io.hfile.slab;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.io.hfile.CacheTestUtils;
27  import org.apache.hadoop.hbase.io.hfile.slab.SlabCache.SlabStats;
28  import org.junit.After;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  /**
33   * Basic test of SlabCache. Puts and gets.
34   * <p>
35   *
36   * Tests will ensure that blocks that are uncached are identical to the ones
37   * being cached, and that the cache never exceeds its capacity. Note that its
38   * fine if the cache evicts before it reaches max capacity - Guava Mapmaker may
39   * choose to evict at any time.
40   *
41   */
42  public class TestSlabCache {
43    static final int CACHE_SIZE = 1000000;
44    static final int NUM_BLOCKS = 101;
45    static final int BLOCK_SIZE = CACHE_SIZE / NUM_BLOCKS;
46    static final int NUM_THREADS = 50;
47    static final int NUM_QUERIES = 10000;
48    SlabCache cache;
49  
50    @Before
51    public void setup() {
52      cache = new SlabCache(CACHE_SIZE + BLOCK_SIZE * 2, BLOCK_SIZE);
53      cache.addSlabByConf(new Configuration());
54    }
55  
56    @After
57    public void tearDown() {
58      cache.shutdown();
59    }
60  
61    @Test
62    public void testElementPlacement() {
63      assertEquals(cache.getHigherBlock(BLOCK_SIZE).getKey().intValue(),
64          (BLOCK_SIZE * 11 / 10));
65      assertEquals(cache.getHigherBlock((BLOCK_SIZE * 2)).getKey()
66          .intValue(), (BLOCK_SIZE * 21 / 10));
67    }
68  
69    @Test
70    public void testCacheSimple() throws Exception {
71      CacheTestUtils.testCacheSimple(cache, BLOCK_SIZE, NUM_QUERIES);
72    }
73  
74    @Test
75    public void testCacheMultiThreaded() throws Exception {
76      CacheTestUtils.testCacheMultiThreaded(cache, BLOCK_SIZE, NUM_THREADS,
77          NUM_QUERIES, 0.80);
78    }
79  
80    @Test
81    public void testCacheMultiThreadedSingleKey() throws Exception {
82      CacheTestUtils.hammerSingleKey(cache, BLOCK_SIZE, NUM_THREADS, NUM_QUERIES);
83    }
84  
85    @Test
86    public void testCacheMultiThreadedEviction() throws Exception {
87      CacheTestUtils.hammerEviction(cache, BLOCK_SIZE, 10, NUM_QUERIES);
88    }
89  
90    @Test
91    /*Just checks if ranges overlap*/
92    public void testStatsArithmetic(){
93      SlabStats test = cache.requestStats;
94      for(int i = 0; i < test.NUMDIVISIONS; i++){
95        assertTrue("Upper for index " + i + " is " + test.getUpperBound(i) +
96            " lower " + test.getLowerBound(i + 1),
97            test.getUpperBound(i) <= test.getLowerBound(i + 1));
98      }
99    }
100 
101   @Test
102   public void testHeapSizeChanges(){
103     CacheTestUtils.testHeapSizeChanges(cache, BLOCK_SIZE);
104   }
105 }