-
Notifications
You must be signed in to change notification settings - Fork 11
/
rltest_commands.py
185 lines (164 loc) · 8.09 KB
/
rltest_commands.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# run using:
# RLTest -t rltest_commands.py --module <directory>/rebloom.so -s
from RLTest import Env
from redisbloom.client import Client as RedisBloom
'''
from time import sleep
from unittest import TestCase
'''
# def CreateConn():
# port = 6379
# rb = RedisBloom(port=port)
# rb.flushdb()
# return rb
i = lambda l: [int(v) for v in l]
class TestRedisBloom():
def __init__(self):
self.env = Env()
self.rb = RedisBloom(port=6379)
def testCreate(self):
'''Test CREATE/RESERVE calls'''
self.env.cmd("flushall")
rb = self.rb
self.env.assertTrue(rb.bfCreate('bloom', 0.01, 1000))
self.env.assertTrue(rb.cfCreate('cuckoo', 1000))
self.env.assertTrue(rb.cmsInitByDim('cmsDim', 100, 5))
self.env.assertTrue(rb.cmsInitByProb('cmsProb', 0.01, 0.01))
self.env.assertTrue(rb.topkReserve('topk', 5, 100, 5, 0.9))
################### Test Bloom Filter ###################
def testBFAdd(self):
self.env.cmd("flushall")
rb = self.rb
self.env.assertTrue(rb.bfCreate('bloom', 0.01, 1000))
self.env.assertEqual(1, rb.bfAdd('bloom', 'foo'))
self.env.assertEqual(0, rb.bfAdd('bloom', 'foo'))
self.env.assertEqual([0], i(rb.bfMAdd('bloom', 'foo')))
self.env.assertEqual([0, 1], rb.bfMAdd('bloom', 'foo', 'bar'))
self.env.assertEqual([0, 0, 1], rb.bfMAdd('bloom', 'foo', 'bar', 'baz'))
self.env.assertEqual(1, rb.bfExists('bloom', 'foo'))
self.env.assertEqual(0, rb.bfExists('bloom', 'noexist'))
self.env.assertEqual([1, 0], i(rb.bfMExists('bloom', 'foo', 'noexist')))
def testBFInsert(self):
self.env.cmd("flushall")
rb = self.rb
self.env.assertTrue(rb.bfCreate('bloom', 0.01, 1000))
self.env.assertEqual([1], i(rb.bfInsert('bloom', ['foo'])))
self.env.assertEqual([0, 1], i(rb.bfInsert('bloom', ['foo', 'bar'])))
self.env.assertEqual([1], i(rb.bfInsert('captest', ['foo'], capacity=1000)))
self.env.assertEqual([1], i(rb.bfInsert('errtest', ['foo'], error=0.01)))
self.env.assertEqual(1, rb.bfExists('bloom', 'foo'))
self.env.assertEqual(0, rb.bfExists('bloom', 'noexist'))
self.env.assertEqual([1, 0], i(rb.bfMExists('bloom', 'foo', 'noexist')))
def testBFDumpLoad(self):
self.env.cmd("flushall")
rb = self.rb
# Store a filter
rb.bfCreate('myBloom', '0.0001', '1000')
# test is probabilistic and might fail. It is OK to change variables if
# certain to not break anything
def do_verify():
res = 0
for x in range(1000):
rb.bfAdd('myBloom', x)
rv = rb.bfExists('myBloom', x)
self.env.assertTrue(rv)
rv = rb.bfExists('myBloom', 'nonexist_{}'.format(x))
res += (rv == x)
self.env.assertLess(res, 5)
do_verify()
cmds = []
cur = rb.bfScandump('myBloom', 0)
first = cur[0]
cmds.append(cur)
while True:
cur = rb.bfScandump('myBloom', first)
first = cur[0]
if first == 0:
break
else:
cmds.append(cur)
prev_info = rb.execute_command('bf.debug', 'myBloom')
# Remove the filter
rb.execute_command('del', 'myBloom')
# Now, load all the commands:
for cmd in cmds:
rb.bfLoadChunk('myBloom', *cmd)
cur_info = rb.execute_command('bf.debug', 'myBloom')
self.env.assertEqual(prev_info, cur_info)
do_verify()
rb.execute_command('del', 'myBloom')
rb.bfCreate('myBloom', '0.0001', '10000000')
################### Test Cuckoo Filter ###################
def testCFAddInsert(self):
self.env.cmd("flushall")
rb = self.rb
self.env.assertTrue(rb.cfCreate('cuckoo', 1000))
self.env.assertTrue(rb.cfAdd('cuckoo', 'filter'))
self.env.assertFalse(rb.cfAddNX('cuckoo', 'filter'))
self.env.assertEqual(1, rb.cfAddNX('cuckoo', 'newItem'))
self.env.assertEqual([1], rb.cfInsert('captest', ['foo']))
self.env.assertEqual([1], rb.cfInsert('captest', ['foo'], capacity=1000))
self.env.assertEqual([1], rb.cfInsertNX('captest', ['bar']))
self.env.assertEqual([0, 0, 1], rb.cfInsertNX('captest', ['foo', 'bar', 'baz']))
self.env.assertEqual([0], rb.cfInsertNX('captest', ['bar'], capacity=1000))
self.env.assertEqual([1], rb.cfInsert('empty1', ['foo'], capacity=1000))
self.env.assertEqual([1], rb.cfInsertNX('empty2', ['bar'], capacity=1000))
def testCFExistsDel(self):
self.env.cmd("flushall")
rb = self.rb
self.env.assertTrue(rb.cfCreate('cuckoo', 1000))
self.env.assertTrue(rb.cfAdd('cuckoo', 'filter'))
self.env.assertTrue(rb.cfExists('cuckoo', 'filter'))
self.env.assertFalse(rb.cfExists('cuckoo', 'notexist'))
self.env.assertEqual(1, rb.cfCount('cuckoo', 'filter'))
self.env.assertEqual(0, rb.cfCount('cuckoo', 'notexist'))
self.env.assertTrue(rb.cfDel('cuckoo', 'filter'))
self.env.assertEqual(0, rb.cfCount('cuckoo', 'filter'))
################### Test Count-Min Sketch ###################
def testCMS(self):
self.env.cmd("flushall")
rb = self.rb
self.env.assertTrue(rb.cmsInitByDim('dim', 1000, 5))
self.env.assertTrue(rb.cmsInitByProb('prob', 0.01, 0.01))
self.env.assertTrue(rb.cmsIncrBy('dim', ['foo'], [5]))
self.env.assertEqual([0], rb.cmsQuery('dim', 'notexist'))
self.env.assertEqual([5], rb.cmsQuery('dim', 'foo'))
self.env.assertTrue(rb.cmsIncrBy('dim', ['foo', 'bar'], [5, 15]))
self.env.assertEqual([10, 15], rb.cmsQuery('dim', 'foo', 'bar'))
info = rb.cmsInfo('dim')
self.env.assertEqual(1000, info.width)
self.env.assertEqual(5, info.depth)
self.env.assertEqual(25, info.count)
def testCMSMerge(self):
self.env.cmd("flushall")
rb = self.rb
self.env.assertTrue(rb.cmsInitByDim('A', 1000, 5))
self.env.assertTrue(rb.cmsInitByDim('B', 1000, 5))
self.env.assertTrue(rb.cmsInitByDim('C', 1000, 5))
self.env.assertTrue(rb.cmsIncrBy('A', ['foo', 'bar', 'baz'], [5, 3, 9]))
self.env.assertTrue(rb.cmsIncrBy('B', ['foo', 'bar', 'baz'], [2, 3, 1]))
self.env.assertEqual([5, 3, 9], rb.cmsQuery('A', 'foo', 'bar', 'baz'))
self.env.assertEqual([2, 3, 1], rb.cmsQuery('B', 'foo', 'bar', 'baz'))
self.env.assertTrue(rb.cmsMerge('C', 2, ['A', 'B']))
self.env.assertEqual([7, 6, 10], rb.cmsQuery('C', 'foo', 'bar', 'baz'))
self.env.assertTrue(rb.cmsMerge('C', 2, ['A', 'B'], ['1', '2']))
self.env.assertEqual([9, 9, 11], rb.cmsQuery('C', 'foo', 'bar', 'baz'))
self.env.assertTrue(rb.cmsMerge('C', 2, ['A', 'B'], ['2', '3']))
self.env.assertEqual([16, 15, 21], rb.cmsQuery('C', 'foo', 'bar', 'baz'))
################### Test Top-K ###################
def testTopK(self):
self.env.cmd("flushall")
rb = self.rb
# test list with empty buckets
self.env.assertTrue(rb.topkReserve('topk', 10, 50, 3, 0.9))
self.env.assertTrue(rb.topkAdd('topk', 'A', 'B', 'C', 'D', 'E', 'A', 'A', 'B', 'C',
'G', 'D', 'B', 'D', 'A', 'E', 'E'))
self.env.assertEqual([1, 1, 1, 1, 1, 0, 1],
rb.topkQuery('topk', 'A', 'B', 'C', 'D', 'E', 'F', 'G'))
self.env.assertEqual([4, 3, 2, 3, 3, 0, 1],
rb.topkCount('topk', 'A', 'B', 'C', 'D', 'E', 'F', 'G'))
# test full list
self.env.assertTrue(rb.topkReserve('topklist', 3, 50, 3, 0.9))
self.env.assertTrue(rb.topkAdd('topklist', 'A', 'B', 'C', 'D', 'E','A', 'A', 'B', 'C',
'G', 'D', 'B', 'D', 'A', 'E', 'E'))
self.env.assertEqual(['D', 'A', 'B'], rb.topkList('topklist'))