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.util;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.hbase.testclassification.MiscTests;
023import org.apache.hadoop.hbase.testclassification.SmallTests;
024import org.junit.jupiter.api.Tag;
025import org.junit.jupiter.api.Test;
026import org.junit.jupiter.api.TestInfo;
027
028@Tag(MiscTests.TAG)
029@Tag(SmallTests.TAG)
030public class TestMovingAverage {
031
032  private long[] data = { 1, 12, 13, 24, 25, 26, 37, 38, 39, 40 };
033  private double delta = 0.1;
034
035  @Test
036  public void testSimpleMovingAverage(TestInfo testInfo) throws Exception {
037    MovingAverage<?> algorithm = new SimpleMovingAverage(testInfo.getTestMethod().get().getName());
038    int index = 0;
039    // [1, 12, 13, 24]
040    int bound = 4;
041    for (; index < bound; index++) {
042      algorithm.updateMostRecentTime(data[index]);
043    }
044    assertEquals(12.5, algorithm.getAverageTime(), delta);
045    // [1, 12, 13, 24, 25]
046    bound = 5;
047    for (; index < bound; index++) {
048      algorithm.updateMostRecentTime(data[index]);
049    }
050    assertEquals(15.0, algorithm.getAverageTime(), delta);
051    // [1, 12, 13, 24, 25, 26, 37, 38]
052    bound = 8;
053    for (; index < bound; index++) {
054      algorithm.updateMostRecentTime(data[index]);
055    }
056    assertEquals(22.0, algorithm.getAverageTime(), delta);
057    // [1, 12, 13, 24, 25, 26, 37, 38, 39, 40]
058    for (; index < data.length; index++) {
059      algorithm.updateMostRecentTime(data[index]);
060    }
061    assertEquals(25.5, algorithm.getAverageTime(), delta);
062  }
063
064  @Test
065  public void testWindowMovingAverage(TestInfo testInfo) throws Exception {
066    // Default size is 5.
067    MovingAverage<?> algorithm = new WindowMovingAverage(testInfo.getTestMethod().get().getName());
068    int index = 0;
069    // [1, 12, 13, 24]
070    int bound = 4;
071    for (; index < bound; index++) {
072      algorithm.updateMostRecentTime(data[index]);
073    }
074    assertEquals(12.5, algorithm.getAverageTime(), delta);
075    // [1, 12, 13, 24, 25]
076    bound = 5;
077    for (; index < bound; index++) {
078      algorithm.updateMostRecentTime(data[index]);
079    }
080    assertEquals(15.0, algorithm.getAverageTime(), delta);
081    // [24, 25, 26, 37, 38]
082    bound = 8;
083    for (; index < bound; index++) {
084      algorithm.updateMostRecentTime(data[index]);
085    }
086    assertEquals(30.0, algorithm.getAverageTime(), delta);
087    // [26, 37, 38, 39, 40]
088    for (; index < data.length; index++) {
089      algorithm.updateMostRecentTime(data[index]);
090    }
091    assertEquals(36.0, algorithm.getAverageTime(), delta);
092  }
093
094  @Test
095  public void testWeightedMovingAverage(TestInfo testInfo) throws Exception {
096    // Default size is 5.
097    MovingAverage<?> algorithm =
098      new WeightedMovingAverage(testInfo.getTestMethod().get().getName());
099    int index = 0;
100    // [1, 12, 13, 24]
101    int bound = 4;
102    for (; index < bound; index++) {
103      algorithm.updateMostRecentTime(data[index]);
104    }
105    assertEquals(12.5, algorithm.getAverageTime(), delta);
106    // [1, 12, 13, 24, 25]
107    bound = 5;
108    for (; index < bound; index++) {
109      algorithm.updateMostRecentTime(data[index]);
110    }
111    assertEquals(15.0, algorithm.getAverageTime(), delta);
112    // [24, 25, 26, 37, 38]
113    bound = 8;
114    for (; index < bound; index++) {
115      algorithm.updateMostRecentTime(data[index]);
116    }
117    assertEquals(32.67, algorithm.getAverageTime(), delta);
118    // [26, 37, 38, 39, 40]
119    for (; index < data.length; index++) {
120      algorithm.updateMostRecentTime(data[index]);
121    }
122    assertEquals(38.0, algorithm.getAverageTime(), delta);
123  }
124
125  @Test
126  public void testExponentialMovingAverage(TestInfo testInfo) throws Exception {
127    // [1, 12, 13, 24, 25, 26, 37, 38, 39, 40]
128    MovingAverage<?> algorithm =
129      new ExponentialMovingAverage(testInfo.getTestMethod().get().getName());
130    int index = 0;
131    int bound = 5;
132    for (; index < bound; index++) {
133      algorithm.updateMostRecentTime(data[index]);
134    }
135    assertEquals(15.0, algorithm.getAverageTime(), delta);
136    bound = 6;
137    for (; index < bound; index++) {
138      algorithm.updateMostRecentTime(data[index]);
139    }
140    assertEquals(18.67, algorithm.getAverageTime(), delta);
141    bound = 8;
142    for (; index < bound; index++) {
143      algorithm.updateMostRecentTime(data[index]);
144    }
145    assertEquals(29.16, algorithm.getAverageTime(), delta);
146    for (; index < data.length; index++) {
147      algorithm.updateMostRecentTime(data[index]);
148    }
149    assertEquals(34.97, algorithm.getAverageTime(), delta);
150  }
151}