From 2dbb88fdeb575cdbed5ed3f8a347427fda0a14dd Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Thu, 28 Sep 2023 17:41:23 +0530 Subject: [PATCH] perf: use parse instead of getMany for gjson --- cmd/collectors/collectorstest.go | 10 +++--- cmd/collectors/restperf/restperf.go | 18 ++++------ .../storagegrid/plugins/joinrest/joinrest.go | 6 ++-- cmd/collectors/storagegrid/rest/client.go | 27 +++++++-------- cmd/tools/rest/client.go | 14 ++++---- cmd/tools/rest/rest.go | 33 ++++++++----------- 6 files changed, 48 insertions(+), 60 deletions(-) diff --git a/cmd/collectors/collectorstest.go b/cmd/collectors/collectorstest.go index ad432c0db..9e9b073aa 100644 --- a/cmd/collectors/collectorstest.go +++ b/cmd/collectors/collectorstest.go @@ -50,13 +50,11 @@ func JSONToGson(path string, flatten bool) []gjson.Result { return nil } bb := b.Bytes() - output := gjson.GetManyBytes(bb, "records", "num_records", "_links.next.href") + output := gjson.ParseBytes(bb) + data := output.Get("records") + numRecords := output.Get("num_records") - data := output[0] - numRecords := output[1] - isNonIterRestCall := !data.Exists() - - if isNonIterRestCall { + if !data.Exists() { contentJSON := `{"records":[]}` response, err := sjson.SetRawBytes([]byte(contentJSON), "records.-1", bb) if err != nil { diff --git a/cmd/collectors/restperf/restperf.go b/cmd/collectors/restperf/restperf.go index d65ab905c..e398e818a 100644 --- a/cmd/collectors/restperf/restperf.go +++ b/cmd/collectors/restperf/restperf.go @@ -476,18 +476,12 @@ func parseMetricResponse(instanceData gjson.Result, metric string) *metricRespon for _, name := range t.Array() { if name.String() == metric { metricPath := "counters.#(name=" + metric + ")" - many := gjson.GetMany(instanceDataS, - metricPath+".value", - metricPath+".values", - metricPath+".labels", - metricPath+".counters.#.label", - metricPath+".counters.#.values", - ) - value := many[0] - values := many[1] - labels := many[2] - subLabels := many[3] - subValues := many[4] + many := gjson.Parse(instanceDataS) + value := many.Get(metricPath + ".value") + values := many.Get(metricPath + ".values") + labels := many.Get(metricPath + ".labels") + subLabels := many.Get(metricPath + ".counters.#.label") + subValues := many.Get(metricPath + ".counters.#.values") if value.String() != "" { return &metricResponse{value: strings.Clone(value.String()), label: ""} } diff --git a/cmd/collectors/storagegrid/plugins/joinrest/joinrest.go b/cmd/collectors/storagegrid/plugins/joinrest/joinrest.go index d4f6fa12a..1e8e23967 100644 --- a/cmd/collectors/storagegrid/plugins/joinrest/joinrest.go +++ b/cmd/collectors/storagegrid/plugins/joinrest/joinrest.go @@ -131,9 +131,9 @@ func (t *JoinRest) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, err } func (t *JoinRest) updateCache(model join, bytes *[]byte) { - results := gjson.GetManyBytes(*bytes, "data.#."+model.JoinRest, "data.#."+model.LabelRest) - keys := results[0].Array() - vals := results[1].Array() + results := gjson.ParseBytes(*bytes) + keys := results.Get("data.#." + model.JoinRest).Array() + vals := results.Get("data.#." + model.LabelRest).Array() if len(keys) != len(vals) { t.Logger.Error(). Str("restKey", model.JoinRest). diff --git a/cmd/collectors/storagegrid/rest/client.go b/cmd/collectors/storagegrid/rest/client.go index d8feb4d08..c2af63765 100644 --- a/cmd/collectors/storagegrid/rest/client.go +++ b/cmd/collectors/storagegrid/rest/client.go @@ -143,8 +143,8 @@ func (c *Client) Fetch(request string, result *[]gjson.Result) error { return fmt.Errorf("error making request %w", err) } - output := gjson.GetManyBytes(fetched, "data") - data = output[0] + output := gjson.ParseBytes(fetched) + data = output.Get("data") for _, r := range data.Array() { *result = append(*result, r.Array()...) } @@ -172,8 +172,8 @@ func (c *Client) GetMetricQuery(metric string, result *[]gjson.Result) error { if err != nil { return err } - output := gjson.GetManyBytes(fetched, "data") - data := output[0] + output := gjson.ParseBytes(fetched) + data := output.Get("data") for _, r := range data.Array() { *result = append(*result, r.Array()...) } @@ -280,8 +280,8 @@ func (c *Client) Init(retries int) error { if content, err = c.GetGridRest("grid/config/product-version"); err != nil { continue } - results := gjson.GetManyBytes(content, "data.productVersion") - err = c.SetVersion(results[0].String()) + results := gjson.ParseBytes(content) + err = c.SetVersion(results.Get("data.productVersion").String()) if err != nil { return err } @@ -289,14 +289,15 @@ func (c *Client) Init(retries int) error { if content, err = c.GetGridRest("grid/health/topology?depth=grid"); err != nil { continue } - results = gjson.GetManyBytes(content, "data.name") - c.Cluster.Name = strings.ReplaceAll(results[0].String(), " ", "_") + + results = gjson.ParseBytes(content) + c.Cluster.Name = strings.ReplaceAll(results.Get("data.name").String(), " ", "_") if content, err = c.GetGridRest("grid/license"); err != nil { continue } - results = gjson.GetManyBytes(content, "data.systemId") - c.Cluster.UUID = results[0].String() + results = gjson.ParseBytes(content) + c.Cluster.UUID = results.Get("data.systemId").String() return nil } @@ -377,9 +378,9 @@ func (c *Client) fetchTokenWithAuthRetry() error { return errs.NewStorageGridErr(response.StatusCode, body) } - results := gjson.GetManyBytes(body, "data", "message.text") - token := results[0] - errorMsg := results[1] + results := gjson.ParseBytes(body) + token := results.Get("data") + errorMsg := results.Get("message.text") if token.Exists() { c.token = token.String() diff --git a/cmd/tools/rest/client.go b/cmd/tools/rest/client.go index 6a486f299..0aaff5ce7 100644 --- a/cmd/tools/rest/client.go +++ b/cmd/tools/rest/client.go @@ -301,13 +301,13 @@ func (c *Client) Init(retries int) error { continue } - results := gjson.GetManyBytes(content, "name", "uuid", "version.full", "version.generation", "version.major", "version.minor") - c.cluster.Name = results[0].String() - c.cluster.UUID = results[1].String() - c.cluster.Info = results[2].String() - c.cluster.Version[0] = int(results[3].Int()) - c.cluster.Version[1] = int(results[4].Int()) - c.cluster.Version[2] = int(results[5].Int()) + results := gjson.ParseBytes(content) + c.cluster.Name = results.Get("name").String() + c.cluster.UUID = results.Get("uuid").String() + c.cluster.Info = results.Get("version.full").String() + c.cluster.Version[0] = int(results.Get("version.generation").Int()) + c.cluster.Version[1] = int(results.Get("version.major").Int()) + c.cluster.Version[2] = int(results.Get("version.minor").Int()) return nil } return err diff --git a/cmd/tools/rest/rest.go b/cmd/tools/rest/rest.go index a5be563ee..a42a402dc 100644 --- a/cmd/tools/rest/rest.go +++ b/cmd/tools/rest/rest.go @@ -450,16 +450,12 @@ func fetch(client *Client, href string, records *[]gjson.Result, downloadAll boo return fmt.Errorf("error making request %w", err) } - isNonIterRestCall := false - output := gjson.GetManyBytes(getRest, "records", "num_records", "_links.next.href") - data := output[0] - numRecords := output[1] - next := output[2] - if !data.Exists() { - isNonIterRestCall = true - } + output := gjson.ParseBytes(getRest) + data := output.Get("records") + numRecords := output.Get("num_records") + next := output.Get("_links.next.href") - if isNonIterRestCall { + if !data.Exists() { contentJSON := `{"records":[]}` response, err := sjson.SetRawBytes([]byte(contentJSON), "records.-1", getRest) if err != nil { @@ -503,11 +499,11 @@ func fetchAnalytics(client *Client, href string, records *[]gjson.Result, analyt return fmt.Errorf("error making request %w", err) } - output := gjson.GetManyBytes(getRest, "records", "num_records", "_links.next.href", "analytics") - data := output[0] - numRecords := output[1] - next := output[2] - *analytics = output[3] + output := gjson.ParseBytes(getRest) + data := output.Get("records") + numRecords := output.Get("num_records") + next := output.Get("_links.next.href") + *analytics = output.Get("analytics") // extract returned records since paginated records need to be merged into a single lists if numRecords.Exists() && numRecords.Int() > 0 { @@ -546,11 +542,10 @@ func FetchRestPerfData(client *Client, href string, perfRecords *[]PerfRecord) e } // extract returned records since paginated records need to be merged into a single list - output := gjson.GetManyBytes(getRest, "records", "num_records", "_links.next.href") - - data := output[0] - numRecords := output[1] - next := output[2] + output := gjson.ParseBytes(getRest) + data := output.Get("records") + numRecords := output.Get("num_records") + next := output.Get("_links.next.href") if numRecords.Exists() && numRecords.Int() > 0 { p := PerfRecord{Records: data, Timestamp: time.Now().UnixNano()}