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