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 java.util.concurrent.TimeUnit;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
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 TestCacheStats {
031
032  @ClassRule
033  public static final HBaseClassTestRule CLASS_RULE =
034    HBaseClassTestRule.forClass(TestCacheStats.class);
035
036  @Test
037  public void testPeriodicMetrics() throws Exception {
038    CacheStats cacheStats = new CacheStats("test", 5, 1, TimeUnit.SECONDS);
039    cacheStats.hit(false, false, BlockType.DATA);
040    cacheStats.hit(false, false, BlockType.DATA);
041    cacheStats.hit(false, false, BlockType.DATA);
042    cacheStats.miss(false, false, BlockType.DATA);
043    // first period should have a 75% hit, 25% miss
044    Thread.sleep(1001);
045    cacheStats.hit(false, false, BlockType.DATA);
046    cacheStats.hit(false, false, BlockType.DATA);
047    cacheStats.miss(false, false, BlockType.DATA);
048    cacheStats.miss(false, false, BlockType.DATA);
049    Thread.sleep(1001);
050    cacheStats.hit(false, false, BlockType.DATA);
051    cacheStats.miss(false, false, BlockType.DATA);
052    cacheStats.miss(false, false, BlockType.DATA);
053    cacheStats.miss(false, false, BlockType.DATA);
054    Thread.sleep(1001);
055    cacheStats.hit(false, false, BlockType.DATA);
056    cacheStats.hit(false, false, BlockType.DATA);
057    cacheStats.hit(false, false, BlockType.DATA);
058    cacheStats.hit(false, false, BlockType.DATA);
059    Thread.sleep(1001);
060    cacheStats.miss(false, false, BlockType.DATA);
061    cacheStats.miss(false, false, BlockType.DATA);
062    cacheStats.miss(false, false, BlockType.DATA);
063    cacheStats.miss(false, false, BlockType.DATA);
064    Thread.sleep(1001);
065    cacheStats.getMetricsRollerScheduler().shutdownNow();
066    long[] hitCounts = cacheStats.getHitCounts();
067    long[] requestCounts = cacheStats.getRequestCounts();
068    assertEquals(5, hitCounts.length);
069    assertEquals(5, requestCounts.length);
070    assertEquals(3, hitCounts[0]);
071    assertEquals(2, hitCounts[1]);
072    assertEquals(1, hitCounts[2]);
073    assertEquals(4, hitCounts[3]);
074    assertEquals(0, hitCounts[4]);
075    assertEquals(10, cacheStats.getHitCount());
076    assertEquals(0.5, cacheStats.getHitRatioPastNPeriods(), 0.01);
077  }
078}