Skip to content

Commit 57a516f

Browse files
committed
test(route): add comparison tests for Dijkstra algorithm implementations
Add performance and correctness tests comparing DijkstraRouteAlgorithm with DijkstraRouteAlgorithmHeap on Waxman and Tree topologies. Tests measure build time speedup and verify route query results match across 10000 random source-destination pairs.
1 parent 687401d commit 57a516f

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

test/network/test_route_ex.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from qns.network.route.dijkstra import DijkstraRouteAlgorithm
2+
from qns.network.route.dijkstra_heap import DijkstraRouteAlgorithmHeap
3+
from qns.network.topology.waxmantopo import WaxmanTopology
4+
from qns.network.topology.treetopo import TreeTopology
5+
import time
6+
7+
8+
def test_speed():
9+
dijk_default = DijkstraRouteAlgorithm()
10+
dijk_heap = DijkstraRouteAlgorithmHeap()
11+
12+
builder = WaxmanTopology(300, 100, 0.5, 0.2)
13+
topo = builder.build()
14+
15+
print(f"waxman node num: {len(topo[0])}, edge num: {len(topo[1])}")
16+
17+
nodes, edges = topo[0], topo[1]
18+
19+
t0 = time.perf_counter()
20+
dijk_default.build(nodes, edges)
21+
t1 = time.perf_counter()
22+
dijk_heap.build(nodes, edges)
23+
t2 = time.perf_counter()
24+
25+
print(f"\n=== Build Time ===")
26+
print(f"DijkstraRouteAlgorithm build time: {(t1-t0)*1000:.3f} ms")
27+
print(f"DijkstraRouteAlgorithmHeap build time: {(t2-t1)*1000:.3f} ms")
28+
print(f"Heap speedup: {(t1-t0)/(t2-t1):.2f}x")
29+
30+
31+
def test_func():
32+
dijk_default = DijkstraRouteAlgorithm()
33+
dijk_heap = DijkstraRouteAlgorithmHeap()
34+
35+
builder = TreeTopology(300, 3)
36+
topo = builder.build()
37+
38+
print(f"tree node num: {len(topo[0])}, edge num: {len(topo[1])}")
39+
40+
nodes, edges = topo[0], topo[1]
41+
42+
t0 = time.perf_counter()
43+
dijk_default.build(nodes, edges)
44+
t1 = time.perf_counter()
45+
dijk_heap.build(nodes, edges)
46+
t2 = time.perf_counter()
47+
48+
print(f"\n=== Route Result Comparison ===")
49+
mismatches = 0
50+
total_queries = 10000
51+
random.seed(42)
52+
53+
for _ in range(total_queries):
54+
src = random.choice(nodes)
55+
dest = random.choice(nodes)
56+
if src == dest:
57+
continue
58+
59+
result_default = dijk_default.query(src, dest)
60+
result_heap = dijk_heap.query(src, dest)
61+
62+
if len(result_default) != len(result_heap):
63+
mismatches += 1
64+
continue
65+
66+
if len(result_default) == 0:
67+
continue
68+
69+
m_default, next_default, path_default = result_default[0]
70+
m_heap, next_heap, path_heap = result_heap[0]
71+
72+
if m_default != m_heap:
73+
mismatches += 1
74+
continue
75+
76+
if next_default != next_heap:
77+
mismatches += 1
78+
continue
79+
80+
if path_default != path_heap:
81+
mismatches += 1
82+
continue
83+
84+
print(f"Total queries: {total_queries}")
85+
print(f"Mismatches: {mismatches}")
86+
print(f"Result: {'PASS' if mismatches == 0 else 'FAIL'}\n")
87+
88+
test_func()
89+
test_speed()

0 commit comments

Comments
 (0)