-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_all.py
82 lines (65 loc) · 2.63 KB
/
test_all.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
import BinaryNode
import CompressedNode
import MultibitNode
import timeit
binary_root = BinaryNode.Create('0')
compressed_root = CompressedNode.Create("0")
multibit_root = MultibitNode.Create()
# starting performing lookups
with open('tosearch.txt', 'r') as t:
my_list = [line.rstrip('\n') for line in t]
times = []
for entry in my_list:
binary_address = entry.split(',')[1]
start = timeit.default_timer()
binary_root.Lookup(binary_address)
end = timeit.default_timer() - start
times.append(end * 1000)
print("binary_trie (avg per lookup): " + str(sum(times)/len(times)) + "ms")
print("binary_trie (sum of all times): " + str(sum(times)) + "ms")
times = []
for entry in my_list:
binary_address = entry.split(',')[1]
start = timeit.default_timer()
compressed_root.Lookup(binary_address)
end = timeit.default_timer() - start
times.append(end * 1000)
print("compressed trie (avg per lookup): " + str(sum(times)/len(times)) + "ms")
print("compressed trie (sum of all times): " + str(sum(times)) + "ms")
times = []
for entry in my_list:
binary_address = entry.split(',')[1]
start = timeit.default_timer()
multibit_root.Lookup(binary_address, "0")
end = timeit.default_timer() - start
times.append(end * 1000)
print("multibit trie (avg per lookup): " + str(sum(times)/len(times)) + "ms")
print("multibit trie (sum of all times): " + str(sum(times)) + "ms")
# custom ips to search
# CODE FROM RYU
switch = {}
switch["195.0.0.254"] = ["195.0.0.254", "8", "1"]
switch["128.128.0.254"] = ["128.128.0.254", "12", "2"]
switch["154.128.0.254"] = ["154.128.0.254", "16", "3"]
switch["197.160.0.254"] = ["197.160.0.254", "24", "4"]
switch["192.168.0.254"] = ["192.168.0.254", "24", "5"]
switch["192.169.0.254"] = ["192.169.0.254", "24", "6"]
switch["192.170.0.254"] = ["192.170.0.254", "24", "7"]
_root_multibit = MultibitNode.MultibitNode()
_root_binary = BinaryNode.BinaryNode("0")
_root_compressed = CompressedNode.CompressedNode("0")
tuples = []
for key, value in switch.iteritems(): # creating list of tuples for ordering
ip = value[0]
mask = int(value[1])
tuples.append((ip, mask))
for entry in sorted(tuples, key=lambda x: x[1]):
ip, mask = entry
binary_address = MultibitNode.convert_in_bin(ip)[:mask]
_root_multibit.AddChild(ip, binary_address)
_root_binary.AddChild(ip, binary_address)
_root_compressed.AddChild(ip, binary_address)
binary_address = MultibitNode.convert_in_bin("195.0.128.1")
print (_root_multibit.LookupNonRecursive(binary_address, "0"))
print (_root_binary.LookupNonRecursive(binary_address, "0"))
print (_root_compressed.LookupNonRecursive(binary_address, "0"))