k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/scheduling_throughput_prometheus.go (about)

     1  /*
     2  Copyright 2018 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/errors"
    25  	"k8s.io/perf-tests/clusterloader2/pkg/measurement"
    26  	measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util"
    27  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    28  )
    29  
    30  const (
    31  	schedulingThroughputPrometheusMeasurementName = "SchedulingThroughputPrometheus"
    32  
    33  	maxSchedulingThroughputQuery = `max_over_time(sum(irate(apiserver_request_total{verb="POST", resource="pods", subresource="binding",code="201"}[1m]))[%v:5s])`
    34  )
    35  
    36  func init() {
    37  	create := func() measurement.Measurement { return CreatePrometheusMeasurement(&schedulingThroughputGatherer{}) }
    38  	if err := measurement.Register(schedulingThroughputPrometheusMeasurementName, create); err != nil {
    39  		klog.Fatalf("Cannot register %s: %v", schedulingThroughputMeasurementName, err)
    40  	}
    41  }
    42  
    43  type schedulingThroughputGatherer struct{}
    44  
    45  func (a *schedulingThroughputGatherer) Gather(executor QueryExecutor, startTime, endTime time.Time, config *measurement.Config) ([]measurement.Summary, error) {
    46  	throughputSummary, err := a.getThroughputSummary(executor, startTime, endTime, config)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	content, err := util.PrettyPrintJSON(throughputSummary)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	threshold, err := util.GetFloat64OrDefault(config.Params, "threshold", 0)
    57  	if threshold > 0 && throughputSummary.Max < threshold {
    58  		err = errors.NewMetricViolationError(
    59  			"scheduler throughput_prometheus",
    60  			fmt.Sprintf("actual throughput %f lower than threshold %f", throughputSummary.Max, threshold))
    61  	}
    62  
    63  	summaries := []measurement.Summary{
    64  		measurement.CreateSummary(a.String(), "json", content),
    65  	}
    66  
    67  	return summaries, err
    68  }
    69  
    70  func (a *schedulingThroughputGatherer) getThroughputSummary(executor QueryExecutor, startTime, endTime time.Time, config *measurement.Config) (*schedulingThroughputPrometheus, error) {
    71  	measurementDuration := endTime.Sub(startTime)
    72  	promDuration := measurementutil.ToPrometheusTime(measurementDuration)
    73  	query := fmt.Sprintf(maxSchedulingThroughputQuery, promDuration)
    74  
    75  	samples, err := executor.Query(query, endTime)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	if len(samples) != 1 {
    80  		return nil, fmt.Errorf("got unexpected number of samples: %d", len(samples))
    81  	}
    82  
    83  	maxSchedulingThroughput := samples[0].Value
    84  	throughputSummary := &schedulingThroughputPrometheus{
    85  		Max: float64(maxSchedulingThroughput),
    86  	}
    87  
    88  	return throughputSummary, nil
    89  }
    90  
    91  func (a *schedulingThroughputGatherer) String() string {
    92  	return schedulingThroughputPrometheusMeasurementName
    93  }
    94  
    95  func (a *schedulingThroughputGatherer) Configure(config *measurement.Config) error {
    96  	return nil
    97  }
    98  
    99  func (a *schedulingThroughputGatherer) IsEnabled(config *measurement.Config) bool {
   100  	return true
   101  }
   102  
   103  type schedulingThroughputPrometheus struct {
   104  	Max float64 `json:"max"`
   105  }