forked from jsommers/pytricia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perftest.py
87 lines (73 loc) · 2.42 KB
/
perftest.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
import pytricia
import radix
import SubnetTree
import random
def build_tree_subnet(s):
n = 0
for i in xrange(256):
for j in xrange(256):
# for k in xrange(256):
# prefix='.'.join([str(i),str(j),str(k),"0"])+'/16'
prefix='.'.join([str(i),str(j),"0.0"])+'/16'
s[prefix] = n
n += 1
def lookups_subnet(s, n=100000):
for i in xrange(n):
random.randint(0,255)
addr='.'.join([str(random.randint(0,255)),str(random.randint(0,255)),str(random.randint(0,255)),str(random.randint(0,255))])
val = s[addr]
def runtestsubnet():
s = SubnetTree.SubnetTree()
build_tree_subnet(s)
lookups_subnet(s)
def build_tree_radix(r):
n = 0
for i in xrange(256):
for j in xrange(256):
prefix='.'.join([str(i),str(j),"0.0"])
node = r.add(network=prefix, masklen=24)
node.data["n"] = n
n += 1
def lookups_radix(r, n=100000):
for i in xrange(n):
random.randint(0,255)
addr='.'.join([str(random.randint(0,255)),str(random.randint(0,255)),str(random.randint(0,255)),str(random.randint(0,255))])
node = r.search_exact(addr)
def runtestradix():
r = radix.Radix()
build_tree_radix(r)
lookups_radix(r)
def build_tree(pt):
n = 0
for i in xrange(256):
for j in xrange(256):
# for k in xrange(256):
# prefix='.'.join([str(i),str(j),str(k),"0"])+'/16'
prefix='.'.join([str(i),str(j),"0.0"])+'/16'
pt[prefix] = n
n += 1
def lookups(pt, n=100000):
for i in xrange(n):
random.randint(0,255)
addr='.'.join([str(random.randint(0,255)),str(random.randint(0,255)),str(random.randint(0,255)),str(random.randint(0,255))])
val = pt[addr]
def runtestpt():
pt = pytricia.PyTricia(32)
build_tree(pt)
lookups(pt)
def main():
from timeit import Timer
t = Timer("runtestpt()", "from __main__ import runtestpt")
v = t.timeit(number=10)
v = v / 10.0
print "Average execution time for PyTricia:",v
t = Timer("runtestradix()", "from __main__ import runtestradix")
v = t.timeit(number=10)
v = v / 10.0
print "Average execution time for radix:",v
t = Timer("runtestsubnet()", "from __main__ import runtestsubnet")
v = t.timeit(number=10)
v = v / 10.0
print "Average execution time for subnet:",v
if __name__ == '__main__':
main()