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.util;
019
020import com.codahale.metrics.Histogram;
021import com.codahale.metrics.Reservoir;
022import com.codahale.metrics.Snapshot;
023import java.lang.reflect.Constructor;
024import java.text.DecimalFormat;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/** Utility functions for working with Yammer Metrics. */
028@InterfaceAudience.Private
029public final class YammerHistogramUtils {
030
031  // not for public consumption
032  private YammerHistogramUtils() {
033  }
034
035  /**
036   * Used formatting doubles so only two places after decimal point.
037   */
038  private static DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#0.00");
039
040  /**
041   * Create a new {@link com.codahale.metrics.Histogram} instance. These constructors are not public
042   * in 2.2.0, so we use reflection to find them.
043   */
044  public static Histogram newHistogram(Reservoir sample) {
045    try {
046      Constructor<?> ctor = Histogram.class.getDeclaredConstructor(Reservoir.class);
047      ctor.setAccessible(true);
048      return (Histogram) ctor.newInstance(sample);
049    } catch (Exception e) {
050      throw new RuntimeException(e);
051    }
052  }
053
054  /** @return an abbreviated summary of {@code hist}. */
055  public static String getShortHistogramReport(final Histogram hist) {
056    Snapshot sn = hist.getSnapshot();
057    return "mean=" + DOUBLE_FORMAT.format(sn.getMean()) + ", min="
058      + DOUBLE_FORMAT.format(sn.getMin()) + ", max=" + DOUBLE_FORMAT.format(sn.getMax())
059      + ", stdDev=" + DOUBLE_FORMAT.format(sn.getStdDev()) + ", 95th="
060      + DOUBLE_FORMAT.format(sn.get95thPercentile()) + ", 99th="
061      + DOUBLE_FORMAT.format(sn.get99thPercentile());
062  }
063
064  /** @return a summary of {@code hist}. */
065  public static String getHistogramReport(final Histogram hist) {
066    Snapshot sn = hist.getSnapshot();
067    return "mean=" + DOUBLE_FORMAT.format(sn.getMean()) + ", min="
068      + DOUBLE_FORMAT.format(sn.getMin()) + ", max=" + DOUBLE_FORMAT.format(sn.getMax())
069      + ", stdDev=" + DOUBLE_FORMAT.format(sn.getStdDev()) + ", 50th="
070      + DOUBLE_FORMAT.format(sn.getMedian()) + ", 75th="
071      + DOUBLE_FORMAT.format(sn.get75thPercentile()) + ", 95th="
072      + DOUBLE_FORMAT.format(sn.get95thPercentile()) + ", 99th="
073      + DOUBLE_FORMAT.format(sn.get99thPercentile()) + ", 99.9th="
074      + DOUBLE_FORMAT.format(sn.get999thPercentile()) + ", 99.99th="
075      + DOUBLE_FORMAT.format(sn.getValue(0.9999)) + ", 99.999th="
076      + DOUBLE_FORMAT.format(sn.getValue(0.99999));
077  }
078
079  /** @return pretty summary of {@code hist}. */
080  public static String getPrettyHistogramReport(final Histogram h) {
081    Snapshot sn = h.getSnapshot();
082    return "Mean      = " + DOUBLE_FORMAT.format(sn.getMean()) + "\n" + "Min       = "
083      + DOUBLE_FORMAT.format(sn.getMin()) + "\n" + "Max       = "
084      + DOUBLE_FORMAT.format(sn.getMax()) + "\n" + "StdDev    = "
085      + DOUBLE_FORMAT.format(sn.getStdDev()) + "\n" + "50th      = "
086      + DOUBLE_FORMAT.format(sn.getMedian()) + "\n" + "75th      = "
087      + DOUBLE_FORMAT.format(sn.get75thPercentile()) + "\n" + "95th      = "
088      + DOUBLE_FORMAT.format(sn.get95thPercentile()) + "\n" + "99th      = "
089      + DOUBLE_FORMAT.format(sn.get99thPercentile()) + "\n" + "99.9th    = "
090      + DOUBLE_FORMAT.format(sn.get999thPercentile()) + "\n" + "99.99th   = "
091      + DOUBLE_FORMAT.format(sn.getValue(0.9999)) + "\n" + "99.999th  = "
092      + DOUBLE_FORMAT.format(sn.getValue(0.99999));
093  }
094}