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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
package ride_test
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/martinlehoux/biking_home/ride"
)
func benchmarkDataDir(tb testing.TB) string {
tb.Helper()
candidates := []string{
filepath.Join("..", "data", "rides"),
filepath.Join("data", "rides"),
filepath.Join(".", "data", "rides"),
}
for _, c := range candidates {
if stat, err := os.Stat(c); err == nil && stat.IsDir() {
abs, _ := filepath.Abs(c)
if abs != "" {
return abs
}
return c
}
}
tb.Fatalf("data/rides directory not found (tried %v)", candidates)
return ""
}
func loadBenchDatas(tb testing.TB, pattern string) ([][]byte, int64) {
tb.Helper()
dir := benchmarkDataDir(tb)
glob := filepath.Join(dir, pattern)
files, err := filepath.Glob(glob)
if err != nil {
tb.Fatalf("glob %s: %v", glob, err)
}
if len(files) == 0 {
tb.Fatalf("no files matching %s", glob)
}
datas := make([][]byte, 0, len(files))
var total int64
for _, f := range files {
data, err := os.ReadFile(f)
if err != nil {
tb.Fatalf("read %s: %v", f, err)
}
if len(data) == 0 {
tb.Fatalf("empty file %s", f)
}
datas = append(datas, data)
total += int64(len(data))
}
return datas, total
}
func filterValidDatas(tb testing.TB, datas [][]byte, parser ride.RideParser) ([][]byte, int64, int) {
tb.Helper()
valid := make([][]byte, 0, len(datas))
var total int64
skipped := 0
for _, data := range datas {
if _, err := parser.Parse(bytes.NewReader(data)); err != nil {
skipped++
continue
}
valid = append(valid, data)
total += int64(len(data))
}
return valid, total, skipped
}
func BenchmarkGPXParse(b *testing.B) {
datas, _ := loadBenchDatas(b, "*.gpx")
if len(datas) == 0 {
b.Skip("no gpx files")
}
parser := ride.GPXRideParser{}
datas, totalBytes, skipped := filterValidDatas(b, datas, parser)
if len(datas) == 0 {
b.Skip("no valid gpx files")
}
if skipped > 0 {
b.Logf("GPX corpus: %d valid, %d skipped (indoor/no track points), total %d bytes (avg %.0f bytes)", len(datas), skipped, totalBytes, float64(totalBytes)/float64(len(datas)))
} else {
b.Logf("GPX corpus: %d files, total %d bytes (avg %.0f bytes)", len(datas), totalBytes, float64(totalBytes)/float64(len(datas)))
}
avgBytes := totalBytes / int64(len(datas))
if avgBytes > 0 {
b.SetBytes(avgBytes)
}
b.ReportAllocs()
b.ResetTimer()
idx := 0
for b.Loop() {
data := datas[idx%len(datas)]
idx++
if _, err := parser.Parse(bytes.NewReader(data)); err != nil {
b.Fatalf("GPX parse failed: %v", err)
}
}
}
func BenchmarkFITParse(b *testing.B) {
datas, totalBytes := loadBenchDatas(b, "*.fit")
if len(datas) == 0 {
b.Skip("no fit files")
}
b.Logf("FIT corpus: %d files, total %d bytes (avg %.0f bytes)", len(datas), totalBytes, float64(totalBytes)/float64(len(datas)))
avgBytes := totalBytes / int64(len(datas))
if avgBytes > 0 {
b.SetBytes(avgBytes)
}
b.ReportAllocs()
b.ResetTimer()
parser := ride.FITRideParser{}
idx := 0
for b.Loop() {
data := datas[idx%len(datas)]
idx++
if _, err := parser.Parse(bytes.NewReader(data)); err != nil {
b.Fatalf("FIT parse failed: %v", err)
}
}
}
func BenchmarkGPXParseAndAnalyze(b *testing.B) {
datas, _ := loadBenchDatas(b, "*.gpx")
if len(datas) == 0 {
b.Skip("no gpx files")
}
parser := ride.GPXRideParser{}
datas, totalBytes, skipped := filterValidDatas(b, datas, parser)
if len(datas) == 0 {
b.Skip("no valid gpx files")
}
if skipped > 0 {
b.Logf("GPX corpus: %d valid, %d skipped, total %d bytes (avg %.0f bytes)", len(datas), skipped, totalBytes, float64(totalBytes)/float64(len(datas)))
} else {
b.Logf("GPX corpus: %d files, total %d bytes (avg %.0f bytes)", len(datas), totalBytes, float64(totalBytes)/float64(len(datas)))
}
avgBytes := totalBytes / int64(len(datas))
if avgBytes > 0 {
b.SetBytes(avgBytes)
}
b.ReportAllocs()
b.ResetTimer()
idx := 0
for b.Loop() {
data := datas[idx%len(datas)]
idx++
r, err := parser.Parse(bytes.NewReader(data))
if err != nil {
b.Fatalf("GPX parse failed: %v", err)
}
_ = ride.MovingTime(r)
_ = r.Cotacol()
}
}
func BenchmarkFITParseAndAnalyze(b *testing.B) {
datas, totalBytes := loadBenchDatas(b, "*.fit")
if len(datas) == 0 {
b.Skip("no fit files")
}
b.Logf("FIT corpus: %d files, total %d bytes (avg %.0f bytes)", len(datas), totalBytes, float64(totalBytes)/float64(len(datas)))
avgBytes := totalBytes / int64(len(datas))
if avgBytes > 0 {
b.SetBytes(avgBytes)
}
b.ReportAllocs()
b.ResetTimer()
parser := ride.FITRideParser{}
idx := 0
for b.Loop() {
data := datas[idx%len(datas)]
idx++
r, err := parser.Parse(bytes.NewReader(data))
if err != nil {
b.Fatalf("FIT parse failed: %v", err)
}
_ = ride.MovingTime(r)
_ = r.Cotacol()
}
}
// BenchmarkGPXParseAll and BenchmarkFITParseAll measure throughput when parsing
// the entire corpus as a single operation, which matches batch import.
func BenchmarkGPXParseAll(b *testing.B) {
datas, _ := loadBenchDatas(b, "*.gpx")
if len(datas) == 0 {
b.Skip("no gpx files")
}
parser := ride.GPXRideParser{}
datas, totalBytes, skipped := filterValidDatas(b, datas, parser)
if len(datas) == 0 {
b.Skip("no valid gpx files")
}
if skipped > 0 {
b.Logf("GPX corpus: %d valid, %d skipped, total %d bytes", len(datas), skipped, totalBytes)
} else {
b.Logf("GPX corpus: %d files, total %d bytes", len(datas), totalBytes)
}
if totalBytes > 0 {
b.SetBytes(totalBytes)
}
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
for _, data := range datas {
if _, err := parser.Parse(bytes.NewReader(data)); err != nil {
b.Fatalf("GPX parse failed: %v", err)
}
}
}
}
func BenchmarkFITParseAll(b *testing.B) {
datas, totalBytes := loadBenchDatas(b, "*.fit")
if len(datas) == 0 {
b.Skip("no fit files")
}
b.Logf("FIT corpus: %d files, total %d bytes", len(datas), totalBytes)
if totalBytes > 0 {
b.SetBytes(totalBytes)
}
b.ReportAllocs()
b.ResetTimer()
parser := ride.FITRideParser{}
for b.Loop() {
for _, data := range datas {
if _, err := parser.Parse(bytes.NewReader(data)); err != nil {
b.Fatalf("FIT parse failed: %v", err)
}
}
}
}
|