11package analyzer_test
22
33import (
4+ "fmt"
5+ "math/rand"
46 "testing"
7+ "time"
58
69 "github.com/elecbug/netkit/v2/graph"
710 "github.com/elecbug/netkit/v2/graph/analyzer"
11+ "github.com/elecbug/netkit/v2/graph/standard"
812)
913
1014// TestShortestPaths tests the ShortestPaths method of the Analyzer to ensure it correctly finds the shortest path between two nodes in a graph.
1115func TestShortestPaths (t * testing.T ) {
16+ fmt .Println ("Test Shortest Paths" )
17+ testComputeShortestPath (t )
18+ testPerformance (t )
19+ }
20+
21+ // testComputeShortestPath sets up a simple graph and tests the ComputeAllShortestPaths and ShortestPaths
22+ // methods of the Analyzer to verify that it correctly computes and caches shortest paths, and that
23+ // it updates the cache when the graph changes. It checks for correct path results and proper error handling when paths are removed.
24+ func testComputeShortestPath (t * testing.T ) {
25+ fmt .Println ("- Test Compute Shortest Paths" )
1226 g := graph .New (true , true )
1327 g .AddNode ("A" )
1428 g .AddNode ("B" )
@@ -19,7 +33,7 @@ func TestShortestPaths(t *testing.T) {
1933 g .AddEdge ("A" , "C" , graph .NewWeight (2 ))
2034 g .AddEdge ("C" , "D" , graph .NewWeight (1 ))
2135
22- a := analyzer .NewAnalyzer (g )
36+ a := analyzer .NewAnalyzer (g , 1 )
2337
2438 paths , err := a .ShortestPaths ("A" , "D" )
2539 if err != nil {
@@ -72,6 +86,69 @@ func TestShortestPaths(t *testing.T) {
7286 }
7387}
7488
89+ func testPerformance (t * testing.T ) {
90+ fmt .Println ("- Test Performance" )
91+
92+ g , err := standard .ErdosRenyiGraph (
93+ 42 ,
94+ false ,
95+ func (from , to graph.NodeID ) * graph.Weight { return graph .NewWeight (rand .Float64 () * 100 ) },
96+ 1000 ,
97+ 0.01 ,
98+ )
99+ if err != nil {
100+ t .Fatalf ("failed to create graph: %v" , err )
101+ }
102+
103+ a := analyzer .NewAnalyzer (g , 1 )
104+
105+ startTime := time .Now ()
106+ _ , err = a .ShortestPaths ("0" , "999" )
107+ if err != nil {
108+ t .Fatalf ("unexpected error: %v" , err )
109+ }
110+ duration := time .Since (startTime )
111+ fmt .Printf (" - Time taken to compute shortest paths: %v\n " , duration )
112+
113+ a = analyzer .NewAnalyzer (g , 4 )
114+
115+ startTime = time .Now ()
116+ _ , err = a .ShortestPaths ("0" , "999" )
117+ if err != nil {
118+ t .Fatalf ("unexpected error: %v" , err )
119+ }
120+ duration = time .Since (startTime )
121+ fmt .Printf (" - Time taken to compute shortest paths with 4 cores: %v\n " , duration )
122+
123+ a = analyzer .NewAnalyzer (g , 16 )
124+
125+ startTime = time .Now ()
126+ _ , err = a .ShortestPaths ("0" , "999" )
127+ if err != nil {
128+ t .Fatalf ("unexpected error: %v" , err )
129+ }
130+ duration = time .Since (startTime )
131+ fmt .Printf (" - Time taken to compute shortest paths with 16 cores: %v\n " , duration )
132+
133+ a = analyzer .NewAnalyzer (g , 32 )
134+
135+ startTime = time .Now ()
136+ _ , err = a .ShortestPaths ("0" , "999" )
137+ if err != nil {
138+ t .Fatalf ("unexpected error: %v" , err )
139+ }
140+ duration = time .Since (startTime )
141+ fmt .Printf (" - Time taken to compute shortest paths with 32 cores: %v\n " , duration )
142+
143+ startTime = time .Now ()
144+ _ , err = a .ShortestPaths ("0" , "999" )
145+ if err != nil {
146+ t .Fatalf ("unexpected error: %v" , err )
147+ }
148+ duration = time .Since (startTime )
149+ fmt .Printf (" - Time taken to retrieve cached shortest paths: %v\n " , duration )
150+ }
151+
75152// equalPathSlices compares two graph.Path values by node sequence and total distance.
76153func equalPathSlices (a , b graph.Path ) bool {
77154 nodesA := a .Nodes ()
0 commit comments