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