001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.util;
020
021import com.codahale.metrics.Histogram;
022import com.codahale.metrics.Reservoir;
023import com.codahale.metrics.Snapshot;
024import java.lang.reflect.Constructor;
025import java.text.DecimalFormat;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/** Utility functions for working with Yammer Metrics. */
029@InterfaceAudience.Private
030public final class YammerHistogramUtils {
031
032  // not for public consumption
033  private YammerHistogramUtils() {}
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
042   * not public in 2.2.0, so we use reflection to find them.
043   */
044  public static Histogram newHistogram(Reservoir sample) {
045    try {
046      Constructor<?> ctor =
047          Histogram.class.getDeclaredConstructor(Reservoir.class);
048      ctor.setAccessible(true);
049      return (Histogram) ctor.newInstance(sample);
050    } catch (Exception e) {
051      throw new RuntimeException(e);
052    }
053  }
054
055  /** @return an abbreviated summary of {@code hist}. */
056  public static String getShortHistogramReport(final Histogram hist) {
057    Snapshot sn = hist.getSnapshot();
058    return "mean=" + DOUBLE_FORMAT.format(sn.getMean()) +
059        ", min=" + DOUBLE_FORMAT.format(sn.getMin()) +
060        ", max=" + DOUBLE_FORMAT.format(sn.getMax()) +
061        ", stdDev=" + DOUBLE_FORMAT.format(sn.getStdDev()) +
062        ", 95th=" + DOUBLE_FORMAT.format(sn.get95thPercentile()) +
063        ", 99th=" + DOUBLE_FORMAT.format(sn.get99thPercentile());
064  }
065
066  /** @return a summary of {@code hist}. */
067  public static String getHistogramReport(final Histogram hist) {
068    Snapshot sn = hist.getSnapshot();
069    return "mean=" + DOUBLE_FORMAT.format(sn.getMean()) +
070        ", min=" + DOUBLE_FORMAT.format(sn.getMin()) +
071        ", max=" + DOUBLE_FORMAT.format(sn.getMax()) +
072        ", stdDev=" + DOUBLE_FORMAT.format(sn.getStdDev()) +
073        ", 50th=" + DOUBLE_FORMAT.format(sn.getMedian()) +
074        ", 75th=" + DOUBLE_FORMAT.format(sn.get75thPercentile()) +
075        ", 95th=" + DOUBLE_FORMAT.format(sn.get95thPercentile()) +
076        ", 99th=" + DOUBLE_FORMAT.format(sn.get99thPercentile()) +
077        ", 99.9th=" + DOUBLE_FORMAT.format(sn.get999thPercentile()) +
078        ", 99.99th=" + DOUBLE_FORMAT.format(sn.getValue(0.9999)) +
079        ", 99.999th=" + DOUBLE_FORMAT.format(sn.getValue(0.99999));
080  }
081
082  /** @return pretty summary of {@code hist}. */
083  public static String getPrettyHistogramReport(final Histogram h) {
084    Snapshot sn = h.getSnapshot();
085    return
086        "Mean      = " + DOUBLE_FORMAT.format(sn.getMean()) + "\n" +
087        "Min       = " + DOUBLE_FORMAT.format(sn.getMin()) + "\n" +
088        "Max       = " + DOUBLE_FORMAT.format(sn.getMax()) + "\n" +
089        "StdDev    = " + DOUBLE_FORMAT.format(sn.getStdDev()) + "\n" +
090        "50th      = " + DOUBLE_FORMAT.format(sn.getMedian()) + "\n" +
091        "75th      = " + DOUBLE_FORMAT.format(sn.get75thPercentile()) + "\n" +
092        "95th      = " + DOUBLE_FORMAT.format(sn.get95thPercentile()) + "\n" +
093        "99th      = " + DOUBLE_FORMAT.format(sn.get99thPercentile()) + "\n" +
094        "99.9th    = " + DOUBLE_FORMAT.format(sn.get999thPercentile()) + "\n" +
095        "99.99th   = " + DOUBLE_FORMAT.format(sn.getValue(0.9999)) + "\n" +
096        "99.999th  = " + DOUBLE_FORMAT.format(sn.getValue(0.99999));
097  }
098}