From fc23dd03a415f7ced1ea6b5d6038c265ff404605 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Sat, 6 Jul 2024 21:38:47 +0300 Subject: [PATCH] experiment with sonic --- go.mod | 7 +++++++ go.sum | 26 ++++++++++++++++++++++++++ go.work.sum | 9 +++------ internal/app/rest_server.go | 24 ++++++++++++------------ internal/app/storage.go | 10 +++++----- pkg/jsondecoder/decode.go | 9 +++++---- pkg/yaml2json/engine.go | 4 ++-- protoc-gen-gripmock/generator.go | 1 + protoc-gen-gripmock/go.mod | 13 ++++++++++++- protoc-gen-gripmock/go.sum | 12 ++++++++++++ protoc-gen-gripmock/server.tmpl | 4 ++-- 11 files changed, 87 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 9ac74f86..b56aa54f 100644 --- a/go.mod +++ b/go.mod @@ -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-20240706174427-ef324cdfb46b + github.com/bytedance/sonic v1.11.9 github.com/goccy/go-yaml v1.11.3 github.com/google/uuid v1.6.0 github.com/google/wire v0.6.0 @@ -35,8 +36,11 @@ require ( require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect github.com/bufbuild/protocompile v0.14.0 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/caarlos0/env/v10 v10.0.0 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.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 @@ -45,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 diff --git a/go.sum b/go.sum index 853455f1..4b9b3d19 100644 --- a/go.sum +++ b/go.sum @@ -12,10 +12,18 @@ github.com/bavix/gripmock/protogen v0.0.0-20240706174427-ef324cdfb46b/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.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg= +github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= 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/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -69,6 +77,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= @@ -94,9 +106,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= @@ -120,6 +140,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= @@ -196,5 +219,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= diff --git a/go.work.sum b/go.work.sum index 93e3bac3..c651181d 100644 --- a/go.work.sum +++ b/go.work.sum @@ -82,8 +82,7 @@ github.com/kataras/tunnel v0.0.4 h1:sCAqWuJV7nPzGrlb0os3j49lk2JhILT0rID38NHNLpA= github.com/kataras/tunnel v0.0.4/go.mod h1:9FkU4LaeifdMWqZu7o20ojmW4B7hdhv2CMLwfnHGpYw= github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -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 h1:0pHpWtx9vcvC0xGZqEQlQdfSQs7WRlAjuPvk3fOZDCo= github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8= github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= @@ -116,8 +115,6 @@ github.com/tdewolff/minify/v2 v2.12.9 h1:dvn5MtmuQ/DFMwqf5j8QhEVpPX6fi3WGImhv8RU github.com/tdewolff/minify/v2 v2.12.9/go.mod h1:qOqdlDfL+7v0/fyymB+OP497nIxJYSvX4MQWA8OoiXU= github.com/tdewolff/parse/v2 v2.6.8 h1:mhNZXYCx//xG7Yq2e/kVLNZw4YfYmeHbhx+Zc0OvFMA= github.com/tdewolff/parse/v2 v2.6.8/go.mod h1:XHDhaU6IBgsryfdnpzUXBlT6leW/l25yrFBTEb4eIyM= -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/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -131,8 +128,6 @@ github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV github.com/yosssi/ace v0.0.5 h1:tUkIP/BLdKqrlrPwcmH0shwEEhTRHoGnc1wFIWmaBUA= github.com/yosssi/ace v0.0.5/go.mod h1:ALfIzm2vT7t5ZE7uoIZqF3TQ7SAOyupFZnkrF5id+K0= github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= -golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc= -golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= @@ -145,3 +140,5 @@ golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +nullprogram.com/x/optparse v1.0.0 h1:xGFgVi5ZaWOnYdac2foDT3vg0ZZC9ErXFV57mr4OHrI= +rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4= diff --git a/internal/app/rest_server.go b/internal/app/rest_server.go index 51add7e4..46afbd6a 100644 --- a/internal/app/rest_server.go +++ b/internal/app/rest_server.go @@ -1,7 +1,6 @@ package app import ( - "encoding/json" "errors" "fmt" "io" @@ -12,6 +11,7 @@ import ( "sync/atomic" "time" + "github.com/bytedance/sonic/encoder" "github.com/google/uuid" "github.com/gripmock/stuber" "golang.org/x/text/cases" @@ -88,7 +88,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) } } @@ -107,13 +107,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) } } @@ -127,7 +127,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) } } @@ -160,7 +160,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 @@ -202,7 +202,7 @@ 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) } } @@ -210,7 +210,7 @@ func (h *RestServer) ListUsedStubs(w http.ResponseWriter, _ *http.Request) { 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) } } @@ -218,7 +218,7 @@ func (h *RestServer) ListUnusedStubs(w http.ResponseWriter, _ *http.Request) { 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) } } @@ -256,7 +256,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) } } @@ -269,7 +269,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) } } @@ -281,7 +281,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) diff --git a/internal/app/storage.go b/internal/app/storage.go index 3b2ba4d4..95405778 100644 --- a/internal/app/storage.go +++ b/internal/app/storage.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/jsondecoder/decode.go b/pkg/jsondecoder/decode.go index bdef2e05..c379acba 100644 --- a/pkg/jsondecoder/decode.go +++ b/pkg/jsondecoder/decode.go @@ -2,7 +2,8 @@ package jsondecoder import ( "bytes" - "encoding/json" + + "github.com/bytedance/sonic/decoder" ) //nolint:mnd @@ -16,8 +17,8 @@ func UnmarshalSlice(data []byte, v interface{}) error { input = append(append([]byte{91}, input...), 93) } - decoder := json.NewDecoder(bytes.NewReader(input)) - decoder.UseNumber() + streamDecoder := decoder.NewStreamDecoder(bytes.NewReader(input)) + streamDecoder.UseNumber() - return decoder.Decode(v) + return streamDecoder.Decode(v) } diff --git a/pkg/yaml2json/engine.go b/pkg/yaml2json/engine.go index 59974bdf..62e69b09 100644 --- a/pkg/yaml2json/engine.go +++ b/pkg/yaml2json/engine.go @@ -3,9 +3,9 @@ package yaml2json import ( "bytes" "encoding/base64" - "encoding/json" "text/template" + "github.com/bytedance/sonic/encoder" "github.com/google/uuid" ) @@ -40,7 +40,7 @@ func (e *engine) uuid2int64(str string) string { var buffer bytes.Buffer - if err := json.NewEncoder(&buffer).Encode(map[string]int64{ + if err := encoder.NewStreamEncoder(&buffer).Encode(map[string]int64{ "high": high, "low": low, }); err != nil { diff --git a/protoc-gen-gripmock/generator.go b/protoc-gen-gripmock/generator.go index 5e5bc624..ecd9866f 100644 --- a/protoc-gen-gripmock/generator.go +++ b/protoc-gen-gripmock/generator.go @@ -10,6 +10,7 @@ import ( "strings" "text/template" + _ "github.com/bytedance/sonic" "golang.org/x/text/cases" "golang.org/x/text/language" "golang.org/x/tools/imports" diff --git a/protoc-gen-gripmock/go.mod b/protoc-gen-gripmock/go.mod index 21258063..8e4a1756 100644 --- a/protoc-gen-gripmock/go.mod +++ b/protoc-gen-gripmock/go.mod @@ -3,9 +3,20 @@ module github.com/bavix/gripmock/protoc-gen-gripmock go 1.22.2 require ( + github.com/bytedance/sonic v1.11.9 golang.org/x/text v0.16.0 golang.org/x/tools v0.22.0 google.golang.org/protobuf v1.34.2 ) -require golang.org/x/mod v0.19.0 // indirect +require ( + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/klauspost/cpuid/v2 v2.2.5 // indirect + github.com/stretchr/testify v1.9.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + golang.org/x/arch v0.4.0 // indirect + golang.org/x/mod v0.19.0 // indirect + golang.org/x/sys v0.22.0 // indirect +) diff --git a/protoc-gen-gripmock/go.sum b/protoc-gen-gripmock/go.sum index 94aaf586..d467937d 100644 --- a/protoc-gen-gripmock/go.sum +++ b/protoc-gen-gripmock/go.sum @@ -1,12 +1,24 @@ +github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc= golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/protoc-gen-gripmock/server.tmpl b/protoc-gen-gripmock/server.tmpl index 04de4660..0114fba5 100644 --- a/protoc-gen-gripmock/server.tmpl +++ b/protoc-gen-gripmock/server.tmpl @@ -10,9 +10,9 @@ import ( "log" "net" "net/http" - "encoding/json" "github.com/gripmock/environment" + "github.com/bytedance/sonic" jsonpb "google.golang.org/protobuf/encoding/protojson" "google.golang.org/grpc" "google.golang.org/grpc/metadata" @@ -219,7 +219,7 @@ func findStub(ctx context.Context, conf environment.Config, service, method stri } } - data, err := json.Marshal(searchStub.JSON200.Data) + data, err := sonic.Marshal(searchStub.JSON200.Data) if err != nil { return err }