-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
79 lines (74 loc) · 2.21 KB
/
Copy pathbenchmark_test.go
File metadata and controls
79 lines (74 loc) · 2.21 KB
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 MaIII Themd
package dijkstra
import (
"fmt"
"testing"
)
// buildGrid returns a side x side 4-connected unit-weight grid graph.
// Vertices are named "r<row>c<col>". Used to benchmark a realistic
// spatial pathfinding workload (e.g. a robot map).
func buildGrid(side int) *StGraph {
var g StGraph
name := func(r, c int) string { return fmt.Sprintf("r%dc%d", r, c) }
for r := 0; r < side; r++ {
for c := 0; c < side; c++ {
var edges []StEdge
if r > 0 {
edges = append(edges, StEdge{ToVertexName: name(r-1, c), Weight: 1})
}
if r < side-1 {
edges = append(edges, StEdge{ToVertexName: name(r+1, c), Weight: 1})
}
if c > 0 {
edges = append(edges, StEdge{ToVertexName: name(r, c-1), Weight: 1})
}
if c < side-1 {
edges = append(edges, StEdge{ToVertexName: name(r, c+1), Weight: 1})
}
g.VertexAdd(name(r, c), float64(c), float64(r), 0, edges...)
}
}
return &g
}
// BenchmarkScale exercises larger grids (1k / 10k / 25.6k vertices) so
// speed and allocation changes at scale are visible. It measures a full
// single-source Dijkstra (corner goal) and a goal-directed A* (mid
// goal) on each grid.
func BenchmarkScale(b *testing.B) {
for _, side := range []int{32, 100, 160} { // 1024, 10000, 25600
g := buildGrid(side)
corner := fmt.Sprintf("r%dc%d", side-1, side-1)
mid := fmt.Sprintf("r%dc%d", side/4, side/4)
b.Run(fmt.Sprintf("V=%d/dijkstra-corner", side*side), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if ok, _ := g.DijkstraSearch("r0c0", corner); !ok {
b.Fatal("no path")
}
}
})
b.Run(fmt.Sprintf("V=%d/astar-mid", side*side), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if ok, _ := g.AStarSearch("r0c0", mid); !ok {
b.Fatal("no path")
}
}
})
}
}
func BenchmarkDijkstraSearch(b *testing.B) {
for _, side := range []int{7, 14, 32} {
g := buildGrid(side)
from, to := "r0c0", fmt.Sprintf("r%dc%d", side-1, side-1)
b.Run(fmt.Sprintf("V=%d", side*side), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if ok, _ := g.DijkstraSearch(from, to); !ok {
b.Fatal("expected a path across the grid")
}
}
})
}
}