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.yetus.audience.InterfaceAudience;
022
023/**
024 * EMA is similar to {@link WeightedMovingAverage} in weighted, but the weighting factor decrease
025 * exponentially. It brings benefits that it is more sensitive, and can see the trends easily.
026 */
027@InterfaceAudience.Private
028public class ExponentialMovingAverage extends WindowMovingAverage {
029  private double alpha;
030  private double previousAverage;
031  private double currentAverage;
032
033  public ExponentialMovingAverage(String label) {
034    this(label, DEFAULT_SIZE);
035  }
036
037  public ExponentialMovingAverage(String label, double alpha) {
038    this(label, DEFAULT_SIZE, alpha);
039  }
040
041  public ExponentialMovingAverage(String label, int size) {
042    this(label, size, (double) 2 / (1 + size));
043  }
044
045  public ExponentialMovingAverage(String label, int size, double alpha) {
046    super(label, size);
047    this.previousAverage = -1.0;
048    this.currentAverage = 0.0;
049    this.alpha = alpha;
050  }
051
052  @Override
053  public void updateMostRecentTime(long elapsed) {
054    if (!enoughStatistics()) {
055      previousAverage = super.getAverageTime();
056      super.updateMostRecentTime(elapsed);
057      if (!enoughStatistics()) {
058        return;
059      }
060    }
061    // CurrentEMA = α * currentValue + (1 - α) * previousEMA =>
062    // CurrentEMA = (currentValue - previousEMA) * α + previousEMA
063    // This will reduce multiplication.
064    currentAverage = (elapsed - previousAverage) * alpha + previousAverage;
065    previousAverage = currentAverage;
066  }
067
068  @Override
069  public double getAverageTime() {
070    if (!enoughStatistics()) {
071      return super.getAverageTime();
072    }
073    return currentAverage;
074  }
075
076  double getPrevious() {
077    return previousAverage;
078  }
079}