Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] experiment with sonic. v2 #325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/bavix/gripmock-sdk-go v1.0.4
github.com/bavix/gripmock-ui v1.0.0-alpha5
github.com/bavix/gripmock/protogen v0.0.0-20240706201937-fc1e72a8ad5f
github.com/bytedance/sonic v1.10.0-rc3
github.com/cristalhq/base64 v0.1.2
github.com/goccy/go-yaml v1.11.3
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -38,6 +39,8 @@ require (
github.com/bufbuild/protocompile v0.14.0 // indirect
github.com/caarlos0/env/v10 v10.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
Expand All @@ -46,13 +49,16 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/gripmock/deeply v1.0.9 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
Expand Down
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ github.com/bavix/gripmock/protogen v0.0.0-20240706201937-fc1e72a8ad5f/go.mod h1:
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/bufbuild/protocompile v0.14.0 h1:z3DW4IvXE5G/uTOnSQn+qwQQxvhckkTWLS/0No/o7KU=
github.com/bufbuild/protocompile v0.14.0/go.mod h1:N6J1NYzkspJo3ZwyL4Xjvli86XOj1xq4qAasUFxGups=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.0-rc3 h1:uNSnscRapXTwUgTyOF0GVljYD08p9X/Lbr9MweSV3V0=
github.com/bytedance/sonic v1.10.0-rc3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cristalhq/base64 v0.1.2 h1:edsefYyYDiac7Ytdh2xdaiiSSJzcI2f0yIkdGEf1qY0=
github.com/cristalhq/base64 v0.1.2/go.mod h1:sy4+2Hale2KbtSqkzpdMeYTP/IrB+HCvxVHWsh2VSYk=
Expand Down Expand Up @@ -71,6 +81,10 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737
github.com/jhump/protoreflect v1.16.0 h1:54fZg+49widqXYQ0b+usAFHbMkBGR4PpXrsHc8+TBDg=
github.com/jhump/protoreflect v1.16.0/go.mod h1:oYPd7nPvcBw/5wlDfm/AVmU9zH9BgqGCI469pGxfj/8=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -96,9 +110,17 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.53.0 h1:KHTx4DmXkuhl/a4/jU5eDMrPuxulzd7m8nusORJ64Fc=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.53.0/go.mod h1:Orsflew5fQlsj8qLxP5A9Y38PGaRxXs93TGaDHDwGT0=
Expand All @@ -122,6 +144,9 @@ go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeX
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc=
golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
Expand Down Expand Up @@ -198,5 +223,8 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
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=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
24 changes: 12 additions & 12 deletions internal/app/rest_server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -13,6 +12,7 @@ import (
"sync/atomic"
"time"

"github.com/bytedance/sonic/encoder"
"github.com/google/uuid"
"github.com/gripmock/stuber"
"golang.org/x/text/cases"
Expand Down Expand Up @@ -86,7 +86,7 @@ func (h *RestServer) ServicesList(w http.ResponseWriter, r *http.Request) {
}
}

if err := json.NewEncoder(w).Encode(results); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(results); err != nil {
h.responseError(err, w)
}
}
Expand All @@ -105,13 +105,13 @@ func (h *RestServer) ServiceMethodsList(w http.ResponseWriter, r *http.Request,
}
}

if err := json.NewEncoder(w).Encode(results); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(results); err != nil {
h.responseError(err, w)
}
}

func (h *RestServer) Liveness(w http.ResponseWriter, _ *http.Request) {
if err := json.NewEncoder(w).Encode(rest.MessageOK{Message: "ok", Time: time.Now()}); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(rest.MessageOK{Message: "ok", Time: time.Now()}); err != nil {
h.responseError(err, w)
}
}
Expand All @@ -125,7 +125,7 @@ func (h *RestServer) Readiness(w http.ResponseWriter, _ *http.Request) {

w.Header().Set("Content-Type", "application/json")

if err := json.NewEncoder(w).Encode(rest.MessageOK{Message: "ok", Time: time.Now()}); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(rest.MessageOK{Message: "ok", Time: time.Now()}); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func (h *RestServer) AddStub(w http.ResponseWriter, r *http.Request) {
}
}

