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.metrics.impl;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.Arrays;
023import java.util.Random;
024import java.util.concurrent.ThreadLocalRandom;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.Assert;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033/**
034 * Testcases for FastLongHistogram.
035 */
036@Category({ MiscTests.class, SmallTests.class })
037public class TestFastLongHistogram {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestFastLongHistogram.class);
042
043  private static void doTestUniform(FastLongHistogram hist) {
044    long[] VALUES = { 0, 10, 20, 30, 40, 50 };
045    double[] qs = new double[VALUES.length];
046    for (int i = 0; i < qs.length; i++) {
047      qs[i] = (double) VALUES[i] / VALUES[VALUES.length - 1];
048    }
049
050    for (int i = 0; i < 10; i++) {
051      for (long v : VALUES) {
052        hist.add(v, 1);
053      }
054      long[] vals = hist.getQuantiles(qs);
055      System.out.println(Arrays.toString(vals));
056      for (int j = 0; j < qs.length; j++) {
057        Assert.assertTrue(j + "-th element org: " + VALUES[j] + ", act: " + vals[j],
058          Math.abs(vals[j] - VALUES[j]) <= 10);
059      }
060      hist.snapshotAndReset();
061    }
062  }
063
064  @Test
065  public void testUniform() {
066    FastLongHistogram hist = new FastLongHistogram(100, 0, 50);
067    doTestUniform(hist);
068  }
069
070  @Test
071  public void testAdaptionOfChange() {
072    // assumes the uniform distribution
073    FastLongHistogram hist = new FastLongHistogram(100, 0, 100);
074
075    Random rand = ThreadLocalRandom.current();
076
077    for (int n = 0; n < 10; n++) {
078      for (int i = 0; i < 900; i++) {
079        hist.add(rand.nextInt(100), 1);
080      }
081
082      // add 10% outliers, this breaks the assumption, hope bin10xMax works
083      for (int i = 0; i < 100; i++) {
084        hist.add(1000 + rand.nextInt(100), 1);
085      }
086
087      long[] vals = hist.getQuantiles(new double[] { 0.25, 0.75, 0.95 });
088      System.out.println(Arrays.toString(vals));
089      if (n == 0) {
090        Assert.assertTrue("Out of possible value", vals[0] >= 0 && vals[0] <= 50);
091        Assert.assertTrue("Out of possible value", vals[1] >= 50 && vals[1] <= 100);
092        Assert.assertTrue("Out of possible value", vals[2] >= 900 && vals[2] <= 1100);
093      }
094
095      hist.snapshotAndReset();
096    }
097  }
098
099  @Test
100  public void testGetNumAtOrBelow() {
101    long[] VALUES = { 1, 10, 20, 30, 40, 50 };
102
103    FastLongHistogram h = new FastLongHistogram();
104    for (long v : VALUES) {
105      for (int i = 0; i < 100; i++) {
106        h.add(v, 1);
107      }
108    }
109
110    h.add(Integer.MAX_VALUE, 1);
111
112    h.snapshotAndReset();
113
114    for (long v : VALUES) {
115      for (int i = 0; i < 100; i++) {
116        h.add(v, 1);
117      }
118    }
119    // Add something way out there to make sure it doesn't throw off the counts.
120    h.add(Integer.MAX_VALUE, 1);
121
122    assertEquals(100, h.getNumAtOrBelow(1));
123    assertEquals(200, h.getNumAtOrBelow(11));
124    assertEquals(601, h.getNumAtOrBelow(Long.MAX_VALUE));
125  }
126
127  @Test
128  public void testSameValues() {
129    FastLongHistogram hist = new FastLongHistogram(100);
130
131    hist.add(50, 100);
132
133    hist.snapshotAndReset();
134    doTestUniform(hist);
135  }
136}