forked from go-openapi/spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
46 lines (40 loc) · 1.21 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
package spec
import (
jsonv1 "encoding/json"
"io"
"net/http"
"reflect"
"testing"
"github.com/dsnet/try"
jsonv2 "github.com/go-json-experiment/json"
"github.com/google/go-cmp/cmp"
)
var content = func() []byte {
resp := try.E1(http.Get("https://raw.githubusercontent.com/kubernetes/kubernetes/38e99289d6179672b3fb38be917ed49669f8b719/api/openapi-spec/swagger.json"))
defer resp.Body.Close()
return try.E1(io.ReadAll(resp.Body))
}()
func init() {
// Sanity check that v1 and v2 behavior are identical.
var v1, v2 Swagger
try.E(jsonv1.Unmarshal(content, &v1))
try.E(jsonv2.Unmarshal(content, &v2))
exportAny := cmp.Exporter(func(reflect.Type) bool { return true })
if diff := cmp.Diff(v1, v2, exportAny); diff != "" {
panic(diff)
}
}
func BenchmarkUnmarshalJSON(b *testing.B) {
b.Run("V1/Swagger", benchmark[Swagger](jsonv1.Unmarshal))
b.Run("V1/Interface", benchmark[any](jsonv1.Unmarshal))
b.Run("V2/Swagger", benchmark[Swagger](jsonv2.Unmarshal))
b.Run("V2/Interface", benchmark[any](jsonv2.Unmarshal))
}
func benchmark[T any](unmarshal func([]byte, any) error) func(b *testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
try.E(unmarshal(content, new(T)))
}
}
}