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 f575167 commit 0b43a3a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 14 deletions.
118 changes: 112 additions & 6 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsoncolor_test

import (
"bytes"
stdj "encoding/json"
"io/ioutil"
"testing"
Expand All @@ -9,6 +10,7 @@ import (
segmentj "github.com/segmentio/encoding/json"

"github.com/neilotoole/jsoncolor"
nwidgerj "github.com/nwidger/jsoncolor"
)

func makeBenchRecs() [][]interface{} {
Expand All @@ -35,7 +37,8 @@ func makeBenchRecs() [][]interface{} {
// - segmentj: the encoder by segment.io
// - jsoncolor: this fork of segmentj that supports color

func BenchmarkStdj(b *testing.B) {
func Benchmark_stdlib_NoIndent(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

Expand All @@ -52,7 +55,8 @@ func BenchmarkStdj(b *testing.B) {
}
}

func BenchmarkStdj_Indent(b *testing.B) {
func Benchmark_stdlib_Indent(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

Expand All @@ -70,7 +74,8 @@ func BenchmarkStdj_Indent(b *testing.B) {
}
}

func BenchmarkSegmentj(b *testing.B) {
func Benchmark_segmentj_NoIndent(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

Expand All @@ -87,7 +92,8 @@ func BenchmarkSegmentj(b *testing.B) {
}
}

func BenchmarkSegmentj_Indent(b *testing.B) {
func Benchmark_segmentj_Indent(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

Expand All @@ -104,13 +110,53 @@ func BenchmarkSegmentj_Indent(b *testing.B) {
}
}
}
func BenchmarkJSONColorEnc(b *testing.B) {

func Benchmark_neilotoolejsoncolor_NoIndent(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
enc := jsoncolor.NewEncoder(ioutil.Discard)
enc.SetEscapeHTML(false)

for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}

func Benchmark_neilotoolejsoncolor_Indent(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
enc := jsoncolor.NewEncoder(ioutil.Discard)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")

for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}

func Benchmark_neilotoolejsoncolor_NoIndent_Color(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
enc := jsoncolor.NewEncoder(ioutil.Discard)
enc.SetEscapeHTML(false)
enc.SetColors(jsoncolor.DefaultColors())

for i := range recs {
err := enc.Encode(recs[i])
Expand All @@ -121,14 +167,37 @@ func BenchmarkJSONColorEnc(b *testing.B) {
}
}

func BenchmarkJSONColor_Indent(b *testing.B) {
func Benchmark_neilotoolejsoncolor_Indent_Color(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
enc := jsoncolor.NewEncoder(ioutil.Discard)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
enc.SetColors(jsoncolor.DefaultColors())

for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}

func Benchmark_nwidgerjsoncolor_Indent_Color(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
buf := &bytes.Buffer{}
b.ResetTimer()

for n := 0; n < b.N; n++ {
f := newNwidgerFormatter()
enc := nwidgerj.NewEncoderWithFormatter(buf, f)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")

for i := range recs {
err := enc.Encode(recs[i])
Expand All @@ -138,3 +207,40 @@ func BenchmarkJSONColor_Indent(b *testing.B) {
}
}
}

func Benchmark_nwidgerjsoncolor_NoIndent_Color(b *testing.B) {
b.ReportAllocs()
recs := makeBenchRecs()
buf := &bytes.Buffer{}
b.ResetTimer()

for n := 0; n < b.N; n++ {
f := newNwidgerFormatter()
enc := nwidgerj.NewEncoderWithFormatter(buf, f)
enc.SetEscapeHTML(false)
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
}

func newNwidgerFormatter() *nwidgerj.Formatter {
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
return f
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.8
github.com/mattn/go-isatty v0.0.14
github.com/nwidger/jsoncolor v0.3.0 // indirect
github.com/segmentio/encoding v0.2.20
github.com/nwidger/jsoncolor v0.3.0
github.com/segmentio/encoding v0.1.14
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/klauspost/cpuid/v2 v2.0.6 h1:dQ5ueTiftKxp0gyjKSx5+8BtPWkyQbd95m8Gys/RarI=
github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -25,10 +23,8 @@ github.com/nwidger/jsoncolor v0.3.0 h1:VdTH8Dc0SJoq4pJ8pRxxFZW0/5Ng5akbN4YToCBJD
github.com/nwidger/jsoncolor v0.3.0/go.mod h1:Cs34umxLbJvgBMnVNVqhji9BhoT/N/KinHqZptQ7cf4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/asm v1.0.0 h1:GMF7/2eLkte13LERSOmPI766AJXJDRlsqqiN8T7Mfmk=
github.com/segmentio/asm v1.0.0/go.mod h1:4EUJGaKsB8ImLUwOGORVsNd9vTRDeh44JGsY4aKp5I4=
github.com/segmentio/encoding v0.2.20 h1:ThEuQ7cvf8iAnZqktmOArOfSodYqIZEKuoe6CdUvcdg=
github.com/segmentio/encoding v0.2.20/go.mod h1:paNNU+VcHW9yV1ErwIsQ6kHjhb3DDemPuvFPo3bgooA=
github.com/segmentio/encoding v0.1.14 h1:BfnglNbNRohLaBLf93uP5/IwKqeWrezXK/g6IRnj75c=
github.com/segmentio/encoding v0.1.14/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
1 change: 1 addition & 0 deletions jsoncolor_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package jsoncolor_test

0 comments on commit 0b43a3a

Please sign in to comment.