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 */ 017package org.apache.camel.management.mbean; 018 019import org.apache.camel.util.StopWatch; 020 021/** 022 * Holds the load throughput messages/second 023 */ 024public final class LoadThroughput { 025 026 private final StopWatch watch = new StopWatch(false); 027 private long last; 028 private double thp; 029 030 /** 031 * Update the load statistics 032 * 033 * @param currentReading the current reading 034 */ 035 public void update(long currentReading) { 036 if (!watch.isStarted()) { 037 watch.restart(); 038 thp = 0; 039 } else { 040 long time = watch.takenAndRestart(); 041 if (time > 0) { 042 long delta = currentReading - last; 043 if (delta > 0) { 044 // need to calculate with fractions 045 thp = (1000d / time) * delta; 046 } else { 047 thp = 0; 048 } 049 } else { 050 thp = 0; 051 } 052 } 053 last = currentReading; 054 } 055 056 public double getThroughput() { 057 return thp; 058 } 059 060 public void reset() { 061 last = 0; 062 thp = 0; 063 } 064 065 @Override 066 public String toString() { 067 return "" + thp; 068 } 069 070}