refactor: replace json with json-iterator/go to improve performance

This commit is contained in:
debugtalk
2022-03-04 17:39:15 +08:00
parent b729b66be3
commit 7dd2ac9831
12 changed files with 37 additions and 13 deletions

View File

@@ -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) {

1
go.mod
View File

@@ -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

5
go.sum
View File

@@ -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=

View File

@@ -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

View File

@@ -1,8 +1,9 @@
package boomer
import (
"encoding/json"
"time"
"github.com/httprunner/hrp/internal/json"
)
type transaction struct {

View File

@@ -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{}{

View File

@@ -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 (

14
internal/json/json.go Normal file
View File

@@ -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
)

View File

@@ -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()

View File

@@ -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"
)

View File

@@ -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")

View File

@@ -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"
)