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      readRequestCountPerSecond = (readRequestCount - previousReadRequestCount) /
045        ((lastReportTimestamp - previousLastReportTimestamp) / 1000);
046      filteredReadRequestCountPerSecond =
047        (filteredReadRequestCount - previousFilteredReadRequestCount) /
048        ((lastReportTimestamp - previousLastReportTimestamp) / 1000);
049      writeRequestCountPerSecond = (writeRequestCount - previousWriteRequestCount) /
050        ((lastReportTimestamp - previousLastReportTimestamp) / 1000);
051
052      previousLastReportTimestamp = lastReportTimestamp;
053      previousReadRequestCount = readRequestCount;
054      previousFilteredReadRequestCount = filteredReadRequestCount;
055      previousWriteRequestCount = writeRequestCount;
056    }
057  }
058
059  public long getReadRequestCountPerSecond() {
060    return readRequestCountPerSecond < 0 ? 0 : readRequestCountPerSecond;
061  }
062
063  public long getFilteredReadRequestCountPerSecond() {
064    return filteredReadRequestCountPerSecond < 0 ? 0 : filteredReadRequestCountPerSecond;
065  }
066
067  public long getWriteRequestCountPerSecond() {
068    return writeRequestCountPerSecond < 0 ? 0 : writeRequestCountPerSecond;
069  }
070
071  public long getRequestCountPerSecond() {
072    return getReadRequestCountPerSecond() + getWriteRequestCountPerSecond();
073  }
074}