-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmst_test.py
48 lines (44 loc) · 1.63 KB
/
mst_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
import random
from atmst.all import MemoryBlockStore, NodeStore, NodeWrangler, mst_diff, very_slow_mst_diff
from cbrrr import CID
import time
PERF_BENCH = False
def random_test():
bs = MemoryBlockStore()
ns = NodeStore(bs)
nw = NodeWrangler(ns)
root = ns.get_node(None).cid
keys = []
for _ in range(10240 if PERF_BENCH else random.randrange(0, 32)):
k = random.randbytes(8).hex()
keys.append(k)
root = nw.put_record(root, k, CID.cidv1_dag_cbor_sha256_32_from(random.randbytes(8)))
root_a = root
for _ in range(8 if PERF_BENCH else random.randrange(0, 8)):
# some random additions
root = nw.put_record(root, random.randbytes(8).hex(), CID.cidv1_dag_cbor_sha256_32_from(random.randbytes(8)))
if keys:
# some random modifications
for _ in range(4 if PERF_BENCH else random.randrange(0, 4)):
for k in random.choice(keys):
root = nw.put_record(root, k, CID.cidv1_dag_cbor_sha256_32_from(random.randbytes(8)))
# some random deletions
for _ in range(4 if PERF_BENCH else random.randrange(0, 4)):
for k in random.choice(keys):
root = nw.del_record(root, k)
diff_start = time.time()
c, d = mst_diff(ns, root_a, root)
#c, d = very_slow_mst_diff(ns, root_a, root)
diff_duration = time.time()-diff_start
e, f = mst_diff(ns, root, root_a)
assert(c == f) # compare with reverse
assert(e == d) # compare with reverse
g, h = very_slow_mst_diff(ns, root_a, root)
assert(c == g) # compare with known-good
assert(d == h) # compare with known-good
return diff_duration
if __name__ == "__main__":
duration = 0
for _ in range(1 if PERF_BENCH else 2000):
duration += random_test()
print("time spent diffing (ms):", duration*1000)