HBASE-19450 Addendum Limit logging of chore execution time at INFO to once per 5...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / util / MovingAverage.java
blob55a9766891de232cdaa3423a53f25e686b05c8e3
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.apache.hadoop.hbase.util;
21 import org.apache.yetus.audience.InterfaceAudience;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 /**
26 * The purpose of introduction of {@link MovingAverage} mainly is to measure execution time of a
27 * specific method, which can help us to know its performance fluctuation in response to different
28 * machine states or situations, better case, then to act accordingly.
29 * <br>
30 * In different situation, different {@link MovingAverage} algorithm can be used based on needs.
32 @InterfaceAudience.Private
33 public abstract class MovingAverage<T> {
34 private final static Logger LOG = LoggerFactory.getLogger(MovingAverage.class);
36 protected final String label;
38 protected MovingAverage(String label) {
39 this.label = label;
42 /**
43 * Mark start time of an execution.
44 * @return time in ns.
46 protected long start() {
47 return System.nanoTime();
50 /**
51 * Mark end time of an execution, and return its interval.
52 * @param startTime start time of an execution
53 * @return elapsed time
55 protected long stop(long startTime) {
56 return System.nanoTime() - startTime;
59 /**
60 * Measure elapsed time of a measurable method.
61 * @param measurable method implements {@link TimeMeasurable}
62 * @return T it refers to the original return type of the measurable method
64 public T measure(TimeMeasurable<T> measurable) {
65 long startTime = start();
66 LOG.trace("{} - start to measure at: {} ns.", label, startTime);
67 // Here may throw exceptions which should be taken care by caller, not here.
68 // If exception occurs, this time wouldn't count.
69 T result = measurable.measure();
70 long elapsed = stop(startTime);
71 LOG.trace("{} - elapse: {} ns.", label, elapsed);
72 updateMostRecentTime(elapsed);
73 return result;
76 /**
77 * Update the most recent data.
78 * @param elapsed elapsed time of the most recent measurement
80 protected abstract void updateMostRecentTime(long elapsed);
82 /**
83 * Get average execution time of the measured method.
84 * @return average time in ns
86 public abstract double getAverageTime();