-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
benchfit_test.go
171 lines (146 loc) · 3.78 KB
/
benchfit_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
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
// Copyright 2024 The FIT SDK for Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package benchfit_test
import (
"bytes"
"encoding/binary"
"io"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/muktihari/fit/decoder"
"github.com/muktihari/fit/encoder"
"github.com/muktihari/fit/profile/filedef"
"github.com/tormoder/fit"
)
var (
_, filename, _, _ = runtime.Caller(0)
cd = filepath.Dir(filename)
testdata = filepath.Join(cd, "..", "..", "..", "testdata")
big_activity = filepath.Join(testdata, "big_activity.fit")
)
func BenchmarkDecode(b *testing.B) {
b.Run("muktihari/fit raw", func(b *testing.B) {
b.StopTimer()
// NOTE: We use os.ReadFile to remove syscall process in our decoding process. So we have pure decoding performance.
f, err := os.ReadFile(big_activity)
if err != nil {
b.Fatalf("open file: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
dec := decoder.New(bytes.NewReader(f))
_, err = dec.Decode()
if err != nil {
b.Fatalf("decode error: %v", err)
}
}
})
b.Run("muktihari/fit", func(b *testing.B) {
b.StopTimer()
f, err := os.ReadFile(big_activity)
if err != nil {
b.Fatalf("open file: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
al := filedef.NewListener()
dec := decoder.New(bytes.NewReader(f),
decoder.WithMesgListener(al),
decoder.WithBroadcastOnly(),
)
_, err = dec.Decode()
if err != nil {
b.Fatalf("decode error: %v", err)
}
_ = al.File()
al.Close()
}
})
b.Run("tormoder/fit", func(b *testing.B) {
b.StopTimer()
f, err := os.ReadFile(big_activity)
if err != nil {
b.Fatalf("open file: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err = fit.Decode(bytes.NewReader(f))
if err != nil {
b.Fatalf("decode error: %v", err)
}
}
})
}
var discard = discardAt{}
type discardAt struct{}
var _ io.Writer = (*discardAt)(nil)
var _ io.WriterAt = (*discardAt)(nil)
func (discardAt) Write(p []byte) (int, error) { return len(p), nil }
func (discardAt) WriteAt(p []byte, off int64) (n int, err error) { return len(p), nil }
func BenchmarkEncode(b *testing.B) {
b.Run("muktihari/fit raw", func(b *testing.B) {
b.StopTimer()
f, err := os.ReadFile(big_activity)
if err != nil {
b.Fatalf("open file: %v", err)
}
dec := decoder.New(bytes.NewReader(f))
fit, err := dec.Decode()
if err != nil {
b.Fatalf("decode error: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
enc := encoder.New(discard) // include the encoder creation.
if err := enc.Encode(fit); err != nil {
b.Fatalf("encode error: %v", err)
}
}
})
b.Run("muktihari/fit", func(b *testing.B) {
b.StopTimer()
f, err := os.ReadFile(big_activity)
if err != nil {
b.Fatalf("open file: %v", err)
}
al := filedef.NewListener()
defer al.Close()
dec := decoder.New(bytes.NewReader(f),
decoder.WithMesgListener(al),
decoder.WithBroadcastOnly(),
)
_, err = dec.Decode()
if err != nil {
b.Fatalf("decode error: %v", err)
}
activity := al.File().(*filedef.Activity)
b.StartTimer()
for i := 0; i < b.N; i++ {
fit := activity.ToFIT(nil)
enc := encoder.New(discard)
if err := enc.Encode(&fit); err != nil {
b.Fatalf("encode error: %v", err)
}
}
})
b.Run("tormoder/fit", func(b *testing.B) {
b.StopTimer()
f, err := os.ReadFile(big_activity)
if err != nil {
b.Fatalf("open file: %v", err)
}
fitFile, err := fit.Decode(bytes.NewReader(f))
if err != nil {
b.Fatalf("decode error: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if err := fit.Encode(discard, fitFile, binary.LittleEndian); err != nil {
b.Fatalf("encode error: %v", err)
}
}
})
}