View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.io.hfile;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import org.apache.hadoop.hbase.io.hfile.CombinedBlockCache.CombinedCacheStats;
23  import org.apache.hadoop.hbase.testclassification.SmallTests;
24  import org.junit.Test;
25  import org.junit.experimental.categories.Category;
26  
27  @Category({SmallTests.class})
28  public class TestCombinedBlockCache {
29    @Test
30    public void testCombinedCacheStats() {
31      CacheStats lruCacheStats = new CacheStats("lruCacheStats", 2);
32      CacheStats bucketCacheStats = new CacheStats("bucketCacheStats", 2);
33      CombinedCacheStats stats =
34          new CombinedCacheStats(lruCacheStats, bucketCacheStats);
35      
36      double delta = 0.01;
37      
38      // period 1:
39      // lru cache: 1 hit caching, 1 miss caching
40      // bucket cache: 2 hit non-caching,1 miss non-caching/primary,1 fail insert
41      lruCacheStats.hit(true);
42      lruCacheStats.miss(true);
43      bucketCacheStats.hit(false);
44      bucketCacheStats.hit(false);
45      bucketCacheStats.miss(false);
46      
47      assertEquals(5, stats.getRequestCount());
48      assertEquals(2, stats.getRequestCachingCount());
49      assertEquals(2, stats.getMissCount());
50      assertEquals(1, stats.getMissCachingCount());
51      assertEquals(3, stats.getHitCount());
52      assertEquals(1, stats.getHitCachingCount());
53      assertEquals(0.6, stats.getHitRatio(), delta);
54      assertEquals(0.5, stats.getHitCachingRatio(), delta);
55      assertEquals(0.4, stats.getMissRatio(), delta);
56      assertEquals(0.5, stats.getMissCachingRatio(), delta);
57      
58      
59      // lru cache: 2 evicted, 1 evict
60      // bucket cache: 1 evict
61      lruCacheStats.evicted(1000);
62      lruCacheStats.evicted(1000);
63      lruCacheStats.evict();
64      bucketCacheStats.evict();
65      assertEquals(2, stats.getEvictionCount());
66      assertEquals(2, stats.getEvictedCount());
67      assertEquals(1.0, stats.evictedPerEviction(), delta);
68      
69      // rollMetricsPeriod
70      stats.rollMetricsPeriod();
71      assertEquals(3, stats.getSumHitCountsPastNPeriods());
72      assertEquals(5, stats.getSumRequestCountsPastNPeriods());
73      assertEquals(1, stats.getSumHitCachingCountsPastNPeriods());
74      assertEquals(2, stats.getSumRequestCachingCountsPastNPeriods());
75      assertEquals(0.6, stats.getHitRatioPastNPeriods(), delta);
76      assertEquals(0.5, stats.getHitCachingRatioPastNPeriods(), delta);
77      
78      // period 2:
79      // lru cache: 3 hit caching
80      lruCacheStats.hit(true);
81      lruCacheStats.hit(true);
82      lruCacheStats.hit(true);
83      stats.rollMetricsPeriod();
84      assertEquals(6, stats.getSumHitCountsPastNPeriods());
85      assertEquals(8, stats.getSumRequestCountsPastNPeriods());
86      assertEquals(4, stats.getSumHitCachingCountsPastNPeriods());
87      assertEquals(5, stats.getSumRequestCachingCountsPastNPeriods());
88      assertEquals(0.75, stats.getHitRatioPastNPeriods(), delta);
89      assertEquals(0.8, stats.getHitCachingRatioPastNPeriods(), delta);
90    }
91  }