sq/cli/output/jsonw/internal/benchmark_test.go

127 lines
2.4 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package internal_test
import (
stdj "encoding/json"
2022-12-18 02:11:33 +03:00
"io"
2020-08-06 20:58:47 +03:00
"testing"
segmentj "github.com/segmentio/encoding/json"
jcolorenc "github.com/neilotoole/sq/cli/output/jsonw/internal/jcolorenc"
"github.com/neilotoole/sq/testh"
"github.com/neilotoole/sq/testh/sakila"
)
// The following benchmarks compare the encoding performance
// of JSON encoders. These are:
//
// - stdj: the std lib json encoder
// - segmentj: the encoder by segment.io
// - jcolorenc: sq's fork of segmentj that supports color
func BenchmarkStdj(b *testing.B) {
_, recs := testh.RecordsFromTbl(b, sakila.SL3, "payment")
b.ResetTimer()
for n := 0; n < b.N; n++ {
2022-12-18 02:11:33 +03:00
enc := stdj.NewEncoder(io.Discard)
2020-08-06 20:58:47 +03:00
enc.SetEscapeHTML(false)
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}
func BenchmarkStdj_Indent(b *testing.B) {
_, recs := testh.RecordsFromTbl(b, sakila.SL3, "payment")
b.ResetTimer()
for n := 0; n < b.N; n++ {
2022-12-18 02:11:33 +03:00
enc := stdj.NewEncoder(io.Discard)
2020-08-06 20:58:47 +03:00
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}
func BenchmarkSegmentj(b *testing.B) {
_, recs := testh.RecordsFromTbl(b, sakila.SL3, "payment")
b.ResetTimer()
for n := 0; n < b.N; n++ {
2022-12-18 02:11:33 +03:00
enc := segmentj.NewEncoder(io.Discard)
2020-08-06 20:58:47 +03:00
enc.SetEscapeHTML(false)
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}
func BenchmarkSegmentj_Indent(b *testing.B) {
_, recs := testh.RecordsFromTbl(b, sakila.SL3, "payment")
b.ResetTimer()
for n := 0; n < b.N; n++ {
2022-12-18 02:11:33 +03:00
enc := segmentj.NewEncoder(io.Discard)
2020-08-06 20:58:47 +03:00
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}
2020-08-06 20:58:47 +03:00
func BenchmarkJColorEnc(b *testing.B) {
_, recs := testh.RecordsFromTbl(b, sakila.SL3, "payment")
b.ResetTimer()
for n := 0; n < b.N; n++ {
2022-12-18 02:11:33 +03:00
enc := jcolorenc.NewEncoder(io.Discard)
2020-08-06 20:58:47 +03:00
enc.SetEscapeHTML(false)
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}
func BenchmarkJColorEnc_Indent(b *testing.B) {
_, recs := testh.RecordsFromTbl(b, sakila.SL3, "payment")
b.ResetTimer()
for n := 0; n < b.N; n++ {
2022-12-18 02:11:33 +03:00
enc := jcolorenc.NewEncoder(io.Discard)
2020-08-06 20:58:47 +03:00
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}