-
Notifications
You must be signed in to change notification settings - Fork 24
/
perftest.py
110 lines (93 loc) · 3.22 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# This file is part of Pytricia.
# Joel Sommers <[email protected]>
#
# Pytricia is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pytricia is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pytricia. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function
import pytricia
import radix
import SubnetTree
import random
if sys.version_info.major == 3:
xrange = range
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()