Skip to content

Commit

Permalink
test: Add segmention/encoding/json
Browse files Browse the repository at this point in the history
  • Loading branch information
romshark committed Jan 26, 2024
1 parent 987e0a8 commit c7405c5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 23 deletions.
32 changes: 18 additions & 14 deletions bench/array_2d_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
easyjson "github.com/mailru/easyjson"
ffjson "github.com/pquerna/ffjson/ffjson"
jscan "github.com/romshark/jscan/v2"
segmentio "github.com/segmentio/encoding/json"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -68,6 +69,12 @@ func TestImplementationsDecode2DArrayBool(t *testing.T) {
require.Equal(t, expect(), v)
})

t.Run("segmention", func(t *testing.T) {
var v [][]bool
require.NoError(t, segmentio.Unmarshal([]byte(in), &v))
require.Equal(t, expect(), v)
})

t.Run("jscan/decoder", func(t *testing.T) {
d := jscandec.NewDecoder[[]byte, [][]bool](jscan.NewTokenizer[[]byte](2048, 2048*1024))
var v [][]bool
Expand All @@ -87,7 +94,7 @@ func TestImplementationsDecode2DArrayBool(t *testing.T) {

t.Run("jscan/handwritten", func(t *testing.T) {
tokenizer := jscan.NewTokenizer[[]byte](2048, 2048*1024)
v, err := bench.DecodeArray2DCustomParser(tokenizer, []byte(in))
v, err := bench.JscanBoolMatrix(tokenizer, []byte(in))
require.NoError(t, err)
require.Equal(t, expect(), v)
})
Expand Down Expand Up @@ -166,6 +173,15 @@ func BenchmarkDecode2DArrayBool(b *testing.B) {
}
})

b.Run("segmention", func(b *testing.B) {
for n := 0; n < b.N; n++ {
var v [][]bool
if err := segmentio.Unmarshal(in, &v); err != nil {
b.Fatal(err)
}
}
})

b.Run("jscan/decoder", func(b *testing.B) {
tokenizer := jscan.NewTokenizer[[]byte](2048, 2048*1024)
d := jscandec.NewDecoder[[]byte, [][]bool](tokenizer)
Expand Down Expand Up @@ -193,22 +209,10 @@ func BenchmarkDecode2DArrayBool(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
var err error
if v, err = bench.DecodeArray2DCustomParser(tokenizer, in); err != nil {
if v, err = bench.JscanBoolMatrix(tokenizer, in); err != nil {
b.Fatal(err)
}
}
runtime.KeepAlive(v)
})

// b.Run("jscan2", func(b *testing.B) {
// tokenizer := jscan.NewTokenizer[[]byte](2048, 2048*1024)
// d := NewDecoder2[[]byte, [][]bool](tokenizer)
// b.ResetTimer()
// for n := 0; n < b.N; n++ {
// var v [][]bool
// if err := d.Decode(in, &v); err.IsErr() {
// b.Fatal(err)
// }
// }
// })
}
20 changes: 18 additions & 2 deletions bench/array_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

jscandec "github.com/romshark/jscan-experimental-decoder"
"github.com/romshark/jscan-experimental-decoder/bench"
segmentio "github.com/segmentio/encoding/json"

jsonv2 "github.com/go-json-experiment/json"
goccy "github.com/goccy/go-json"
Expand Down Expand Up @@ -76,6 +77,12 @@ func TestImplementationsDecodeArrayInt(t *testing.T) {
require.Equal(t, expect, v)
})

t.Run("segmentio", func(t *testing.T) {
var v []int
require.NoError(t, segmentio.Unmarshal([]byte(in), &v))
require.Equal(t, expect, v)
})

