Skip to content

Commit

Permalink
wip: refactoring benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
neilotoole committed Oct 3, 2021
1 parent 0b43a3a commit f04c95a
Showing 1 changed file with 122 additions and 17 deletions.
139 changes: 122 additions & 17 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsoncolor_test
import (
"bytes"
stdj "encoding/json"
"io"
"io/ioutil"
"testing"
"time"
Expand All @@ -13,21 +14,23 @@ import (
nwidgerj "github.com/nwidger/jsoncolor"
)

func makeBenchRecs() [][]interface{} {
const maxRecs = 20000
recs := make([][]interface{}, 0, maxRecs)
for i := 0; i < maxRecs; i++ {
rec := []interface{}{
1,
2,
"3.00",
true,
time.Unix(1631659220, 0),
}
recs = append(recs, rec)
func BenchmarkEncoder_Encode(b *testing.B) {
benchmarks := []struct {
name string
indent bool
color bool
fn newEncoderFunc
}{
{name: "stdlib_no_indent", fn: newEncStdlib},
}

for _, bm := range benchmarks {
bm := bm
b.Run(bm.name, func(b *testing.B) {

})
}

return recs
}

// The following benchmarks compare the encoding performance
Expand All @@ -36,7 +39,6 @@ func makeBenchRecs() [][]interface{} {
// - stdj: the std lib json encoder
// - segmentj: the encoder by segment.io
// - jsoncolor: this fork of segmentj that supports color

func Benchmark_stdlib_NoIndent(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
Expand Down Expand Up @@ -194,7 +196,7 @@ func Benchmark_nwidgerjsoncolor_Indent_Color(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
f := newNwidgerFormatter()
f := newNwidgerColorFormatter()
enc := nwidgerj.NewEncoderWithFormatter(buf, f)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
Expand All @@ -215,7 +217,7 @@ func Benchmark_nwidgerjsoncolor_NoIndent_Color(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
f := newNwidgerFormatter()
f := newNwidgerColorFormatter()
enc := nwidgerj.NewEncoderWithFormatter(buf, f)
enc.SetEscapeHTML(false)
for i := range recs {
Expand All @@ -227,7 +229,7 @@ func Benchmark_nwidgerjsoncolor_NoIndent_Color(b *testing.B) {
}
}

func newNwidgerFormatter() *nwidgerj.Formatter {
func newNwidgerColorFormatter() *nwidgerj.Formatter {
f := nwidgerj.NewFormatter()
f.SpaceColor = nwidgerj.DefaultSpaceColor
f.CommaColor = nwidgerj.DefaultCommaColor
Expand All @@ -244,3 +246,106 @@ func newNwidgerFormatter() *nwidgerj.Formatter {
f.NullColor = nwidgerj.DefaultNullColor
return f
}

func makeBenchRecs() [][]interface{} {
const maxRecs = 20000
recs := make([][]interface{}, 0, maxRecs)
for i := 0; i < maxRecs; i++ {
rec := []interface{}{
1,
3.14,
"6.77",
true,
false,
time.Unix(1631659220, 0),
time.Millisecond * 1631659220,
}
recs = append(recs, rec)
}

return recs
}

type newEncoderFunc func(w io.Writer, indent, color bool) encoder

var (
_ newEncoderFunc = newEncStdlib
_ newEncoderFunc = newEncSegmentj
_ newEncoderFunc = newEncNeilotoole
_ newEncoderFunc = newEncNwidger
)

type encoder interface {
SetEscapeHTML(on bool)
SetIndent(prefix, indent string)
Encode(v interface{}) error
}

func newEncStdlib(w io.Writer, indent, color bool) encoder {
enc := stdj.NewEncoder(w)
if indent {
enc.SetIndent("", " ")
}
enc.SetEscapeHTML(true)
return enc
}

func newEncSegmentj(w io.Writer, indent, color bool) encoder {
enc := stdj.NewEncoder(w)
if indent {
enc.SetIndent("", " ")
}
enc.SetEscapeHTML(true)
return enc
}

func newEncNeilotoole(w io.Writer, indent, color bool) encoder {
enc := jsoncolor.NewEncoder(w)
if indent {
enc.SetIndent("", " ")
}
enc.SetEscapeHTML(true)

if color {
clrs := jsoncolor.DefaultColors()
enc.SetColors(clrs)
}

return enc
}

func newEncNwidger(w io.Writer, indent, color bool) encoder {
if !color {
enc := nwidgerj.NewEncoder(w)
enc.SetEscapeHTML(false)
if indent {
enc.SetIndent("", " ")
}
return enc
}

// It's color
f := nwidgerj.NewFormatter()
f.SpaceColor = nwidgerj.DefaultSpaceColor
f.CommaColor = nwidgerj.DefaultCommaColor
f.ColonColor = nwidgerj.DefaultColonColor
f.ObjectColor = nwidgerj.DefaultObjectColor
f.ArrayColor = nwidgerj.DefaultArrayColor
f.FieldQuoteColor = nwidgerj.DefaultFieldQuoteColor
f.FieldColor = nwidgerj.DefaultFieldColor
f.StringQuoteColor = nwidgerj.DefaultStringQuoteColor
f.StringColor = nwidgerj.DefaultStringColor
f.TrueColor = nwidgerj.DefaultTrueColor
f.FalseColor = nwidgerj.DefaultFalseColor
f.NumberColor = nwidgerj.DefaultNumberColor
f.NullColor = nwidgerj.DefaultNullColor

enc := nwidgerj.NewEncoderWithFormatter(w, f)
enc.SetEscapeHTML(false)

if indent {
enc.SetIndent("", " ")
}

return enc
}

0 comments on commit f04c95a

Please sign in to comment.