From 7dd2ac9831eab1f9de5acc27e5bc0b2aabdc3527 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 4 Mar 2022 17:39:15 +0800 Subject: [PATCH] refactor: replace json with json-iterator/go to improve performance --- convert.go | 3 ++- go.mod | 1 + go.sum | 5 +++++ internal/boomer/output.go | 3 ++- internal/boomer/stats.go | 3 ++- internal/builtin/function.go | 4 ++-- internal/har2case/core.go | 2 +- internal/json/json.go | 14 ++++++++++++++ parser.go | 6 +++--- plugin/inner/grpc.go | 2 +- response.go | 5 +++-- runner.go | 2 +- 12 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 internal/json/json.go diff --git a/convert.go b/convert.go index 396416f8..d0cdcfee 100644 --- a/convert.go +++ b/convert.go @@ -2,13 +2,14 @@ package hrp import ( "bytes" - "encoding/json" "fmt" "os" "path/filepath" "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" + + "github.com/httprunner/hrp/internal/json" ) func loadFromJSON(path string) (*TCase, error) { diff --git a/go.mod b/go.mod index 3bbe6445..855fec71 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/httprunner/hrp/plugin v0.0.0 github.com/jinzhu/copier v0.3.2 github.com/jmespath/go-jmespath v0.4.0 + github.com/json-iterator/go v1.1.12 github.com/maja42/goval v1.2.1 github.com/mattn/go-runewidth v0.0.13 // indirect github.com/olekukonko/tablewriter v0.0.5 diff --git a/go.sum b/go.sum index 7332da7e..806d0647 100644 --- a/go.sum +++ b/go.sum @@ -209,6 +209,8 @@ github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= @@ -252,9 +254,12 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= diff --git a/internal/boomer/output.go b/internal/boomer/output.go index 2f05149d..a2aa6422 100644 --- a/internal/boomer/output.go +++ b/internal/boomer/output.go @@ -1,7 +1,6 @@ package boomer import ( - "encoding/json" "fmt" "os" "sort" @@ -13,6 +12,8 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" "github.com/rs/zerolog/log" + + "github.com/httprunner/hrp/internal/json" ) // Output is primarily responsible for printing test results to different destinations diff --git a/internal/boomer/stats.go b/internal/boomer/stats.go index 0f19f595..2d501781 100644 --- a/internal/boomer/stats.go +++ b/internal/boomer/stats.go @@ -1,8 +1,9 @@ package boomer import ( - "encoding/json" "time" + + "github.com/httprunner/hrp/internal/json" ) type transaction struct { diff --git a/internal/builtin/function.go b/internal/builtin/function.go index 78c2f9fe..70cff18e 100644 --- a/internal/builtin/function.go +++ b/internal/builtin/function.go @@ -5,7 +5,6 @@ import ( "crypto/md5" "encoding/csv" "encoding/hex" - "encoding/json" "fmt" "math" "math/rand" @@ -15,9 +14,10 @@ import ( "strings" "time" + "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" - "github.com/rs/zerolog/log" + "github.com/httprunner/hrp/internal/json" ) var Functions = map[string]interface{}{ diff --git a/internal/har2case/core.go b/internal/har2case/core.go index ed2ce221..4d2b93cc 100644 --- a/internal/har2case/core.go +++ b/internal/har2case/core.go @@ -2,7 +2,6 @@ package har2case import ( "encoding/base64" - "encoding/json" "fmt" "io" "net/url" @@ -17,6 +16,7 @@ import ( "github.com/httprunner/hrp" "github.com/httprunner/hrp/internal/builtin" "github.com/httprunner/hrp/internal/ga" + "github.com/httprunner/hrp/internal/json" ) const ( diff --git a/internal/json/json.go b/internal/json/json.go new file mode 100644 index 00000000..9a5649ae --- /dev/null +++ b/internal/json/json.go @@ -0,0 +1,14 @@ +package json + +import ( + jsoniter "github.com/json-iterator/go" +) + +var json = jsoniter.ConfigCompatibleWithStandardLibrary + +var ( + Marshal = json.Marshal + MarshalIndent = json.MarshalIndent + Unmarshal = json.Unmarshal + NewDecoder = json.NewDecoder +) diff --git a/parser.go b/parser.go index 7cfccf55..8e1e8602 100644 --- a/parser.go +++ b/parser.go @@ -1,7 +1,7 @@ package hrp import ( - "encoding/json" + builtinJSON "encoding/json" "fmt" "net/url" "reflect" @@ -68,7 +68,7 @@ func (p *parser) parseData(raw interface{}, variablesMapping map[string]interfac switch rawValue.Kind() { case reflect.String: // json.Number - if rawValue, ok := raw.(json.Number); ok { + if rawValue, ok := raw.(builtinJSON.Number); ok { return parseJSONNumber(rawValue) } // other string @@ -108,7 +108,7 @@ func (p *parser) parseData(raw interface{}, variablesMapping map[string]interfac } } -func parseJSONNumber(raw json.Number) (interface{}, error) { +func parseJSONNumber(raw builtinJSON.Number) (interface{}, error) { if strings.Contains(raw.String(), ".") { // float64 return raw.Float64() diff --git a/plugin/inner/grpc.go b/plugin/inner/grpc.go index 5a1988ba..1c38a602 100644 --- a/plugin/inner/grpc.go +++ b/plugin/inner/grpc.go @@ -2,13 +2,13 @@ package pluginInternal import ( "context" - "encoding/json" "github.com/hashicorp/go-plugin" "github.com/pkg/errors" "github.com/rs/zerolog/log" "google.golang.org/grpc" + "github.com/httprunner/hrp/internal/json" "github.com/httprunner/hrp/plugin/proto" ) diff --git a/response.go b/response.go index 2861b119..895390f8 100644 --- a/response.go +++ b/response.go @@ -2,7 +2,7 @@ package hrp import ( "bytes" - "encoding/json" + builtinJSON "encoding/json" "fmt" "io" "net/http" @@ -15,6 +15,7 @@ import ( "github.com/rs/zerolog/log" "github.com/httprunner/hrp/internal/builtin" + "github.com/httprunner/hrp/internal/json" ) func newResponseObject(t *testing.T, parser *parser, resp *http.Response) (*responseObject, error) { @@ -184,7 +185,7 @@ func (v *responseObject) searchJmespath(expr string) interface{} { log.Error().Str("expr", expr).Err(err).Msg("search jmespath failed") return expr // jmespath not found, return the expression } - if number, ok := checkValue.(json.Number); ok { + if number, ok := checkValue.(builtinJSON.Number); ok { checkNumber, err := parseJSONNumber(number) if err != nil { log.Error().Interface("json number", number).Err(err).Msg("convert json number failed") diff --git a/runner.go b/runner.go index 7dd0178b..c986e6b5 100644 --- a/runner.go +++ b/runner.go @@ -7,7 +7,6 @@ import ( "compress/gzip" "crypto/tls" _ "embed" - "encoding/json" "fmt" "html/template" "io" @@ -32,6 +31,7 @@ import ( "github.com/httprunner/hrp/internal/builtin" "github.com/httprunner/hrp/internal/ga" + "github.com/httprunner/hrp/internal/json" pluginInternal "github.com/httprunner/hrp/plugin/inner" )