forked from kod3r/pyblume
/
pyblume_test.py
executable file
·115 lines (92 loc) · 3.06 KB
/
pyblume_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
import hashlib
import time
import struct
import random
import tempfile
import os
import pyblume
def pyblume_test_fpp(N):
print ' a. Creating filter'
bloom = pyblume.Filter(1024 * 1024 * 10, 0.000001, "/tmp/pyblume_fpp_test.bf")
print ' b. Loading filter'
start = time.time()
for k in xrange(0, N, 10):
bloom.add(str(k))
end = time.time()
print ' bloom.add: %d/s' % ((N / 10) / (end - start))
print ' c. Computing false positive rate'
fn_count = 0
fp_count = 0
start = time.time()
for k in xrange(0, N):
hit = str(k) in bloom
if hit and k % 10 != 0:
fp_count += 1
elif not hit and k % 10 == 0:
fn_count += 1
end = time.time()
print ' bloom.check: %d/s' % (N / (end - start))
print ' d. %d false positives out of %d trials (p = %0.4f)' % (fp_count, N, float(fp_count) / N)
def pyblume_test_fromfile(N):
filepath = '/tmp/pyblume_fromfile_test.bf'
try:
os.unlink(filepath)
except:
pass
def create_filter():
print ' a. Creating filter at "%s"' % (filepath,)
bloom = pyblume.Filter(1024 * 1024 * 10, 0.000001, filepath)
print ' b. Loading filter'
start = time.time()
for k in xrange(0, N, 10):
bloom.add(str(k))
end = time.time()
print ' bloom.add: %d/s' % ((N / 10) / (end - start))
bloom.close()
def test_filter():
bloom = pyblume.open(filepath)
print ' c. Computing false positive rate'
fn_count = 0
fp_count = 0
start = time.time()
for k in xrange(0, N):
hit = str(k) in bloom
if hit and k % 10 != 0:
fp_count += 1
elif not hit and k % 10 == 0:
fn_count += 1
end = time.time()
print ' bloom.check: %d/s' % (N / (end - start))
print ' d. %d false positives out of %d trials (p = %0.4f)' % (fp_count, N, float(fp_count) / N)
create_filter()
test_filter()
def pyblume_test_full(N):
print ' a. Creating filter'
bloom = pyblume.Filter(1024 * 512, 0.000001, "/tmp/pyblume_full_test.bf")
print ' b. Loading filter'
start = time.time()
for k in xrange(0, N, 10):
bloom.add(str(k))
end = time.time()
print ' bloom.add: %d/s' % ((N / 10) / (end - start))
def pyblume_test_locking(N):
print ' a. Creating filter'
bloom = pyblume.Filter(1024 * 512, 0.000001, "/tmp/pyblume_locking_test.bf")
bloom2 = pyblume.open("/tmp/pyblume_locking_test.bf", for_write=1)
print ' b. Loading filter'
start = time.time()
for k in xrange(0, N, 10):
bloom.add(str(k))
end = time.time()
print ' bloom.add: %d/s' % ((N / 10) / (end - start))
try:
pyblume_test_locking(1000000 * 10)
except pyblume.LockedException as e:
print 'OK!'
try:
pyblume_test_full(1000000 * 10)
except pyblume.FullException as e:
print 'OK!'
pyblume_test_fpp(1000000 * 10)
pyblume_test_fromfile(1000000 * 10)