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

Function BenchmarkJSONReader

esutil/json_reader_benchmark_test.go:53–109  ·  view source on GitHub ↗
(b *testing.B)

Source from the content-addressed store, hash-verified

51}
52
53func BenchmarkJSONReader(b *testing.B) {
54 b.ReportAllocs()
55
56 b.Run("None", func(b *testing.B) {
57 b.ResetTimer()
58
59 var buf bytes.Buffer
60 for i := 0; i < b.N; i++ {
61 _ = json.NewEncoder(&buf).Encode(map[string]string{"foo": "bar"})
62 if buf.String() != `{"foo":"bar"}`+"\n" {
63 b.Fatalf("Unexpected output: %q", buf.String())
64 }
65 buf.Reset()
66 }
67 })
68
69 b.Run("Default", func(b *testing.B) {
70 b.ResetTimer()
71
72 for i := 0; i < b.N; i++ {
73 out, err := io.ReadAll(esutil.NewJSONReader(map[string]string{"foo": "bar"}))
74 if err != nil {
75 b.Fatalf("Unexpected error: %s", err)
76 }
77 if string(out) != `{"foo":"bar"}`+"\n" {
78 b.Fatalf("Unexpected output: %q", out)
79 }
80 }
81 })
82
83 b.Run("Default-Copy", func(b *testing.B) {
84 b.ResetTimer()
85
86 var buf bytes.Buffer
87 for i := 0; i < b.N; i++ {
88 _, _ = io.Copy(&buf, esutil.NewJSONReader(map[string]string{"foo": "bar"}))
89 if buf.String() != `{"foo":"bar"}`+"\n" {
90 b.Fatalf("Unexpected output: %q", buf.String())
91 }
92 buf.Reset()
93 }
94 })
95
96 b.Run("Custom", func(b *testing.B) {
97 b.ResetTimer()
98
99 for i := 0; i < b.N; i++ {
100 out, err := io.ReadAll(esutil.NewJSONReader(Foo{Bar: "baz"}))
101 if err != nil {
102 b.Fatalf("Unexpected error: %s", err)
103 }
104 if string(out) != `{"bar":"BAZ"}`+"\n" {
105 b.Fatalf("Unexpected output: %q", out)
106 }
107 }
108 })
109}

Callers

nothing calls this directly

Calls 3

NewJSONReaderFunction · 0.92
RunMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected