|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package common |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "k8s.io/klog/v2" |
| 24 | + "k8s.io/perf-tests/clusterloader2/pkg/measurement" |
| 25 | + measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util" |
| 26 | + "k8s.io/perf-tests/clusterloader2/pkg/util" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + // prometheusSchedulingMetricsMeasurementName is the identifier used in ClusterLoader2 config files. |
| 31 | + // It intentionally differs from schedulerLatencyMetricName to let users choose which implementation |
| 32 | + // they want to run (direct scrape vs Prometheus based) without changing the output payload. |
| 33 | + prometheusSchedulingMetricsMeasurementName = "PrometheusSchedulingMetrics" |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + create := func() measurement.Measurement { |
| 38 | + return CreatePrometheusMeasurement(&prometheusSchedulerLatencyGatherer{}) |
| 39 | + } |
| 40 | + if err := measurement.Register(prometheusSchedulingMetricsMeasurementName, create); err != nil { |
| 41 | + klog.Fatalf("Cannot register %s: %v", prometheusSchedulingMetricsMeasurementName, err) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// prometheusSchedulerLatencyGatherer implements the Gatherer interface and generates the same |
| 46 | +// JSON summary as scheduler_latency.go but sources the histogram data from Prometheus instead of |
| 47 | +// scraping kube-scheduler directly. |
| 48 | +// |
| 49 | +// The Gather method uses Prometheus' increase() function to calculate bucket increments over the |
| 50 | +// test window [startTime, endTime). Those increments are then converted into Histogram structures |
| 51 | +// identical to the ones produced by the in-cluster scrape implementation, after which existing |
| 52 | +// helper code is re-used to compute the 50th/90th/99th percentiles. |
| 53 | +// |
| 54 | +// No parameters are required – the measurement automatically queries all buckets for the four |
| 55 | +// relevant histograms. |
| 56 | +// |
| 57 | +// NOTE: This measurement assumes that the kube-scheduler metrics endpoint is being scraped by |
| 58 | +// Prometheus and that the metric names match the upstream defaults. |
| 59 | + |
| 60 | +type prometheusSchedulerLatencyGatherer struct{} |
| 61 | + |
| 62 | +func (g *prometheusSchedulerLatencyGatherer) Configure(_ *measurement.Config) error { return nil } |
| 63 | +func (g *prometheusSchedulerLatencyGatherer) IsEnabled(_ *measurement.Config) bool { return true } |
| 64 | +func (g *prometheusSchedulerLatencyGatherer) String() string { |
| 65 | + return prometheusSchedulingMetricsMeasurementName |
| 66 | +} |
| 67 | + |
| 68 | +func (g *prometheusSchedulerLatencyGatherer) Gather(executor QueryExecutor, startTime, endTime time.Time, config *measurement.Config) ([]measurement.Summary, error) { |
| 69 | + window := endTime.Sub(startTime) |
| 70 | + promWindow := measurementutil.ToPrometheusTime(window) |
| 71 | + |
| 72 | + queryHistogram := func(metric string, labelFilter string) (*measurementutil.Histogram, error) { |
| 73 | + var query string |
| 74 | + if labelFilter != "" { |
| 75 | + query = fmt.Sprintf(`sum(increase(%s{%s}[%s])) by (le)`, metric, labelFilter, promWindow) |
| 76 | + } else { |
| 77 | + query = fmt.Sprintf(`sum(increase(%s[%s])) by (le)`, metric, promWindow) |
| 78 | + } |
| 79 | + |
| 80 | + samples, err := executor.Query(query, endTime) |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("failed to execute query %q: %w", query, err) |
| 83 | + } |
| 84 | + |
| 85 | + hist := measurementutil.NewHistogram(nil) |
| 86 | + for _, s := range samples { |
| 87 | + measurementutil.ConvertSampleToHistogram(s, hist) |
| 88 | + } |
| 89 | + return hist, nil |
| 90 | + } |
| 91 | + |
| 92 | + metrics := schedulerLatencyMetrics{ |
| 93 | + e2eSchedulingDurationHist: measurementutil.NewHistogram(nil), |
| 94 | + schedulingAlgorithmDurationHist: measurementutil.NewHistogram(nil), |
| 95 | + preemptionEvaluationHist: measurementutil.NewHistogram(nil), |
| 96 | + frameworkExtensionPointDurationHist: make(map[string]*measurementutil.Histogram), |
| 97 | + } |
| 98 | + |
| 99 | + for _, ep := range extentionsPoints { |
| 100 | + metrics.frameworkExtensionPointDurationHist[ep] = measurementutil.NewHistogram(nil) |
| 101 | + } |
| 102 | + |
| 103 | + var errList []error |
| 104 | + |
| 105 | + if h, err := queryHistogram(string(e2eSchedulingDurationMetricName), ""); err != nil { |
| 106 | + errList = append(errList, err) |
| 107 | + } else { |
| 108 | + metrics.e2eSchedulingDurationHist = h |
| 109 | + } |
| 110 | + |
| 111 | + if h, err := queryHistogram(string(schedulingAlgorithmDurationMetricName), ""); err != nil { |
| 112 | + errList = append(errList, err) |
| 113 | + } else { |
| 114 | + metrics.schedulingAlgorithmDurationHist = h |
| 115 | + } |
| 116 | + |
| 117 | + if h, err := queryHistogram(string(preemptionEvaluationMetricName), ""); err != nil { |
| 118 | + errList = append(errList, err) |
| 119 | + } else { |
| 120 | + metrics.preemptionEvaluationHist = h |
| 121 | + } |
| 122 | + |
| 123 | + for _, ep := range extentionsPoints { |
| 124 | + labelSel := fmt.Sprintf(`extension_point="%s"`, ep) |
| 125 | + h, err := queryHistogram(string(frameworkExtensionPointDurationMetricName), labelSel) |
| 126 | + if err != nil { |
| 127 | + errList = append(errList, err) |
| 128 | + continue |
| 129 | + } |
| 130 | + metrics.frameworkExtensionPointDurationHist[ep] = h |
| 131 | + } |
| 132 | + |
| 133 | + if len(errList) > 0 { |
| 134 | + return nil, fmt.Errorf("prometheus scheduler latency gathering errors: %v", errList) |
| 135 | + } |
| 136 | + |
| 137 | + slm := &schedulerLatencyMeasurement{} |
| 138 | + result, err := slm.setQuantiles(metrics) |
| 139 | + if err != nil { |
| 140 | + return nil, fmt.Errorf("failed to compute quantiles: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + content, err := util.PrettyPrintJSON(result) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + summaries := []measurement.Summary{ |
| 149 | + measurement.CreateSummary(config.Identifier+"_"+prometheusSchedulingMetricsMeasurementName, "json", content), |
| 150 | + } |
| 151 | + return summaries, nil |
| 152 | +} |
0 commit comments