Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use parse instead of getMany for gjson #2385

Merged
merged 1 commit into from
Sep 28, 2023
Merged

perf: use parse instead of getMany for gjson #2385

merged 1 commit into from
Sep 28, 2023

Conversation

rahulguptajss
Copy link
Contributor

BenchStat

benchstat old.txt new.txt                                                                                              
goos: darwin
goarch: amd64
pkg: github.com/netapp/harvest/v2/cmd/tools/rest
cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
      │   old.txt   │              new.txt               │
      │   sec/op    │   sec/op     vs base               │
Run-8   175.5m ± 3%   167.8m ± 1%  -4.39% (p=0.000 n=20)

      │    old.txt    │               new.txt                │
      │     B/op      │     B/op      vs base                │
Run-8   29.432Mi ± 0%   4.494Mi ± 0%  -84.73% (p=0.000 n=20)

      │  old.txt   │              new.txt               │
      │ allocs/op  │ allocs/op   vs base                │
Run-8   23.00 ± 0%   20.00 ± 0%  -13.04% (p=0.000 n=20)

GetManyBytes

goos: darwin
goarch: amd64
pkg: github.com/netapp/harvest/v2/cmd/tools/rest
cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
BenchmarkRun-8   	       6	 176583157 ns/op	30864000 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 171905008 ns/op	30863104 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 173131458 ns/op	30861786 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 172861186 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 191586987 ns/op	30863104 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 208865388 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 182311277 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 184147214 ns/op	30861744 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 174858837 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 172048299 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 175390261 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 174621808 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 175406557 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 182451984 ns/op	30861770 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 171076953 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 175685866 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 176748725 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 177837733 ns/op	30861754 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 173864807 ns/op	30861738 B/op	      23 allocs/op
BenchmarkRun-8   	       6	 181394717 ns/op	30861738 B/op	      23 allocs/op
PASS
ok  	github.com/netapp/harvest/v2/cmd/tools/rest	66.796s

ParseBytes

goos: darwin
goarch: amd64
pkg: github.com/netapp/harvest/v2/cmd/tools/rest
cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
BenchmarkRun-8   	       7	 166125964 ns/op	 4714582 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 168723358 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 166443073 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 168285388 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 168824665 ns/op	 4713794 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 167641136 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 168703432 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 166981145 ns/op	 4717426 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 168052484 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 177139801 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 171096416 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 168462390 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 167633231 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 166960088 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 167239540 ns/op	 4712629 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 167137994 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 170434566 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 164541698 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       6	 177470561 ns/op	 4712624 B/op	      20 allocs/op
BenchmarkRun-8   	       7	 167038610 ns/op	 4712624 B/op	      20 allocs/op
PASS
ok  	github.com/netapp/harvest/v2/cmd/tools/rest	70.290s

Benchmark Code

package rest

import (
	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
	"io/ioutil"
	"net/http"
	"testing"
)

var path = "https://raw.githubusercontent.com/json-iterator/test-data/master/large-file.json"

func BenchmarkParse(b *testing.B) {
	_, newJson := initFile()

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		o := gjson.Parse(newJson)
		records := o.Get("records").Array()
		numRecords := o.Get("num_records").Int()
		href := o.Get("_links.self.href").String()

		_ = records
		_ = numRecords
		_ = href
	}
}

func BenchmarkGet(b *testing.B) {

	bytes, _ := initFile()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		o := gjson.GetManyBytes(bytes, "records", "num_records", "_links.next.href")
		records := o[0].Array()
		numRecords := o[1].Int()
		links := o[2].String()
		_ = records
		_ = numRecords
		_ = links
	}

}

func initFile() ([]byte, string) {
	resp, err := http.Get(path)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	json := string(bytes)

	newJson, err := sjson.SetRaw(`{}`, "records", json)
	if err != nil {
		panic(err)
	}

	newJson, err = sjson.Set(newJson, "num_records", 197)
	if err != nil {
		panic(err)
	}

	newJson, err = sjson.Set(newJson, "_links.self.href", "/api/storage/volumes?fields=*")
	if err != nil {
		panic(err)
	}

	bytes = []byte(newJson)

	return bytes, newJson
}

@cgrinds cgrinds merged commit 5830b93 into main Sep 28, 2023
9 checks passed
@cgrinds cgrinds deleted the rg2-perf-gjson branch September 28, 2023 14:32
@rahulguptajss rahulguptajss linked an issue Sep 29, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Analyze collectors for alloc and inuse memory improvements
3 participants