Repository: kubernetes/kubernetes
File: test/integration/scheduler_perf/scheduler_perf.go (Go)

Code before:
	}
	return value > threshold
}

func compareMetricWithThreshold(items []DataItem, threshold float64, metricSelector thresholdMetricSelector) error {
	if threshold == 0 {
		return nil
	}
	for _, item := range items {
		if item.Labels["Metric"] == metricSelector.Name && labelsMatch(item.Labels, metricSelector.Labels) && !valueWithinThreshold(item.Data["Average"], threshold, metricSelector.ExpectLower) {
			if metricSelector.ExpectLower {
				return fmt.Errorf("%s: expected %s Average to be lower: got %f, want %f", item.Labels["Name"], metricSelector.Name, item.Data["Average"], threshold)
			}
			return fmt.Errorf("%s: expected %s Average to be higher: got %f, want %f", item.Labels["Name"], metricSelector.Name, item.Data["Average"], threshold)
		}
	}
	return nil
}

func addThresholdToData(items []DataItem, threshold float64, metricSelector thresholdMetricSelector) {
	if threshold == 0 {
		return
	}
	for _, item := range items {
		if item.Labels["Metric"] == metricSelector.Name && labelsMatch(item.Labels, metricSelector.Labels) {
			item.Data["Threshold"] = threshold
		}
	}
}

func checkEmptyInFlightEvents() error {
	labels := append(schedframework.AllClusterEventLabels(), metrics.PodPoppedInFlightEvent)
	for _, label := range labels {
		value, err := testutil.GetGaugeMetricValue(metrics.InFlightEvents.WithLabelValues(label))
		if err != nil {
			return fmt.Errorf("failed to get InFlightEvents metric for label %s", label)
		}
		if value > 0 {
			return fmt.Errorf("InFlightEvents for label %s should be empty, but has %v items", label, value)
		}
	}
	return nil
}

func startCollectingMetrics(tCtx ktesting.TContext, collectorWG *sync.WaitGroup, podInformer coreinformers.PodInformer, mcc *metricsCollectorConfig, throughputErrorMargin float64, opIndex int, name string, namespaces []string, labelSelector map[string]string) (ktesting.TContext, []testDataCollector, error) {
	collectorCtx := ktesting.WithCancel(tCtx)
	workloadName := tCtx.Name()

	// Clean up memory usage from the initial setup phase.
	runtime.GC()


Review comment (suggestion):
If we call it “Threshold,” the reader may not know which data bucket (`Average`, `Perc50`, etc.) it refers to.

Code after:
			return false
		}
	}
	return true
}

func valueWithinThreshold(value, threshold float64, expectLower bool) bool {
	if expectLower {
		return value < threshold
	}
	return value > threshold
}

// applyThreshold adds the threshold to data item with metric specified via metricSelector and verifies that
// this metrics value is within threshold.
func applyThreshold(items []DataItem, threshold float64, metricSelector thresholdMetricSelector) error {
	if threshold == 0 {
		return nil
	}
	dataBucket := metricSelector.DataBucket
	var errs []error
	for _, item := range items {
		if item.Labels["Metric"] != metricSelector.Name || !labelsMatch(item.Labels, metricSelector.Labels) {
			continue
		}
		thresholdItemName := dataBucket + "Threshold"
		item.Data[thresholdItemName] = threshold
		dataItem, ok := item.Data[dataBucket]
		if !ok {
			errs = append(errs, fmt.Errorf("%s: no data present for %q metric %q bucket", item.Labels["Name"], metricSelector.Name, dataBucket))
			continue
		}
		if !valueWithinThreshold(dataItem, threshold, metricSelector.ExpectLower) {
			if metricSelector.ExpectLower {
				errs = append(errs, fmt.Errorf("%s: expected %q %q to be lower: got %f, want %f", item.Labels["Name"], metricSelector.Name, dataBucket, dataItem, threshold))
			} else {
				errs = append(errs, fmt.Errorf("%s: expected %q %q to be higher: got %f, want %f", item.Labels["Name"], metricSelector.Name, dataBucket, dataItem, threshold))
			}
		}
	}
	return errors.Join(errs...)
}

func checkEmptyInFlightEvents() error {
	labels := append(schedframework.AllClusterEventLabels(), metrics.PodPoppedInFlightEvent)
	for _, label := range labels {
		value, err := testutil.GetGaugeMetricValue(metrics.InFlightEvents.WithLabelValues(label))
		if err != nil {
			return fmt.Errorf("failed to get InFlightEvents metric for label %s", label)
		}
		if value > 0 {