Skip to content

Commit

Permalink
perf: use parse instead of getMany for gjson
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulguptajss committed Sep 28, 2023
1 parent b025ed6 commit 2dbb88f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 60 deletions.
10 changes: 4 additions & 6 deletions cmd/collectors/collectorstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 6 additions & 12 deletions cmd/collectors/restperf/restperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/collectors/storagegrid/plugins/joinrest/joinrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
27 changes: 14 additions & 13 deletions cmd/collectors/storagegrid/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...)
}
Expand Down Expand Up @@ -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()...)
}
Expand Down Expand Up @@ -280,23 +280,24 @@ 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
}

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
}

Expand Down Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions cmd/tools/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 14 additions & 19 deletions cmd/tools/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()}
Expand Down

0 comments on commit 2dbb88f

Please sign in to comment.