MCPcopy
hub / github.com/elastic/go-elasticsearch / BenchmarkDecode

Function BenchmarkDecode

_examples/encoding/benchmark_test.go:113–182  ·  view source on GitHub ↗
(b *testing.B)

Source from the content-addressed store, hash-verified

111}
112
113func BenchmarkDecode(b *testing.B) {
114 b.ReportAllocs()
115
116 resSearch := fixture("testdata/response_search.json")
117 resClusterStats := fixture("testdata/response_cluster_stats.json")
118
119 b.Run("Search - json", func(b *testing.B) {
120 for i := 0; i < b.N; i++ {
121 var res model.SearchResponse
122 err := json.NewDecoder(bytes.NewReader(resSearch.Bytes())).Decode(&res)
123 if err != nil {
124 b.Error(err)
125 }
126 }
127 })
128
129 b.Run("Search - easyjson", func(b *testing.B) {
130 for i := 0; i < b.N; i++ {
131 var res model.SearchResponse
132 err := easyjson.UnmarshalFromReader(bytes.NewReader(resSearch.Bytes()), &res)
133 if err != nil {
134 b.Error(err)
135 }
136 }
137 })
138
139 b.Run("Cluster - json - map", func(b *testing.B) {
140 for i := 0; i < b.N; i++ {
141 var out = make(map[string]interface{})
142 err := json.NewDecoder(bytes.NewReader(resClusterStats.Bytes())).Decode(&out)
143 if err != nil {
144 b.Error(err)
145 }
146 }
147 })
148
149 b.Run("Cluster - json - stc", func(b *testing.B) {
150 type ClusterHealthResponse struct {
151 ClusterName string `json:"cluster_name"`
152 Status string
153 Indices struct {
154 Count int
155 Docs struct {
156 Count int
157 }
158 }
159 }
160
161 for i := 0; i < b.N; i++ {
162 var out ClusterHealthResponse
163 err := json.NewDecoder(bytes.NewReader(resClusterStats.Bytes())).Decode(&out)
164 if err != nil {
165 b.Error(err)
166 }
167 if len(out.ClusterName) < 3 {
168 b.Errorf("Unexpected len(%s)=%d", out.ClusterName, len(out.ClusterName))
169 }
170 }

Callers

nothing calls this directly

Calls 6

fixtureFunction · 0.70
UnmarshalFromReaderMethod · 0.65
RunMethod · 0.45
BytesMethod · 0.45
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected