View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import com.yammer.metrics.core.Histogram;
22  import com.yammer.metrics.stats.Sample;
23  import com.yammer.metrics.stats.Snapshot;
24  
25  import java.lang.reflect.Constructor;
26  import java.text.DecimalFormat;
27  
28  /** Utility functions for working with Yammer Metrics. */
29  public final class YammerHistogramUtils {
30  
31    // not for public consumption
32    private YammerHistogramUtils() {}
33  
34    /**
35     * Used formatting doubles so only two places after decimal point.
36     */
37    private static DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#0.00");
38  
39    /**
40     * Create a new {@link com.yammer.metrics.core.Histogram} instance. These constructors are
41     * not public in 2.2.0, so we use reflection to find them.
42     */
43    public static Histogram newHistogram(Sample sample) {
44      try {
45        Constructor<?> ctor =
46            Histogram.class.getDeclaredConstructor(Sample.class);
47        ctor.setAccessible(true);
48        return (Histogram) ctor.newInstance(sample);
49      } catch (Exception e) {
50        throw new RuntimeException(e);
51      }
52    }
53  
54    /** @return an abbreviated summary of {@code hist}. */
55    public static String getShortHistogramReport(final Histogram hist) {
56      Snapshot sn = hist.getSnapshot();
57      return "mean=" + DOUBLE_FORMAT.format(hist.mean()) +
58          ", min=" + DOUBLE_FORMAT.format(hist.min()) +
59          ", max=" + DOUBLE_FORMAT.format(hist.max()) +
60          ", stdDev=" + DOUBLE_FORMAT.format(hist.stdDev()) +
61          ", 95th=" + DOUBLE_FORMAT.format(sn.get95thPercentile()) +
62          ", 99th=" + DOUBLE_FORMAT.format(sn.get99thPercentile());
63    }
64  
65    /** @return a summary of {@code hist}. */
66    public static String getHistogramReport(final Histogram hist) {
67      Snapshot sn = hist.getSnapshot();
68      return ", mean=" + DOUBLE_FORMAT.format(hist.mean()) +
69          ", min=" + DOUBLE_FORMAT.format(hist.min()) +
70          ", max=" + DOUBLE_FORMAT.format(hist.max()) +
71          ", stdDev=" + DOUBLE_FORMAT.format(hist.stdDev()) +
72          ", 50th=" + DOUBLE_FORMAT.format(sn.getMedian()) +
73          ", 75th=" + DOUBLE_FORMAT.format(sn.get75thPercentile()) +
74          ", 95th=" + DOUBLE_FORMAT.format(sn.get95thPercentile()) +
75          ", 99th=" + DOUBLE_FORMAT.format(sn.get99thPercentile()) +
76          ", 99.9th=" + DOUBLE_FORMAT.format(sn.get999thPercentile()) +
77          ", 99.99th=" + DOUBLE_FORMAT.format(sn.getValue(0.9999)) +
78          ", 99.999th=" + DOUBLE_FORMAT.format(sn.getValue(0.99999));
79    }
80  }