Skip to content

Instantly share code, notes, and snippets.

@kylebrandt
Created August 28, 2023 18:15
Show Gist options
  • Save kylebrandt/75995d87d6c7c0e88748a3e7e94cae6f to your computer and use it in GitHub Desktop.
Save kylebrandt/75995d87d6c7c0e88748a3e7e94cae6f to your computer and use it in GitHub Desktop.
Benchmark executedQueryString

Before (left), After (Right) image

image

With executed query string on each frame

go test -benchmem -benchtime=40x  -run=^$ -bench ^BenchmarkRangeJson$ ~/go/github.com/grafana/grafana/pkg/tsdb/prometheus/querydata -memprofile ~/tmp/memprofile_bExprBefore.out -cpuprofile=~/tmp/cpu_bExprBefore.out -count 6
testing: open /home/kbrandt/go/github.com/grafana/grafana/pkg/util/converter/~/tmp/cpu_bExprBefore.out: no such file or directory
goos: linux
goarch: amd64
pkg: github.com/grafana/grafana/pkg/tsdb/prometheus/querydata
cpu: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
BenchmarkRangeJson-8          40          33826303 ns/op        31177748 B/op      84394 allocs/op
BenchmarkRangeJson-8          40          35159553 ns/op        35068823 B/op      84402 allocs/op
BenchmarkRangeJson-8          40          33490775 ns/op        27345236 B/op      84385 allocs/op
BenchmarkRangeJson-8          40          34699986 ns/op        29883828 B/op      84390 allocs/op
BenchmarkRangeJson-8          40          34493825 ns/op        33630009 B/op      84397 allocs/op
BenchmarkRangeJson-8          40          34925758 ns/op        32304326 B/op      84394 allocs/op
PASS
ok      github.com/grafana/grafana/pkg/tsdb/prometheus/querydata        8.675s

With first frame only

go test -benchmem -benchtime=40x  -run=^$ -bench ^BenchmarkRangeJson$ ~/go/github.com/grafana/grafana/pkg/tsdb/prometheus/querydata -memprofile ~/tmp/memprofile_bExprAfter.out -cpuprofile=~/tmp/cpu_bExprAfter.out -count 6           
testing: open /home/kbrandt/go/github.com/grafana/grafana/pkg/util/converter/~/tmp/cpu_bExprAfter.out: no such file or directory
goos: linux
goarch: amd64
pkg: github.com/grafana/grafana/pkg/tsdb/prometheus/querydata
cpu: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
BenchmarkRangeJson-8          40          10039714 ns/op         5232868 B/op      82376 allocs/op
BenchmarkRangeJson-8          40           9657197 ns/op         5041564 B/op      82371 allocs/op
BenchmarkRangeJson-8          40           9585840 ns/op         4989276 B/op      82371 allocs/op
BenchmarkRangeJson-8          40           9756458 ns/op         5571160 B/op      82377 allocs/op
BenchmarkRangeJson-8          40           9576301 ns/op         5070035 B/op      82371 allocs/op
BenchmarkRangeJson-8          40           9717125 ns/op         4878926 B/op      82368 allocs/op
PASS
ok      github.com/grafana/grafana/pkg/tsdb/prometheus/querydata        2.557s

code changes:

diff --git a/pkg/tsdb/prometheus/querydata/framing_bench_test.go b/pkg/tsdb/prometheus/querydata/framing_bench_test.go
index 295810ed9f6..2b249bf1ba4 100644
--- a/pkg/tsdb/prometheus/querydata/framing_bench_test.go
+++ b/pkg/tsdb/prometheus/querydata/framing_bench_test.go
@@ -66,7 +66,7 @@ func BenchmarkRangeJson(b *testing.B) {
                r   *backend.QueryDataResponse
                err error
        )
-       body, q := createJsonTestData(1642000000, 1, 300, 400)
+       body, q := createJsonTestData(1642000000, 1, 2, 1000)
        tCtx, err := setup()
        require.NoError(b, err)
 
@@ -80,6 +80,8 @@ func BenchmarkRangeJson(b *testing.B) {
                tCtx.httpProvider.setResponse(&res)
                r, err = tCtx.queryData.Execute(context.Background(), q)
                require.NoError(b, err)
+               _, err = json.Marshal(r)
+               require.NoError(b, err)
        }
 
        resp = r
@@ -129,7 +131,7 @@ func createJsonTestData(start int64, step int64, timestampCount int, seriesCount
        qm := models.QueryModel{
                PrometheusDataQuery: dataquery.PrometheusDataQuery{
                        Range: kindsys.Ptr(true),
-                       Expr:  "test",
+                       Expr:  strings.Repeat("1+", 2000) + "1",
                },
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment