001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.management.mbean;
018
019 /**
020 * Holds the loads averaged over 1min, 5min, and 15min.
021 */
022 public final class LoadTriplet {
023
024 // Exponents for EWMA: exp(-INTERVAL / WINDOW) (in seconds)
025 private static final double EXP_1 = Math.exp(-1 / (60.0 * 1.0));
026 private static final double EXP_5 = Math.exp(-1 / (60.0 * 5.0));
027 private static final double EXP_15 = Math.exp(-1 / (60.0 * 15.0));
028
029 private double load01 = Double.NaN;
030 private double load05 = Double.NaN;
031 private double load15 = Double.NaN;
032
033 /**
034 * Update the load statistics
035 *
036 * @param currentReading the current reading
037 */
038 public void update(int currentReading) {
039 load01 = updateLoad(currentReading, EXP_1, load01);
040 load05 = updateLoad(currentReading, EXP_5, load05);
041 load15 = updateLoad(currentReading, EXP_15, load15);
042 }
043
044 private double updateLoad(int reading, double exp, double recentLoad) {
045 return Double.isNaN(recentLoad) ? reading : reading + exp * (recentLoad - reading);
046 }
047
048 public double getLoad1() {
049 return load01;
050 }
051
052 public double getLoad5() {
053 return load05;
054 }
055
056 public double getLoad15() {
057 return load15;
058 }
059
060 @Override
061 public String toString() {
062 return String.format("%.2f, %.2f, %.2f", getLoad1(), getLoad5(), getLoad15());
063 }
064
065 }