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