001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.io.hfile.CombinedBlockCache.CombinedCacheStats;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028
029@Category({SmallTests.class})
030public class TestCombinedBlockCache {
031
032  @ClassRule
033  public static final HBaseClassTestRule CLASS_RULE =
034      HBaseClassTestRule.forClass(TestCombinedBlockCache.class);
035
036  @Test
037  public void testCombinedCacheStats() {
038    CacheStats lruCacheStats = new CacheStats("lruCacheStats", 2);
039    CacheStats bucketCacheStats = new CacheStats("bucketCacheStats", 2);
040    CombinedCacheStats stats =
041        new CombinedCacheStats(lruCacheStats, bucketCacheStats);
042
043    double delta = 0.01;
044
045    // period 1:
046    // lru cache: 1 hit caching, 1 miss caching
047    // bucket cache: 2 hit non-caching,1 miss non-caching/primary,1 fail insert
048    lruCacheStats.hit(true, true, BlockType.DATA);
049    lruCacheStats.miss(true, false, BlockType.DATA);
050    bucketCacheStats.hit(false,true, BlockType.DATA);
051    bucketCacheStats.hit(false,true, BlockType.DATA);
052    bucketCacheStats.miss(false, true, BlockType.DATA);
053
054    assertEquals(5, stats.getRequestCount());
055    assertEquals(2, stats.getRequestCachingCount());
056    assertEquals(2, stats.getMissCount());
057    assertEquals(1, stats.getPrimaryMissCount());
058    assertEquals(1, stats.getMissCachingCount());
059    assertEquals(3, stats.getHitCount());
060    assertEquals(3, stats.getPrimaryHitCount());
061    assertEquals(1, stats.getHitCachingCount());
062    assertEquals(0.6, stats.getHitRatio(), delta);
063    assertEquals(0.5, stats.getHitCachingRatio(), delta);
064    assertEquals(0.4, stats.getMissRatio(), delta);
065    assertEquals(0.5, stats.getMissCachingRatio(), delta);
066
067
068    // lru cache: 2 evicted, 1 evict
069    // bucket cache: 1 evict
070    lruCacheStats.evicted(1000, true);
071    lruCacheStats.evicted(1000, false);
072    lruCacheStats.evict();
073    bucketCacheStats.evict();
074    assertEquals(2, stats.getEvictionCount());
075    assertEquals(2, stats.getEvictedCount());
076    assertEquals(1, stats.getPrimaryEvictedCount());
077    assertEquals(1.0, stats.evictedPerEviction(), delta);
078
079    // lru cache:  1 fail insert
080    lruCacheStats.failInsert();
081    assertEquals(1, stats.getFailedInserts());
082
083    // rollMetricsPeriod
084    stats.rollMetricsPeriod();
085    assertEquals(3, stats.getSumHitCountsPastNPeriods());
086    assertEquals(5, stats.getSumRequestCountsPastNPeriods());
087    assertEquals(1, stats.getSumHitCachingCountsPastNPeriods());
088    assertEquals(2, stats.getSumRequestCachingCountsPastNPeriods());
089    assertEquals(0.6, stats.getHitRatioPastNPeriods(), delta);
090    assertEquals(0.5, stats.getHitCachingRatioPastNPeriods(), delta);
091
092    // period 2:
093    // lru cache: 3 hit caching
094    lruCacheStats.hit(true, true, BlockType.DATA);
095    lruCacheStats.hit(true, true, BlockType.DATA);
096    lruCacheStats.hit(true, true, BlockType.DATA);
097    stats.rollMetricsPeriod();
098    assertEquals(6, stats.getSumHitCountsPastNPeriods());
099    assertEquals(8, stats.getSumRequestCountsPastNPeriods());
100    assertEquals(4, stats.getSumHitCachingCountsPastNPeriods());
101    assertEquals(5, stats.getSumRequestCachingCountsPastNPeriods());
102    assertEquals(0.75, stats.getHitRatioPastNPeriods(), delta);
103    assertEquals(0.8, stats.getHitCachingRatioPastNPeriods(), delta);
104  }
105}