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 * Utility class for calculating request counts per second.
024 */
025@InterfaceAudience.Private
026public class RequestCountPerSecond {
027  private long previousLastReportTimestamp;
028  private long previousReadRequestCount;
029  private long previousFilteredReadRequestCount;
030  private long previousWriteRequestCount;
031  private long readRequestCountPerSecond;
032  private long filteredReadRequestCountPerSecond;
033  private long writeRequestCountPerSecond;
034
035  public void refresh(long lastReportTimestamp, long readRequestCount,
036    long filteredReadRequestCount, long writeRequestCount) {
037    if (previousLastReportTimestamp == 0) {
038      previousLastReportTimestamp = lastReportTimestamp;
039      previousReadRequestCount = readRequestCount;
040      previousFilteredReadRequestCount = filteredReadRequestCount;
041      previousWriteRequestCount = writeRequestCount;
042    } else if (previousLastReportTimestamp != lastReportTimestamp) {
043      long delta = (lastReportTimestamp - previousLastReportTimestamp) / 1000;
044      if (delta < 1) {
045        delta = 1;
046      }
047      readRequestCountPerSecond = (readRequestCount - previousReadRequestCount) / delta;
048      filteredReadRequestCountPerSecond =
049        (filteredReadRequestCount - previousFilteredReadRequestCount) / delta;
050      writeRequestCountPerSecond = (writeRequestCount - previousWriteRequestCount) / delta;
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}