if err := json.NewEncoder(w).Encode(h.stuber.PutMany(inputs...)); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(h.stuber.PutMany(inputs...)); err != nil {
h.responseError(err, w)

return
Expand Down Expand Up @@ -200,23 +200,23 @@ func (h *RestServer) BatchStubsDelete(w http.ResponseWriter, r *http.Request) {
func (h *RestServer) ListUsedStubs(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")

if err := json.NewEncoder(w).Encode(h.stuber.Used()); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(h.stuber.Used()); err != nil {
h.responseError(err, w)
}
}

func (h *RestServer) ListUnusedStubs(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")

if err := json.NewEncoder(w).Encode(h.stuber.Unused()); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(h.stuber.Unused()); err != nil {
h.responseError(err, w)
}
}

func (h *RestServer) ListStubs(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")

if err := json.NewEncoder(w).Encode(h.stuber.All()); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(h.stuber.All()); err != nil {
h.responseError(err, w)
}
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func (h *RestServer) SearchStubs(w http.ResponseWriter, r *http.Request) {
return
}

if err := json.NewEncoder(w).Encode(result.Found().Output); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(result.Found().Output); err != nil {
h.responseError(err, w)
}
}
Expand All @@ -267,7 +267,7 @@ func (h *RestServer) FindByID(w http.ResponseWriter, _ *http.Request, uuid rest.
return
}

if err := json.NewEncoder(w).Encode(stub); err != nil {
if err := encoder.NewStreamEncoder(w).Encode(stub); err != nil {
h.responseError(err, w)
}
}
Expand All @@ -279,7 +279,7 @@ func (h *RestServer) responseError(err error, w http.ResponseWriter) {
}

func (h *RestServer) writeResponseError(err error, w http.ResponseWriter) {
if err := json.NewEncoder(w).Encode(map[string]string{
if err := encoder.NewStreamEncoder(w).Encode(map[string]string{
"error": err.Error(),
}); err != nil {
h.responseError(err, w)
Expand Down
10 changes: 5 additions & 5 deletions internal/app/storage.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package app

import (
"encoding/json"
"fmt"

"github.com/bytedance/sonic"
"github.com/gripmock/stuber"
)

func stubNotFoundError2(expect stuber.Query, result *stuber.Result) error {
template := fmt.Sprintf("Can't find stub \n\nService: %s \n\nMethod: %s \n\nInput\n\n", expect.Service, expect.Method)

expectString, err := json.MarshalIndent(expect.Data, "", "\t")
expectString, err := sonic.ConfigFastest.MarshalIndent(expect.Data, "", "\t")
if err != nil {
return err
}
Expand All @@ -24,7 +24,7 @@ func stubNotFoundError2(expect stuber.Query, result *stuber.Result) error {
}

if len(result.Similar().Input.Equals) > 0 {
closestMatchString, err := json.MarshalIndent(result.Similar().Input.Equals, "", "\t")
closestMatchString, err := sonic.ConfigFastest.MarshalIndent(result.Similar().Input.Equals, "", "\t")
if err != nil {
return err
}
Expand All @@ -33,7 +33,7 @@ func stubNotFoundError2(expect stuber.Query, result *stuber.Result) error {
}

if len(result.Similar().Input.Contains) > 0 {
closestMatchString, err := json.MarshalIndent(result.Similar().Input.Contains, "", "\t")
closestMatchString, err := sonic.ConfigFastest.MarshalIndent(result.Similar().Input.Contains, "", "\t")
if err != nil {
return err
}
Expand All @@ -42,7 +42,7 @@ func stubNotFoundError2(expect stuber.Query, result *stuber.Result) error {
}

if len(result.Similar().Input.Matches) > 0 {
closestMatchString, err := json.MarshalIndent(result.Similar().Input.Matches, "", "\t")
closestMatchString, err := sonic.ConfigFastest.MarshalIndent(result.Similar().Input.Matches, "", "\t")
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/jsondecoder/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package jsondecoder
import (
"bytes"
"encoding/json"

"github.com/bytedance/sonic/decoder"
)

const minJSONLength = 2
Expand Down Expand Up @@ -44,8 +46,8 @@ func UnmarshalSlice(data []byte, v interface{}) error {
input = append(append([]byte{'['}, input...), ']')
}

decoder := json.NewDecoder(bytes.NewReader(input))
decoder.UseNumber()
streamDecoder := decoder.NewStreamDecoder(bytes.NewReader(input))
streamDecoder.UseNumber()

return decoder.Decode(v)
return streamDecoder.Decode(v)
}
9 changes: 5 additions & 4 deletions pkg/yaml2json/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package yaml2json

import (
"bytes"
"encoding/json"
"text/template"

"github.com/bytedance/sonic/encoder"
"github.com/cristalhq/base64"
"github.com/google/uuid"
)
Expand Down Expand Up @@ -50,12 +50,13 @@ func (e *engine) uuid2int64(str string) string {
"low": low,
}

int64JSON, err := json.Marshal(int64Data)
if err != nil {
var buffer bytes.Buffer

if err := encoder.NewStreamEncoder(&buffer).Encode(int64Data); err != nil {
return ""
}

return string(int64JSON)
return buffer.String()
}

// uuid2base64 converts a UUID string to a base64 string.
Expand Down
Loading