Skip to content

Commit

Permalink
Add autoscaling test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdalpino committed Oct 13, 2024
1 parent 04470d2 commit 9599487
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ jobs:
python-version: ${{ matrix.python }}

- name: Install dependencies
run: pip install .

- name: Install Mypy
run: pip install mypy
run: pip install -e '.[test]'

- name: Static analysis
run: mypy ./src
Expand Down
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools >= 61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "PyBloomer"
name = "OkBloomer"
version = "0.0.1"
requires-python = ">= 3.10"
dependencies = [
Expand All @@ -12,10 +12,14 @@ dependencies = [
authors = [
{name = "Andrew DalPino", email = "[email protected]"},
]
description = "PyBloomer is an implementation of the OkBloomer algorithm, an autoscaling Bloom filter with ultra-low memory footprint for Python."
description = "PyBloomer is an Python implementation of the OkBloomer algorithm, an autoscaling Bloom filter with ultra-low memory footprint."
readme = "README.md"
license = {text = "MIT"}

[project.optional-dependencies]
dev = ["mypy"]
test = ["mypy"]

[project.urls]
Homepage = "https://github.com/andrewdalpino/PyBloomer"
Documentation = "https://github.com/andrewdalpino/PyBloomer/README.md"
Expand Down
44 changes: 33 additions & 11 deletions tests/test_bloom_filter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import unittest
import random
import string

import pybloomer

class TestBloomFilter(unittest.TestCase):
def test_basic(self):
filter = pybloomer.BloomFilter(
max_false_positive_rate=0.01,
num_hashes=4,
layer_size=32000000,
)
filter = pybloomer.BloomFilter()

self.assertEqual(filter.false_positive_rate, 0)

Expand All @@ -26,12 +24,10 @@ def test_basic(self):
self.assertTrue(filter.exists('bar'))
self.assertGreater(filter.false_positive_rate, 0)

self.assertFalse(filter.exists('baz'))

def test_exists_or_insert(self):
filter = pybloomer.BloomFilter(
max_false_positive_rate=0.01,
num_hashes=4,
layer_size=32000000,
)
filter = pybloomer.BloomFilter()

self.assertFalse(filter.exists_or_insert('foo'))

Expand All @@ -43,4 +39,30 @@ def test_exists_or_insert(self):

self.assertFalse(filter.exists_or_insert('baz'))

self.assertTrue(filter.exists_or_insert('baz'))
self.assertTrue(filter.exists_or_insert('baz'))

def test_autoscaling(self):
random.seed(0)

filter = pybloomer.BloomFilter(
max_false_positive_rate=0.01,
num_hashes=4,
layer_size=320000,
)

self.assertEqual(filter.num_layers, 1)

filter.insert('foo')

for i in range(0, 100000):
filter.insert(''.join(random.choice(string.ascii_letters) for j in range(20)))

filter.insert('bar')

self.assertEqual(filter.num_layers, 3)
self.assertLessEqual(filter.false_positive_rate, 0.01)
self.assertLessEqual(filter.utilization, 1.0)

self.assertTrue(filter.exists('foo'))
self.assertTrue(filter.exists('bar'))
self.assertFalse(filter.exists('father'))

0 comments on commit 9599487

Please sign in to comment.