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

Code before:
		for i, metric := range metrics {
			ref, _ := a.Append(refs[i], metric, ts, float64(s)+float64(i)/float64(len(metrics)))
			refs[i] = ref
		}
		// Generating a sparse time series: each label value of "l" will contain data only for
		// pointsPerSparseSeries points
		metric := labels.FromStrings("__name__", "sparse", "l", strconv.Itoa(s/pointsPerSparseSeries))
		_, err := a.Append(0, metric, ts, float64(s)/float64(len(metrics)))
		if err != nil {
			return err
		}
		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
}

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
		}

Review comment (suggestion):
Please add a short comment noting the reason for having this distinct from `setupRangeQueryTestData`.

Code after:
		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
		}
		// Generating a sparse time series: each label value of "l" will contain data only for
		// pointsPerSparseSeries points
		metric := labels.FromStrings("__name__", "sparse", "l", strconv.Itoa(s/pointsPerSparseSeries))
		_, err := a.Append(0, metric, ts, float64(s)/float64(len(metrics)))
		if err != nil {
			return err
		}
		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
}

// 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())