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.bucket;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import org.apache.hadoop.hbase.testclassification.IOTests;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
026import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
027import org.junit.jupiter.api.AfterEach;
028import org.junit.jupiter.api.BeforeEach;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(IOTests.TAG)
033@Tag(SmallTests.TAG)
034public class TestBucketCacheStats {
035
036  private ManualEnvironmentEdge edge;
037  private BucketCacheStats stats;
038
039  @BeforeEach
040  public void setUp() {
041    edge = new ManualEnvironmentEdge();
042    edge.setValue(1_000_000L);
043    EnvironmentEdgeManager.injectEdge(edge);
044    // A single period window does not start the metrics roller thread.
045    stats = new BucketCacheStats(1, 0);
046  }
047
048  @AfterEach
049  public void tearDown() {
050    EnvironmentEdgeManager.reset();
051  }
052
053  private void ioHits(int count) {
054    for (int i = 0; i < count; i++) {
055      stats.ioHit(1000);
056    }
057  }
058
059  @Test
060  public void testIOHitsPerSecondHasNoSideEffect() {
061    ioHits(600);
062    edge.incValue(60_000);
063    assertEquals(10, stats.getIOHitsPerSecond());
064    // Repeated reads, e.g. the RegionServer UI and the stats log, must see the same rate.
065    assertEquals(10, stats.getIOHitsPerSecond());
066    assertTrue(stats.toString().contains("ioHitsPerSecond=10"));
067    assertEquals(10, stats.getIOHitsPerSecond());
068
069    edge.incValue(60_000);
070    assertEquals(5, stats.getIOHitsPerSecond());
071  }
072
073  @Test
074  public void testIOHitsPerSecondWindowRestartsOnReset() {
075    ioHits(600);
076    edge.incValue(60_000);
077    assertEquals(10, stats.getIOHitsPerSecond());
078
079    stats.reset();
080    assertEquals(0, stats.getIOHitsPerSecond());
081
082    ioHits(300);
083    edge.incValue(10_000);
084    assertEquals(30, stats.getIOHitsPerSecond());
085  }
086
087  @Test
088  public void testIOHitsPerSecondWithSubSecondElapsedTime() {
089    ioHits(10);
090    assertEquals(0, stats.getIOHitsPerSecond());
091
092    edge.incValue(500);
093    assertEquals(20, stats.getIOHitsPerSecond());
094
095    edge.incValue(1_000);
096    assertEquals(6, stats.getIOHitsPerSecond());
097  }
098}