HBASE-19450 Addendum Limit logging of chore execution time at INFO to once per 5...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / util / ExponentialMovingAverage.java
blobd36ca7a559413f36da08942190f37bd26b2ef86f
1 /*
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;
23 /**
24 * EMA is similar to {@link WeightedMovingAverage} in weighted, but the weighting factor decrease
25 * exponentially. It brings benefits that it is more sensitive, and can see the trends easily.
27 @InterfaceAudience.Private
28 public class ExponentialMovingAverage extends WindowMovingAverage {
29 private double alpha;
30 private double previousAverage;
31 private double currentAverage;
33 public ExponentialMovingAverage(String label) {
34 this(label, DEFAULT_SIZE);
37 public ExponentialMovingAverage(String label, double alpha) {
38 this(label, DEFAULT_SIZE, alpha);
41 public ExponentialMovingAverage(String label, int size) {
42 this(label, size, (double) 2 / (1 + size));
45 public ExponentialMovingAverage(String label, int size, double alpha) {
46 super(label, size);
47 this.previousAverage = -1.0;
48 this.currentAverage = 0.0;
49 this.alpha = alpha;
52 @Override
53 public void updateMostRecentTime(long elapsed) {
54 if (!enoughStatistics()) {
55 previousAverage = super.getAverageTime();
56 super.updateMostRecentTime(elapsed);
57 if (!enoughStatistics()) {
58 return;
61 // CurrentEMA = α * currentValue + (1 - α) * previousEMA =>
62 // CurrentEMA = (currentValue - previousEMA) * α + previousEMA
63 // This will reduce multiplication.
64 currentAverage = (elapsed - previousAverage) * alpha + previousAverage;
65 previousAverage = currentAverage;
68 @Override
69 public double getAverageTime() {
70 if (!enoughStatistics()) {
71 return super.getAverageTime();
73 return currentAverage;
76 double getPrevious() {
77 return previousAverage;