scheduler_perf: run garbage collection before measurement

The startup phase may have allocated memory that can be garbage-collected.
Forcing GC to run before measurements avoids noise if the garbage collection
kicks in during the measurement and potentially reduces the heap size reported
by metrics.

The exact effect has not been measured, it just seems useful.
This commit is contained in:
Patrick Ohly
2025-09-11 17:11:56 +02:00
parent a9fe67a1dd
commit 16fa150182

View File

@@ -29,6 +29,7 @@ import (
"os"
"path"
"regexp"
"runtime"
"slices"
"strings"
"sync"
@@ -42,7 +43,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -130,7 +131,7 @@ type FeatureGateFlag interface {
func init() {
f := featuregate.NewFeatureGate()
runtime.Must(logsapi.AddFeatureGates(f))
utilruntime.Must(logsapi.AddFeatureGates(f))
LoggingFeatureGate = f
LoggingConfig = logsapi.NewLoggingConfiguration()
@@ -1457,6 +1458,10 @@ func checkEmptyInFlightEvents() error {
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()
// The first part is the same for each workload, therefore we can strip it.
workloadName = workloadName[strings.Index(name, "/")+1:]
collectors := getTestDataCollectors(podInformer, fmt.Sprintf("%s/%s", workloadName, name), namespaces, labelSelector, mcc, throughputErrorMargin)