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

Code before:
	}

	stor.ForceHeadMMap() // Ensure we have at most one head chunk for every series.
	stor.Compact(ctx)
	return nil
}

func setupJoinQueryTestData(stor *teststorage.TestStorage, _ *promql.Engine, interval, numIntervals, numInstances int) error {
	ctx := context.Background()

	commonLabels := []string{
		"environment", "staging",
		"cluster", "test-kubernetes-cluster",
		"namespace", "test-kubernetes-namespace",
		"job", "worker",
		"rpc_method", "fetch-my-data-from-this-service",
		"domain", "test-domain",
	}

	var metrics []labels.Labels
	for range numInstances {
		instance := uuid.New().String()
		labelsWithInstance := append(commonLabels, "instance", instance)

		metrics = append(metrics, labels.FromStrings(append(labelsWithInstance, "__name__", "rpc_request_success_total")...))
		metrics = append(metrics, labels.FromStrings(append(labelsWithInstance, "__name__", "rpc_request_error_total")...))
	}

	refs := make([]storage.SeriesRef, len(metrics))

	for s := range numIntervals {
		a := stor.Appender(context.Background())
		ts := int64(s * interval)
		for i, metric := range metrics {
			ref, _ := a.Append(refs[i], metric, ts, float64(s)+float64(i)/float64(len(metrics)))
			refs[i] = ref
		}
		if err := a.Commit(); err != nil {
			return err
		}
	}

	stor.ForceHeadMMap() // Ensure we have at most one head chunk for every series.
	stor.Compact(ctx)
	return nil
}

type benchCase struct {
	expr  string
	steps int
}

Review comment (suggestion):
small readability nit: We typically try to make things more declarative for tests (reliability and simpler to read), so:

```suggestion
		instance := uuid.New().String()
		metrics = append(metrics, labels.FromStrings(
		  "__name__", "rpc_request_success_total",
		   "instance", instance,
		   "environment", "staging",
		"cluster", "test-kubernetes-cluster",
		"namespace", "test-kubernetes-namespace",
		"job", "worker",
		"rpc_method", "fetch-my-data-from-this-service",
		"domain", "test-domain",
	    ))
	    metrics = append(metrics, labels.FromStrings(
		   "__name__", "rpc_request_error_total",
		   "instance", instance,
		   	   "environment", "staging",
		"cluster", "test-kubernetes-cluster",
		"namespace", "test-kubernetes-namespace",
		"job", "worker",
		"rpc_method", "fetch-my-data-from-this-service",
		"domain", "test-domain",
	    ))
```

or at least:
```
		instance := uuid.New().String()
		metrics = append(metrics, labels.FromStrings(
		   append([]string{
		   "__name__", "rpc_request_success_total",
		   "instance", instance,
		   }, commonLabels...)... 
	    ))
	    metrics = append(metrics, labels.FromStrings(
		   append([]string{
		   "__name__", "rpc_request_error_total",
		   "instance", instance,
		   }, commonLabels...)... 
	    ))
```

Code after:
			return err
		}
	}

	stor.ForceHeadMMap() // Ensure we have at most one head chunk for every series.
	stor.Compact(ctx)
	return nil
}

// setupJoinQueryTestData creates numInstances series for two metrics ("rpc_request_success_total" and
// "rpc_request_error_total") that can be joined on "instance" label for benchmarking join performance.
func setupJoinQueryTestData(stor *teststorage.TestStorage, _ *promql.Engine, interval, numIntervals, numInstances int) error {
	ctx := context.Background()

	commonLabels := labels.FromStrings(
		"environment", "staging",
		"cluster", "test-kubernetes-cluster",
		"namespace", "test-kubernetes-namespace",
		"job", "worker",
		"rpc_method", "fetch-my-data-from-this-service",
		"domain", "test-domain",
	)
	builder := labels.NewBuilder(commonLabels)

	rnd := rand.New(rand.NewSource(0)) // Fixed seed for deterministic results.

	var metrics []labels.Labels
	for range numInstances {
		instance, err := uuid.NewRandomFromReader(rnd)
		if err != nil {
			return err
		}
		builder.Set("instance", instance.String())

		builder.Set("__name__", "rpc_request_success_total")
		metrics = append(metrics, builder.Labels())

		builder.Set("__name__", "rpc_request_error_total")
		metrics = append(metrics, builder.Labels())
	}

	refs := make([]storage.SeriesRef, len(metrics))

	for s := range numIntervals {
		a := stor.Appender(context.Background())
		ts := int64(s * interval)
		for i, metric := range metrics {
			ref, _ := a.Append(refs[i], metric, ts, float64(s)+float64(i)/float64(len(metrics)))
			refs[i] = ref
		}
		if err := a.Commit(); err != nil {