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