1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29 public final class YammerHistogramUtils {
30
31
32 private YammerHistogramUtils() {}
33
34
35
36
37 private static DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#0.00");
38
39
40
41
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
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
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 }