knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/vegeta/pacers/combined_pacer.go (about)

     1  /*
     2  Copyright 2019 The Knative 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 pacers
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"strings"
    23  	"time"
    24  
    25  	vegeta "github.com/tsenart/vegeta/v12/lib"
    26  )
    27  
    28  // combinedPacer is a Pacer that combines multiple Pacers and runs them sequentially when being used for attack.
    29  type combinedPacer struct {
    30  	// pacers is a list of pacers that will be used sequentially for attack.
    31  	// MUST have more than 1 pacer.
    32  	pacers []vegeta.Pacer
    33  	// durations is the list of durations for the given Pacers.
    34  	// MUST have the same length as Pacers, and each duration MUST be longer than 1 second.
    35  	durations []time.Duration
    36  
    37  	// totalDuration is sum of the given Durations.
    38  	totalDuration uint64
    39  	// stepDurations is the accumulative duration of each step calculated from the given Durations.
    40  	stepDurations []uint64
    41  
    42  	curPacerIndex   uint
    43  	prevElapsedHits uint64
    44  	prevElapsedTime uint64
    45  }
    46  
    47  // NewCombined returns a new CombinedPacer with the given config.
    48  func NewCombined(pacers []vegeta.Pacer, durations []time.Duration) (vegeta.Pacer, error) {
    49  	if len(pacers) == 0 || len(durations) == 0 || len(pacers) != len(durations) || len(pacers) == 1 {
    50  		return nil, errors.New("configuration for this CombinedPacer is invalid")
    51  	}
    52  
    53  	var totalDuration uint64
    54  	stepDurations := make([]uint64, len(pacers))
    55  	for i, duration := range durations {
    56  		if duration < 1*time.Second {
    57  			return nil, errors.New("duration for each pacer must be longer than 1 second")
    58  		}
    59  		totalDuration += uint64(duration)
    60  		if i == 0 {
    61  			stepDurations[0] = uint64(duration)
    62  		} else {
    63  			stepDurations[i] = stepDurations[i-1] + uint64(duration)
    64  		}
    65  	}
    66  	pacer := &combinedPacer{
    67  		pacers:    pacers,
    68  		durations: durations,
    69  
    70  		totalDuration: totalDuration,
    71  		stepDurations: stepDurations,
    72  	}
    73  	return pacer, nil
    74  }
    75  
    76  // combinedPacer satisfies the Pacer interface.
    77  var _ vegeta.Pacer = &combinedPacer{}
    78  
    79  // String returns a pretty-printed description of the combinedPacer's behaviour.
    80  func (cp *combinedPacer) String() string {
    81  	var sb strings.Builder
    82  	for i := range cp.pacers {
    83  		sb.WriteString(fmt.Sprintf("Pacer: %s, Duration: %s\n", cp.pacers[i], cp.durations[i]))
    84  	}
    85  	return sb.String()
    86  }
    87  
    88  func (cp *combinedPacer) Pace(elapsedTime time.Duration, elapsedHits uint64) (time.Duration, bool) {
    89  	pacerTimeOffset := uint64(elapsedTime) % cp.totalDuration
    90  	pacerIndex := cp.pacerIndex(pacerTimeOffset)
    91  
    92  	// If it needs to switch to the next pacer, update prevElapsedTime, prevElapsedHits and curPacerIndex.
    93  	if pacerIndex != cp.curPacerIndex {
    94  		cp.prevElapsedTime = uint64(elapsedTime)
    95  		cp.prevElapsedHits = elapsedHits
    96  		cp.curPacerIndex = pacerIndex
    97  	}
    98  
    99  	// Use the adjusted elapsedTime and elapsedHits to get the time to wait for the next hit.
   100  	curPacer := cp.pacers[cp.curPacerIndex]
   101  	curElapsedTime := time.Duration(uint64(elapsedTime) - cp.prevElapsedTime)
   102  	curElapsedHits := elapsedHits - cp.prevElapsedHits
   103  	return curPacer.Pace(curElapsedTime, curElapsedHits)
   104  }
   105  
   106  func (cp *combinedPacer) Rate(elapsedTime time.Duration) float64 {
   107  	curPacer := cp.pacers[cp.curPacerIndex]
   108  	curElapsedTime := time.Duration(uint64(elapsedTime) - cp.prevElapsedTime)
   109  	return curPacer.Rate(curElapsedTime)
   110  }
   111  
   112  // pacerIndex returns the index of pacer that pacerTimeOffset falls into
   113  func (cp *combinedPacer) pacerIndex(pacerTimeOffset uint64) uint {
   114  	i, j := 0, len(cp.stepDurations)
   115  	for i < j {
   116  		m := i + (j-i)/2
   117  		if pacerTimeOffset >= cp.stepDurations[m] {
   118  			i = m + 1
   119  		} else {
   120  			j = m
   121  		}
   122  	}
   123  	return uint(i)
   124  }