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.hbtop.mode;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022
023/**
024 * Utility class for calculating request counts per second.
025 */
026@InterfaceAudience.Private
027public class RequestCountPerSecond {
028  private long previousLastReportTimestamp;
029  private long previousReadRequestCount;
030  private long previousFilteredReadRequestCount;
031  private long previousWriteRequestCount;
032  private long readRequestCountPerSecond;
033  private long filteredReadRequestCountPerSecond;
034  private long writeRequestCountPerSecond;
035
036  public void refresh(long lastReportTimestamp, long readRequestCount,
037    long filteredReadRequestCount, long writeRequestCount) {
038    if (previousLastReportTimestamp == 0) {
039      previousLastReportTimestamp = lastReportTimestamp;
040      previousReadRequestCount = readRequestCount;
041      previousFilteredReadRequestCount = filteredReadRequestCount;
042      previousWriteRequestCount = writeRequestCount;
043    } else if (previousLastReportTimestamp != lastReportTimestamp) {
044      long delta = (lastReportTimestamp - previousLastReportTimestamp) / 1000;
045      if (delta < 1) {
046        delta = 1;
047      }
048      readRequestCountPerSecond = (readRequestCount - previousReadRequestCount) / delta;
049      filteredReadRequestCountPerSecond =
050        (filteredReadRequestCount - previousFilteredReadRequestCount) / delta;
051      writeRequestCountPerSecond = (writeRequestCount - previousWriteRequestCount) / delta;
052
053      previousLastReportTimestamp = lastReportTimestamp;
054      previousReadRequestCount = readRequestCount;
055      previousFilteredReadRequestCount = filteredReadRequestCount;
056      previousWriteRequestCount = writeRequestCount;
057    }
058  }
059
060  public long getReadRequestCountPerSecond() {
061    return readRequestCountPerSecond < 0 ? 0 : readRequestCountPerSecond;
062  }
063
064  public long getFilteredReadRequestCountPerSecond() {
065    return filteredReadRequestCountPerSecond < 0 ? 0 : filteredReadRequestCountPerSecond;
066  }
067
068  public long getWriteRequestCountPerSecond() {
069    return writeRequestCountPerSecond < 0 ? 0 : writeRequestCountPerSecond;
070  }
071
072  public long getRequestCountPerSecond() {
073    return getReadRequestCountPerSecond() + getWriteRequestCountPerSecond();
074  }
075}