def TEST_BQ_Insert(): bq = BloomQueue(10, 10, 10) bq.request(1, 10) if sum(bq.filters[bq.currentFilter]) != 1: return "Data not inserted into currentFilter when it should be." if sum(bq.filters[bq.currentFilter + 1]) != 0: return "Data inserted into currentFilter + 1 when it shouldn't be." bq.request(2, 15) bq.request(2, 20) if len(bq.cache.keys()) != 2: return stringGenerator("Number of keys in cache", len(bq.cache.keys()), 2) if bq.cacheHits != 1: return stringGenerator("Cache hit", bq.cacheHits, 1) if bq.cacheMisses != 2: return stringGenerator("Cache miss", bq.cacheMisses, 2) if bq.falsePositives != 0: return stringGenerator("False positive", bq.falsePositives, 0) if bq.falseNegatives != 0: return stringGenerator("False negative", bq.falseNegatives, 0) return True
def TEST_BQ_Insert(): bq = BloomQueue(10, 10, 10) initalizeWithTestData(bq) bq.request("data2", 20) if len(bq.cache.keys()) != 2: return stringGenerator("Number of keys in cache", len(bq.cache.keys()), 2) if bq.cacheHits != 1: return stringGenerator("Cache hit", bq.cacheHits, 1) if bq.cacheMisses != 2: return stringGenerator("Cache miss", bq.cacheMisses, 2) if bq.falsePositives != 0: return stringGenerator("False positive", bq.falsePositives, 0) if bq.falseNegatives != 0: return stringGenerator("False negative", bq.falseNegatives, 0) return True
def TEST_BQ_StepForwardFalsePostive(): bq = BloomQueue(10, 10, 10) initalizeWithTestData(bq) for x in range(0, 16): bq.stepForwardInTime() # data2 should be expired (expired at time), but still be in the current filter bq.request("data2", 10) # There may be more than one false positive depending on what the hash values are if bq.falsePositives > 1: return stringGenerator("False positive", bq.falsePositives, 1) return True
def TEST_BQ_StepForwardExpire(): bq = BloomQueue(10, 10, 10) initalizeWithTestData(bq) for x in range(0, 10): bq.stepForwardInTime() if bq.time != 10: return stringGenerator("Queue time", bq.time, 10) # We should have advanced to the next filter if bq.currentFilter != 1: return stringGenerator("Current filter", bq.currentFilter, 1) # There should be exactly slot in the filter equal to 1 at this point numItems = sum(bq.filters[bq.currentFilter]) if numItems != 1: return stringGenerator("Items in current filter", numItems, 1) # All positions in the filter should be empty now. for x in range(0, 10): bq.stepForwardInTime() # We should have advanced to the next filter if bq.currentFilter != 2: return stringGenerator("Current filter", bq.currentFilter, 2) numItems = sum(bq.filters[bq.currentFilter]) if numItems != 0: return stringGenerator("Items in current filter", numItems, 0) # Make sure both items are indeed expired. if bq.cacheMisses != 2: return stringGenerator("Cache miss", bq.cacheMisses, 2) initalizeWithTestData(bq) if bq.cacheMisses != 4: return stringGenerator("Cache miss", bq.cacheMisses, 4) return True
from CountingFilter import CountingFilter import Constants import random import matplotlib.pyplot as plt import pprint # Bloom Queue false positives bqfp = [] # Counting Filter false positives cffp = [] # Relative total run time of the two queues runTimeRatios = [] for ttl in range(1, Constants.TTL * 3): print(ttl) bq = BloomQueue(Constants.DEPTH, Constants.RESOLUTION, Constants.WIDTH) cf = CountingFilter(Constants.BITS, Constants.WIDTH) # Total number of requests made requests = 0 # Sweep through time for x in range(0, Constants.TIME): for y in range(0, Constants.RATE): # Generate a random object and inert it into the filter data = random.randrange(0, Constants.NUMBER_OF_OBJECTS) requests += 1 for filter in [cf, bq]: filter.request(data, ttl) # Advance time for both filters for filter in [cf, bq]: filter.stepForwardInTime()
import random import matplotlib.pyplot as plt import pprint spreads = [] # Bloom Queue false positives bqfp = [] # Counting Filter false positives cffp = [] runTimeRatios = [] # Spread indicates how much of the RESOLUTION interval requests are spread across # Sweep through them all (i.e. only the first time unit of each resolution up to the entire thing) for spread in range(1, Constants.RESOLUTION + 1): bq = BloomQueue(Constants.DEPTH, Constants.RESOLUTION, Constants.WIDTH) cf = CountingFilter(Constants.BITS, Constants.WIDTH) # Total number of requests made requests = 0 for x in range(0, Constants.TIME): # Make it so there are only requests during the first 'spread' seconds of each resolution if x % Constants.RESOLUTION >= spread: # We want the same average overall rate, but only during the 'spread' for y in range( 0, Constants.RATE * max(int(Constants.RESOLUTION / spread), 1)): # Generate a random object and inert it into the filter data = random.randrange(0, Constants.NUMBER_OF_OBJECTS) requests += 1 for filter in [cf, bq]: