-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
141 lines (127 loc) · 3.37 KB
/
benchmark_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
package benchmark
import (
"testing"
)
type GetImpl interface {
Get(document interface{}, pointer string) (interface{}, error)
}
type SetImpl interface {
GetImpl
Set(document *interface{}, pointer string, value interface{}) error
}
var implementations = map[string]GetImpl{
"dolmen-go/jsonptr": DolmenGoJsonPtr{},
"xeipuuv/gojsonpointer": XeipuuvGoJsonPointer{},
"mickep76/jsonptr": Mickep76JsonPtr{},
"lestrrat/go-jspointer": LestrratGoJsPointer{},
"dustin/go-jsonpointer": DustinGoJsonPointer{},
"rnd42/go-jsonpointer": Rnd42JSONPointer{},
"twindagger/jsonptr": TwindaggerJSONPtr{},
"qri-io/jsonpointer": QriIoJSONPointer{},
"json-validate/json-pointer-go": JsonValidateJsonPointerGo{},
"go-openapi/jsonpointer": GoOpenAPIJSONPointer{},
"oas3/jsonpointer": OAS3JSONPointer{},
"yukithm/json2csv/jsonpointer": YukithmJSON2CSVJsonPointer{},
}
type PointerParser interface {
Parse(pointer string) (Stringer, error)
}
var parsers = make(map[string]PointerParser, len(implementations))
func init() {
for name, impl := range implementations {
if impl, ok := impl.(PointerParser); ok {
parsers[name] = impl
}
}
}
func BenchmarkGet(b *testing.B) {
doc := map[string]interface{}{
"foo": map[string]interface{}{
"bar": []interface{}{
true,
nil,
false,
},
},
}
const ptr = "/foo/bar/2"
for name, impl := range implementations {
// Check the implementation
res, err := impl.Get(doc, ptr)
if err != nil {
b.FailNow()
}
if res != false {
b.FailNow()
}
b.Run("["+name+"]", func(b *testing.B) {
for i := 0; i < b.N; i++ {
res, err = impl.Get(doc, ptr)
}
})
}
}
var pointers = []string{
// "/foo/bar",
// "/foo/bar/baz/~0~1",
"/definitions/Location",
"/path/~1home~1dolmen",
"/path/~0~1dolmen",
}
func benchmarkParsers(b *testing.B, f func(func(string) (Stringer, error), string) func(*testing.B)) {
for _, pointer := range pointers {
b.Run(`"`+pointer+`"`, func(b *testing.B) {
for name, p := range parsers {
f2 := f(p.Parse, pointer)
if f2 == nil {
continue
}
b.Run("["+name+"]", func(b *testing.B) {
for i := 0; i < b.N; i++ {
f2(b)
}
})
}
})
}
}
func BenchmarkParse(b *testing.B) {
benchmarkParsers(b, func(Parse func(string) (Stringer, error), pointer string) func(*testing.B) {
// Returns the body of the benchmark: parsing the given pointer
return func(*testing.B) {
_, _ = Parse(pointer)
}
})
}
func BenchmarkBackToString(b *testing.B) {
benchmarkParsers(b, func(Parse func(string) (Stringer, error), pointer string) func(*testing.B) {
// Parse the pointer, but before starting the benchmark
ptr, err := Parse(pointer)
if err != nil {
b.FailNow()
}
// Returns the body of the benchmark: formatting the given pointer
return func(b *testing.B) {
out := ptr.String()
if out != pointer {
b.FailNow()
}
}
})
}
func BenchmarkParseAndBackToString(b *testing.B) {
benchmarkParsers(b, func(Parse func(string) (Stringer, error), pointer string) func(*testing.B) {
// Parse the pointer, but before starting the benchmark
// Returns the body of the benchmark: formatting the given pointer
return func(b *testing.B) {
ptr, err := Parse(pointer)
if err != nil {
b.FailNow()
}
out := ptr.String()
if out != pointer {
b.FailNow()
}
}
})
}