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

     1  /*
     2  Copyright 2022 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 test
    18  
    19  import (
    20  	"sort"
    21  	"sync"
    22  	"time"
    23  
    24  	"k8s.io/perf-tests/clusterloader2/pkg/errors"
    25  )
    26  
    27  type substepResult struct {
    28  	name     string
    29  	id       int
    30  	duration time.Duration
    31  	err      *errors.ErrorList
    32  }
    33  
    34  type StepResult struct {
    35  	lock      sync.Mutex
    36  	startTime time.Time
    37  	name      string
    38  	err       *errors.ErrorList
    39  
    40  	results []substepResult
    41  }
    42  
    43  func NewStepResult(stepName string) *StepResult {
    44  	return &StepResult{
    45  		name:      stepName,
    46  		startTime: time.Now(),
    47  		results:   []substepResult{},
    48  		err:       errors.NewErrorList(),
    49  	}
    50  }
    51  
    52  func (s *StepResult) AddSubStepResult(name string, id int, err *errors.ErrorList) {
    53  	s.lock.Lock()
    54  	defer s.lock.Unlock()
    55  
    56  	duration := time.Since(s.startTime)
    57  	s.results = append(s.results, substepResult{
    58  		name:     name,
    59  		id:       id,
    60  		duration: duration,
    61  		err:      err,
    62  	})
    63  }
    64  
    65  func (s *StepResult) getAllErrorsUnsafe() *errors.ErrorList {
    66  	errList := errors.NewErrorList()
    67  	errList.Concat(s.err)
    68  	for _, value := range s.results {
    69  		errList.Concat(value.err)
    70  	}
    71  	return errList
    72  }
    73  
    74  func (s *StepResult) GetAllErrors() *errors.ErrorList {
    75  	s.lock.Lock()
    76  	defer s.lock.Unlock()
    77  
    78  	return s.getAllErrorsUnsafe()
    79  }
    80  
    81  func (s *StepResult) getAllResults() []substepResult {
    82  	s.lock.Lock()
    83  	defer s.lock.Unlock()
    84  
    85  	results := []substepResult{}
    86  	// Special case for phases that do not report substeps.
    87  	if len(s.results) == 0 {
    88  		results = append(results, substepResult{
    89  			name:     s.name,
    90  			duration: time.Since(s.startTime),
    91  			err:      s.getAllErrorsUnsafe(),
    92  		})
    93  	}
    94  
    95  	sort.Slice(s.results, func(i, j int) bool {
    96  		return s.results[i].id < s.results[j].id
    97  	})
    98  
    99  	for _, result := range s.results {
   100  		results = append(results, substepResult{
   101  			name:     s.name + " " + result.name,
   102  			duration: result.duration,
   103  			err:      result.err,
   104  		})
   105  	}
   106  
   107  	return results
   108  }
   109  
   110  func (s *StepResult) AddStepError(errs *errors.ErrorList) {
   111  	s.err.Concat(errs)
   112  }