001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.util; 021 022import static org.junit.Assert.assertEquals; 023 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.testclassification.MiscTests; 026import org.apache.hadoop.hbase.testclassification.SmallTests; 027 028import org.junit.ClassRule; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032@Category({MiscTests.class, SmallTests.class}) 033public class TestLossyCounting { 034 035 @ClassRule 036 public static final HBaseClassTestRule CLASS_RULE = 037 HBaseClassTestRule.forClass(TestLossyCounting.class); 038 039 @Test 040 public void testBucketSize() { 041 LossyCounting lossyCounting = new LossyCounting(0.01, "testBucketSize", null); 042 assertEquals(100L, lossyCounting.getBucketSize()); 043 LossyCounting lossyCounting2 = new LossyCounting("testBucketSize2", null); 044 assertEquals(50L, lossyCounting2.getBucketSize()); 045 } 046 047 @Test 048 public void testAddByOne() { 049 LossyCounting lossyCounting = new LossyCounting(0.01, "testAddByOne", null); 050 for(int i = 0; i < 100; i++){ 051 String key = "" + i; 052 lossyCounting.add(key); 053 } 054 assertEquals(100L, lossyCounting.getDataSize()); 055 for(int i = 0; i < 100; i++){ 056 String key = "" + i; 057 assertEquals(true, lossyCounting.contains(key)); 058 } 059 } 060 061 @Test 062 public void testSweep1() { 063 LossyCounting lossyCounting = new LossyCounting(0.01, "testSweep1", null); 064 for(int i = 0; i < 400; i++){ 065 String key = "" + i; 066 lossyCounting.add(key); 067 } 068 assertEquals(4L, lossyCounting.getCurrentTerm()); 069 //if total rows added are proportional to bucket size 070 assertEquals(lossyCounting.getBucketSize() - 1, lossyCounting.getDataSize()); 071 } 072 073 @Test 074 public void testSweep2() { 075 LossyCounting lossyCounting = new LossyCounting(0.1, "testSweep2", null); 076 for(int i = 0; i < 10; i++){ 077 String key = "" + i; 078 lossyCounting.add(key); 079 } 080 assertEquals(10L, lossyCounting.getDataSize()); 081 for(int i = 0; i < 10; i++){ 082 String key = "1"; 083 lossyCounting.add(key); 084 } 085 assertEquals(1L, lossyCounting.getDataSize()); 086 } 087 088 089}