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