-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
146 lines (119 loc) · 3.13 KB
/
bench_test.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package gtfs_test
import (
"context"
"os"
"testing"
"time"
"tidbyt.dev/gtfs"
"tidbyt.dev/gtfs/testutil"
)
func benchNearbyStops(b *testing.B, backend string) {
static := testutil.LoadStaticFile(b, backend, "testdata/caltrain_20160406.zip")
b.ResetTimer()
for i := 0; i < b.N; i++ {
// The 20 nearest stops for 544 Park Ave, BK
_, err := static.NearbyStops(40.6968986, -73.955555, 20, nil)
if err != nil {
b.Error(err)
}
}
}
func benchRouteDirections(b *testing.B, backend string) {
static := testutil.LoadStaticFile(b, backend, "testdata/caltrain_20160406.zip")
stops, err := static.NearbyStops(40.734673, -73.989951, 0, nil)
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := static.RouteDirections(stops[i%len(stops)].ID)
if err != nil {
b.Error(err)
}
}
}
func benchStaticDepartures(b *testing.B, backend string) {
static := testutil.LoadStaticFile(b, backend, "testdata/bart_static.zip")
tz, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
b.Error(err)
}
when := time.Date(2024, 1, 4, 11, 26, 42, 0, tz)
window := 1 * time.Hour
stops, err := static.NearbyStops(40.734673, -73.989951, 0, nil)
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
stopID := stops[i%len(stops)].ID
_, err := static.Departures(stopID, when, window, -1, "", -1, nil)
if err != nil {
b.Error(err)
}
}
}
func benchRealtimeLoad(b *testing.B, backend string) {
static := testutil.LoadStaticFile(b, backend, "testdata/bart_static.zip")
buf, err := os.ReadFile("testdata/bart_20240104T142509.pb")
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := gtfs.NewRealtime(context.Background(), static, [][]byte{buf})
if err != nil {
b.Error(err)
}
}
}
func benchRealtimeDepartures(b *testing.B, backend string) {
static := testutil.LoadStaticFile(b, backend, "testdata/bart_static.zip")
buf, err := os.ReadFile("testdata/bart_20240104T142509.pb")
if err != nil {
b.Error(err)
}
rt, err := gtfs.NewRealtime(context.Background(), static, [][]byte{buf})
if err != nil {
b.Error(err)
}
stops, err := static.NearbyStops(40.734673, -73.989951, 0, nil)
if err != nil {
b.Error(err)
}
tz, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
b.Error(err)
}
when := time.Date(2024, 1, 4, 11, 26, 42, 0, tz)
window := 30 * time.Minute
b.ResetTimer()
for i := 0; i < b.N; i++ {
stopID := stops[i%len(stops)].ID
_, err := rt.Departures(stopID, when, window, -1, "", -1, nil)
if err != nil {
b.Error(err)
}
}
}
func BenchmarkGTFSStatic(b *testing.B) {
for _, test := range []struct {
Name string
Bench func(b *testing.B, storage string)
}{
{"NearbyStops", benchNearbyStops},
{"RouteDirections", benchRouteDirections},
{"StaticDepartures", benchStaticDepartures},
{"RealtimeLoad", benchRealtimeLoad},
{"RealtimeDepartures", benchRealtimeDepartures},
} {
b.Run(test.Name+"_sqlite", func(b *testing.B) {
test.Bench(b, "sqlite")
})
if testutil.PostgresConnStr != "" {
b.Run(test.Name+"_postgres", func(b *testing.B) {
test.Bench(b, "postgres")
})
}
}
}