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

Function BenchmarkEncode

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

Source from the content-addressed store, hash-verified

34)
35
36func BenchmarkEncode(b *testing.B) {
37 b.ReportAllocs()
38
39 var (
40 buf bytes.Buffer
41
42 article = &model.Article{
43 ID: 1,
44 Title: "Test",
45 Body: "Test",
46 Published: time.Now(),
47 Author: &model.Author{
48 FirstName: "Alice",
49 LastName: "Smith",
50 },
51 }
52
53 query = map[string]interface{}{
54 "query": map[string]interface{}{
55 "match": map[string]interface{}{
56 "title": "test",
57 },
58 },
59 }
60 )
61
62 b.Run("Article - json", func(b *testing.B) {
63 for i := 0; i < b.N; i++ {
64 err := json.NewEncoder(&buf).Encode(article)
65 if err != nil {
66 b.Error(err)
67 }
68 buf.Reset()
69 }
70 })
71
72 b.Run("Article - JSONReader", func(b *testing.B) {
73 for i := 0; i < b.N; i++ {
74 _, err := io.Copy(&buf, esutil.NewJSONReader(article))
75 if err != nil {
76 b.Error(err)
77 }
78 buf.Reset()
79 }
80 })
81
82 b.Run("Article - easyjson", func(b *testing.B) {
83 for i := 0; i < b.N; i++ {
84 _, err := easyjson.MarshalToWriter(article, &buf)
85 if err != nil {
86 b.Error(err)
87 }
88 buf.Reset()
89 }
90 })
91
92 b.Run("map - json", func(b *testing.B) {
93 for i := 0; i < b.N; i++ {

Callers

nothing calls this directly

Calls 3

NewJSONReaderFunction · 0.92
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected