Repository: prometheus/prometheus
File: promql/bench_test.go (Go)

Code before:
	}

	cases := []benchCase{
		{
			expr:  `rpc_request_success_total + rpc_request_error_total`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total + ON (job, instance) GROUP_LEFT rpc_request_error_total`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total AND rpc_request_error_total{instance=~"0.*"}`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total OR rpc_request_error_total{instance=~"0.*"}`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total UNLESS rpc_request_error_total{instance=~"0.*"}`,
			steps: 10000,
		},
	}

	for _, c := range cases {
		name := fmt.Sprintf("expr=%s,steps=%d", c.expr, c.steps)
		b.Run(name, func(b *testing.B) {
			ctx := context.Background()
			b.ReportAllocs()
			for range b.N {
				qry, err := engine.NewRangeQuery(
					ctx, stor, nil, c.expr,
					time.Unix(int64((numIntervals-c.steps)*10), 0),
					time.Unix(int64(numIntervals*10), 0), time.Second*10)
				if err != nil {
					b.Fatal(err)
				}
				res := qry.Exec(ctx)
				if res.Err != nil {
					b.Fatal(res.Err)
				}
				qry.Close()
			}
		})
	}
}

func BenchmarkNativeHistograms(b *testing.B) {
	testStorage := teststorage.New(b)
	defer testStorage.Close()

Review comment (suggestion):
A bit of opinion, but why creating variables used only once? It's clear those are cases.

```suggestion
	for _, c := range []benchCase{
		{
			expr:  `rpc_request_success_total + rpc_request_error_total`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total + ON (job, instance) GROUP_LEFT rpc_request_error_total`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total AND rpc_request_error_total{instance=~"0.*"}`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total OR rpc_request_error_total{instance=~"0.*"}`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total UNLESS rpc_request_error_total{instance=~"0.*"}`,
			steps: 10000,
		},
	} {
```

Code after:
		Logger:     nil,
		Reg:        nil,
		MaxSamples: 50000000,
		Timeout:    100 * time.Second,
	}
	engine := promqltest.NewTestEngineWithOpts(b, opts)

	const interval = 10000 // 10s interval.

	// A day of data plus 10k steps.
	numIntervals := 8640 + 10000

	require.NoError(b, setupJoinQueryTestData(stor, engine, interval, numIntervals, 1000))

	for _, c := range []benchCase{
		{
			expr:  `rpc_request_success_total + rpc_request_error_total`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total + ON (job, instance) GROUP_LEFT rpc_request_error_total`,
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total AND rpc_request_error_total{instance=~"0.*"}`, // 0.* keeps 1/16 of UUID values
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total OR rpc_request_error_total{instance=~"0.*"}`, // 0.* keeps 1/16 of UUID values
			steps: 10000,
		},
		{
			expr:  `rpc_request_success_total UNLESS rpc_request_error_total{instance=~"0.*"}`, // 0.* keeps 1/16 of UUID values
			steps: 10000,
		},
	} {
		name := fmt.Sprintf("expr=%s/steps=%d", c.expr, c.steps)
		b.Run(name, func(b *testing.B) {
			ctx := context.Background()
			b.ReportAllocs()
			for range b.N {
				qry, err := engine.NewRangeQuery(
					ctx, stor, nil, c.expr,
					timestamp.Time(int64((numIntervals-c.steps)*10_000)),
					timestamp.Time(int64(numIntervals*10_000)),
					time.Second*10)
				require.NoError(b, err)

				res := qry.Exec(ctx)
				require.NoError(b, res.Err)