2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org
.apache
.hadoop
.hbase
.util
;
21 import org
.apache
.yetus
.audience
.InterfaceAudience
;
24 * Different from SMA {@link SimpleMovingAverage}, WeightedMovingAverage gives each data different
25 * weight. And it is based on {@link WindowMovingAverage}, such that it only focus on the last N.
27 @InterfaceAudience.Private
28 public class WeightedMovingAverage
extends WindowMovingAverage
{
29 private int[] coefficient
;
30 private int denominator
;
32 public WeightedMovingAverage(String label
) {
33 this(label
, DEFAULT_SIZE
);
36 public WeightedMovingAverage(String label
, int size
) {
38 int length
= getNumberOfStatistics();
39 denominator
= length
* (length
+ 1) / 2;
40 coefficient
= new int[length
];
41 // E.g. default size is 5, coefficient should be [1, 2, 3, 4, 5]
42 for (int i
= 0; i
< length
; i
++) {
43 coefficient
[i
] = i
+ 1;
48 public double getAverageTime() {
49 if (!enoughStatistics()) {
50 return super.getAverageTime();
52 // only we get enough statistics, then start WMA.
55 int length
= getNumberOfStatistics();
56 // tmIndex, it points to the oldest data.
57 for (int tmIndex
= (getMostRecentPosistion() + 1) % length
;
59 coIndex
++, tmIndex
= (++tmIndex
) % length
) {
60 // start the multiplication from oldest to newest
61 average
+= coefficient
[coIndex
] * getStatisticsAtIndex(tmIndex
);
63 return average
/ denominator
;