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.util;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.stream.IntStream;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.testclassification.MiscTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.ClassRule;
028import org.junit.Test;
029import org.junit.experimental.categories.Category;
030
031@Category({ MiscTests.class, SmallTests.class })
032public class TestReservoirSample {
033
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE =
036    HBaseClassTestRule.forClass(TestReservoirSample.class);
037
038  @Test
039  public void test() {
040    int round = 100000;
041    int containsOne = 0;
042    for (int i = 0; i < round; i++) {
043      ReservoirSample<Integer> rs = new ReservoirSample<>(10);
044      for (int j = 0; j < 100; j++) {
045        rs.add(j);
046        if (j < 10) {
047          assertEquals(j + 1, rs.getSamplingResult().size());
048        } else {
049          assertEquals(10, rs.getSamplingResult().size());
050        }
051      }
052      if (rs.getSamplingResult().contains(1)) {
053        containsOne++;
054      }
055    }
056    // we assume a 5% error rate
057    assertTrue(containsOne > round / 10 * 0.95);
058    assertTrue(containsOne < round / 10 * 1.05);
059  }
060
061  @Test
062  public void testIterator() {
063    int round = 100000;
064    int containsOne = 0;
065    for (int i = 0; i < round; i++) {
066      ReservoirSample<Integer> rs = new ReservoirSample<>(10);
067      rs.add(IntStream.range(0, 100).mapToObj(Integer::valueOf).iterator());
068      if (rs.getSamplingResult().contains(1)) {
069        containsOne++;
070      }
071    }
072    // we assume a 5% error rate
073    assertTrue(containsOne > round / 10 * 0.95);
074    assertTrue(containsOne < round / 10 * 1.05);
075  }
076
077  @Test
078  public void testStream() {
079    int round = 100000;
080    int containsOne = 0;
081    for (int i = 0; i < round; i++) {
082      ReservoirSample<Integer> rs = new ReservoirSample<>(10);
083      rs.add(IntStream.range(0, 100).mapToObj(Integer::valueOf));
084      if (rs.getSamplingResult().contains(1)) {
085        containsOne++;
086      }
087    }
088    // we assume a 5% error rate
089    assertTrue(containsOne > round / 10 * 0.95);
090    assertTrue(containsOne < round / 10 * 1.05);
091  }
092}