View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */ 
18  package org.apache.hadoop.hbase.metrics;
19  
20  import static org.mockito.Matchers.anyFloat;
21  import static org.mockito.Matchers.anyLong;
22  import static org.mockito.Matchers.eq;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.verify;
25  
26  import java.util.Random;
27  
28  import org.apache.hadoop.hbase.metrics.histogram.MetricsHistogram;
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.metrics.MetricsRecord;
31  import org.junit.Assert;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  import com.yammer.metrics.stats.Snapshot;
36  
37  @SuppressWarnings("deprecation")
38  @Category(SmallTests.class)
39  public class TestMetricsHistogram {
40  
41    @Test
42    public void testBasicUniform() {
43      MetricsHistogram h = new MetricsHistogram("testHistogram", null);
44  
45      for (int i = 0; i < 100; i++) {
46        h.update(i);
47      }
48  
49      Assert.assertEquals(100, h.getCount());
50      Assert.assertEquals(0, h.getMin());
51      Assert.assertEquals(99, h.getMax());
52      Assert.assertEquals(49.5d, h.getMean(), 0.01);
53    }
54  
55    @Test
56    public void testSnapshotPercentiles() {
57      final MetricsHistogram h = new MetricsHistogram("testHistogram", null);
58      final long[] data = genRandomData(h);
59  
60      final Snapshot s = h.getSnapshot();
61  
62      assertPercentile(data, 50, s.getMedian());
63      assertPercentile(data, 75, s.get75thPercentile());
64      assertPercentile(data, 95, s.get95thPercentile());
65      assertPercentile(data, 98, s.get98thPercentile());
66      assertPercentile(data, 99, s.get99thPercentile());
67      assertPercentile(data, 99.9, s.get999thPercentile());
68    }
69  
70    @Test
71    public void testPushMetric() {
72      final MetricsHistogram h = new MetricsHistogram("testHistogram", null);
73      genRandomData(h);
74  
75      MetricsRecord mr = mock(MetricsRecord.class);
76      h.pushMetric(mr);
77      
78      verify(mr).setMetric("testHistogram_num_ops", 10000L);
79      verify(mr).setMetric(eq("testHistogram_min"), anyLong());
80      verify(mr).setMetric(eq("testHistogram_max"), anyLong());
81      verify(mr).setMetric(eq("testHistogram_mean"), anyFloat());
82      verify(mr).setMetric(eq("testHistogram_std_dev"), anyFloat());
83      verify(mr).setMetric(eq("testHistogram_median"), anyFloat());
84      verify(mr).setMetric(eq("testHistogram_75th_percentile"), anyFloat());
85      verify(mr).setMetric(eq("testHistogram_95th_percentile"), anyFloat());
86      verify(mr).setMetric(eq("testHistogram_99th_percentile"), anyFloat());    
87    }
88  
89    private void assertPercentile(long[] data, double percentile, double value) {
90      int count = 0;
91      for (long v : data) {
92        if (v < value) {
93          count++;
94        }
95      }
96      Assert.assertEquals("Wrong " + percentile + " percentile", 
97          (int)(percentile / 100), count / data.length);
98    }
99    
100   private long[] genRandomData(final MetricsHistogram h) {
101     final Random r = new Random();
102     final long[] data = new long[10000];
103 
104     for (int i = 0; i < data.length; i++) {
105       data[i] = (long) (r.nextGaussian() * 10000);
106       h.update(data[i]);
107     }
108     
109     return data;
110   }
111   
112 }