t.Run("jscan", func(t *testing.T) {
d := jscandec.NewDecoder[[]byte, []int](jscan.NewTokenizer[[]byte](2048, 2048*1024))
var v []int
Expand All @@ -96,7 +103,7 @@ func TestImplementationsDecodeArrayInt(t *testing.T) {

t.Run("jscantok", func(t *testing.T) {
tokenizer := jscan.NewTokenizer[[]byte](2048, 2048*1024)
v, err := bench.DecodeArrayIntCustomParser(tokenizer, []byte(in))
v, err := bench.JscanIntSlice(tokenizer, []byte(in))
require.NoError(t, err)
require.Equal(t, expect, v)
})
Expand Down Expand Up @@ -189,6 +196,15 @@ func BenchmarkDecodeArrayInt12K(b *testing.B) {
}
})

b.Run("segmentio", func(b *testing.B) {
for n := 0; n < b.N; n++ {
var v []int
if err := segmentio.Unmarshal(in, &v); err != nil {
b.Fatal(err)
}
}
})

b.Run("jscan_decoder", func(b *testing.B) {
tokenizer := jscan.NewTokenizer[[]byte](2048, 2048*1024)
d := jscandec.NewDecoder[[]byte, []int](tokenizer)
Expand Down Expand Up @@ -216,7 +232,7 @@ func BenchmarkDecodeArrayInt12K(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
var err error
if v, err = bench.DecodeArrayIntCustomParser(tokenizer, in); err != nil {
if v, err = bench.JscanIntSlice(tokenizer, in); err != nil {
b.Fatal(err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions bench/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/valyala/fastjson"
)

func DecodeArray2DCustomParser[S []byte | string](
func JscanBoolMatrix[S []byte | string](
t *jscan.Tokenizer[S], str S,

Check failure on line 15 in bench/bench.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jscan (typecheck)
) (s [][]bool, err error) {
errk := t.Tokenize(str, func(tokens []jscan.Token[S]) bool {
Expand Down Expand Up @@ -56,7 +56,7 @@ func DecodeArray2DCustomParser[S []byte | string](
return s, nil
}

func DecodeArrayIntCustomParser[S []byte | string](
func JscanIntSlice[S []byte | string](
t *jscan.Tokenizer[S], str S,

Check failure on line 60 in bench/bench.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jscan (typecheck)
) (s []int, err error) {
var sz S
Expand Down Expand Up @@ -128,7 +128,7 @@ func JscanMapStringString[S []byte | string](
return m, nil
}

func JscanDecodeStruct3[S []byte | string](
func JscanStruct3[S []byte | string](
t *jscan.Tokenizer[S], src S,
) (s Struct3, err error) {
errk := t.Tokenize(src, func(tokens []jscan.Token[S]) bool {
Expand Down
16 changes: 16 additions & 0 deletions bench/map_string_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

jscandec "github.com/romshark/jscan-experimental-decoder"
"github.com/romshark/jscan-experimental-decoder/bench"
segmentio "github.com/segmentio/encoding/json"

jsonv2 "github.com/go-json-experiment/json"
goccy "github.com/goccy/go-json"
Expand Down Expand Up @@ -68,6 +69,12 @@ func TestImplementationsMapStringString(t *testing.T) {
require.Equal(t, expect(), v)
})

t.Run("segmentio", func(t *testing.T) {
var v map[string]string
require.NoError(t, segmentio.Unmarshal([]byte(in), &v))
require.Equal(t, expect(), v)
})

t.Run("jscan/decoder", func(t *testing.T) {
d := jscandec.NewDecoder[[]byte, map[string]string](jscan.NewTokenizer[[]byte](2048, 2048*1024))
var v map[string]string
Expand Down Expand Up @@ -166,6 +173,15 @@ func BenchmarkDecodeMapStringString(b *testing.B) {
}
})

b.Run("segmentio", func(b *testing.B) {
for n := 0; n < b.N; n++ {
var v map[string]string
if err := segmentio.Unmarshal(in, &v); err != nil {
b.Fatal(err)
}
}
})

b.Run("jscan/decoder", func(b *testing.B) {
tokenizer := jscan.NewTokenizer[[]byte](2048, 2048*1024)
d := jscandec.NewDecoder[[]byte, map[string]string](tokenizer)
Expand Down
21 changes: 19 additions & 2 deletions bench/struct3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

jscandec "github.com/romshark/jscan-experimental-decoder"
"github.com/romshark/jscan-experimental-decoder/bench"
segmentio "github.com/segmentio/encoding/json"

jsonv2 "github.com/go-json-experiment/json"
goccy "github.com/goccy/go-json"
Expand Down Expand Up @@ -79,6 +80,12 @@ func TestImplementationsStruct3(t *testing.T) {
require.Equal(t, expect(), v)
})

t.Run("segmentio", func(t *testing.T) {
var v bench.Struct3
require.NoError(t, segmentio.Unmarshal([]byte(in), &v))
require.Equal(t, expect(), v)
})

t.Run("jscan/decoder", func(t *testing.T) {
d := jscandec.NewDecoder[[]byte, bench.Struct3](
jscan.NewTokenizer[[]byte](32, len(in)/2),
Expand All @@ -100,7 +107,7 @@ func TestImplementationsStruct3(t *testing.T) {

t.Run("jscan/handwritten", func(t *testing.T) {
tokenizer := jscan.NewTokenizer[[]byte](8, len(in)/2)
v, err := bench.JscanDecodeStruct3(tokenizer, []byte(in))
v, err := bench.JscanStruct3(tokenizer, []byte(in))
require.NoError(t, err)
require.Equal(t, expect(), v)
})
Expand Down Expand Up @@ -191,6 +198,16 @@ func BenchmarkDecodeStruct3(b *testing.B) {
}
})

b.Run("segmentio", func(b *testing.B) {
var v bench.Struct3
b.ResetTimer()
for n := 0; n < b.N; n++ {
if err := segmentio.Unmarshal(in, &v); err != nil {
b.Fatal(err)
}
}
})

b.Run("jscan/decoder", func(b *testing.B) {
tokenizer := jscan.NewTokenizer[[]byte](32, 128)
d := jscandec.NewDecoder[[]byte, bench.Struct3](tokenizer)
Expand Down Expand Up @@ -219,7 +236,7 @@ func BenchmarkDecodeStruct3(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
var err error
if v, err = bench.JscanDecodeStruct3(tokenizer, in); err != nil {
if v, err = bench.JscanStruct3(tokenizer, in); err != nil {
b.Fatal(err)
}
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mailru/easyjson v0.7.7
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7
github.com/romshark/jscan/v2 v2.0.2
github.com/segmentio/encoding v0.4.0
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.0
github.com/valyala/fastjson v1.6.4
Expand All @@ -24,7 +25,9 @@ require (
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/asm v1.1.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/romshark/jscan/v2 v2.0.0-20240125233239-f1e817ac892f h1:JMVxmm71jArAbflFQ9FlyDrG0Zxd+lsFBpXKqG8fiOU=
github.com/romshark/jscan/v2 v2.0.0-20240125233239-f1e817ac892f/go.mod h1:H+vA/18JDD4ifVMk69hoMbyRVs+f0fKEIgC1e0y+af8=
github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
github.com/segmentio/encoding v0.4.0 h1:MEBYvRqiUB2nfR2criEXWqwdY6HJOUrCn5hboVOVmy8=
github.com/segmentio/encoding v0.4.0/go.mod h1:/d03Cd8PoaDeceuhUUUQWjU0KhWjrmYrWPgtJHYZSnI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand All @@ -42,6 +44,8 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 h1:WecRHqgE09JBkh/584XIE6PMz5KKE/vER4izNUi30AQ=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down

0 comments on commit c7405c5

Please sign in to comment.