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

     1  /*
     2  Copyright 2020 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 measurement
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/util/wait"
    23  	"k8s.io/perf-tests/clusterloader2/api"
    24  	"k8s.io/perf-tests/clusterloader2/pkg/errors"
    25  )
    26  
    27  // Execute executes a measurement, which can be a single measurement or a wrapper for multiple measurements.
    28  func Execute(mm Manager, m *api.Measurement) *errors.ErrorList {
    29  	if m.Identifier != "" {
    30  		return executeSingleMeasurement(mm, m)
    31  	}
    32  	return executeWrapperMeasurement(mm, m)
    33  }
    34  
    35  func formatError(method, identifier string, err error) error {
    36  	return fmt.Errorf("measurement call %s - %s error: %v", method, identifier, err)
    37  }
    38  
    39  func executeSingleMeasurement(mm Manager, m *api.Measurement) *errors.ErrorList {
    40  	errList := errors.NewErrorList()
    41  	if err := mm.Execute(m.Method, m.Identifier, m.Params); err != nil {
    42  		errList.Append(formatError(m.Method, m.Identifier, err))
    43  	}
    44  	return errList
    45  }
    46  
    47  func executeWrapperMeasurement(mm Manager, m *api.Measurement) *errors.ErrorList {
    48  	var wg wait.Group
    49  	errList := errors.NewErrorList()
    50  	for i := range m.Instances {
    51  		identifier := m.Instances[i].Identifier
    52  		measurementInstanceParams := m.Instances[i].Params
    53  		// clone the measurement params
    54  		measurementParams := make(map[string]interface{})
    55  		for k, v := range m.Params {
    56  			measurementParams[k] = v
    57  		}
    58  		// add/overwrite the measurement params with wrapper measurement params
    59  		for paramsKey := range measurementInstanceParams {
    60  			measurementParams[paramsKey] = measurementInstanceParams[paramsKey]
    61  		}
    62  		wg.Start(func() {
    63  			if err := mm.Execute(m.Method, identifier, measurementParams); err != nil {
    64  				errList.Append(formatError(m.Method, identifier, err))
    65  			}
    66  		})
    67  	}
    68  	wg.Wait()
    69  	return errList
    70  }