@@ -11,7 +11,8 @@ import (
1111 "github.com/elecbug/netkit/v2/graph/standard"
1212)
1313
14- // TestShortestPaths tests the ShortestPaths method of the Analyzer to ensure it correctly finds the shortest path between two nodes in a graph.
14+ // TestShortestPaths tests the functionality of the Analyzer's shortest path computations, including
15+ // cache management and performance with different parallel core counts.
1516func TestShortestPaths (t * testing.T ) {
1617 fmt .Println ("Test Shortest Paths" )
1718 testComputeShortestPath (t )
@@ -86,6 +87,9 @@ func testComputeShortestPath(t *testing.T) {
8687 }
8788}
8889
90+ // testPerformance creates a larger random graph and tests the performance of the ShortestPaths method with different
91+ // parallel core counts. It measures the time taken to compute shortest paths and to retrieve cached results, ensuring
92+ // that the method works correctly and efficiently under various conditions.
8993func testPerformance (t * testing.T ) {
9094 fmt .Println ("- Test Performance" )
9195
@@ -103,45 +107,62 @@ func testPerformance(t *testing.T) {
103107 a := analyzer .NewAnalyzer (g , 1 )
104108
105109 startTime := time .Now ()
106- _ , err = a .ShortestPaths ("0" , "999" )
110+ paths , err := a .ShortestPaths ("0" , "999" )
111+ // NOTE: paths may be empty if 0 and 999 are not connected (P(no path) ≈ 0.009%),
112+ // but we just want to test the performance of the method,
113+ // so we won't fail the test in that case.
107114 if err != nil {
108115 t .Fatalf ("unexpected error: %v" , err )
116+ } else if len (paths ) > 0 {
117+ fmt .Printf (" - Found shortest paths from 0 to 999: %v\n " , paths [0 ])
109118 }
110119 duration := time .Since (startTime )
111120 fmt .Printf (" - Time taken to compute shortest paths: %v\n " , duration )
112121
113122 a = analyzer .NewAnalyzer (g , 4 )
114123
115124 startTime = time .Now ()
116- _ , err = a .ShortestPaths ("0" , "999" )
125+ pathsCompared , err : = a .ShortestPaths ("0" , "999" )
117126 if err != nil {
118127 t .Fatalf ("unexpected error: %v" , err )
119128 }
120129 duration = time .Since (startTime )
121130 fmt .Printf (" - Time taken to compute shortest paths with 4 cores: %v\n " , duration )
122131
132+ if paths [0 ].TotalDistance () != pathsCompared [0 ].TotalDistance () {
133+ t .Errorf ("expected total distance %v, got %v" , paths [0 ].TotalDistance (), pathsCompared [0 ].TotalDistance ())
134+ }
135+
123136 a = analyzer .NewAnalyzer (g , 16 )
124137
125138 startTime = time .Now ()
126- _ , err = a .ShortestPaths ("0" , "999" )
139+ pathsCompared , err = a .ShortestPaths ("0" , "999" )
127140 if err != nil {
128141 t .Fatalf ("unexpected error: %v" , err )
129142 }
130143 duration = time .Since (startTime )
131144 fmt .Printf (" - Time taken to compute shortest paths with 16 cores: %v\n " , duration )
132145
146+ if paths [0 ].TotalDistance () != pathsCompared [0 ].TotalDistance () {
147+ t .Errorf ("expected total distance %v, got %v" , paths [0 ].TotalDistance (), pathsCompared [0 ].TotalDistance ())
148+ }
149+
133150 a = analyzer .NewAnalyzer (g , 32 )
134151
135152 startTime = time .Now ()
136- _ , err = a .ShortestPaths ("0" , "999" )
153+ pathsCompared , err = a .ShortestPaths ("0" , "999" )
137154 if err != nil {
138155 t .Fatalf ("unexpected error: %v" , err )
139156 }
140157 duration = time .Since (startTime )
141158 fmt .Printf (" - Time taken to compute shortest paths with 32 cores: %v\n " , duration )
142159
160+ if paths [0 ].TotalDistance () != pathsCompared [0 ].TotalDistance () {
161+ t .Errorf ("expected total distance %v, got %v" , paths [0 ].TotalDistance (), pathsCompared [0 ].TotalDistance ())
162+ }
163+
143164 startTime = time .Now ()
144- _ , err = a .ShortestPaths ("0" , "999 " )
165+ _ , err = a .ShortestPaths ("0" , "1 " )
145166 if err != nil {
146167 t .Fatalf ("unexpected error: %v" , err )
147168 }
0 commit comments