How to use sumStagesDuration method of executor Package

Best K6 code snippet using executor.sumStagesDuration

ramping_arrival_rate.go

Source:ramping_arrival_rate.go Github

copy

Full Screen

...84 maxArrRatePerSec, _ := getArrivalRatePerSec(85 getScaledArrivalRate(et.Segment, maxUnscaledRate, varc.TimeUnit.TimeDuration()),86 ).Float64()87 return fmt.Sprintf("Up to %.2f iterations/s for %s over %d stages%s",88 maxArrRatePerSec, sumStagesDuration(varc.Stages),89 len(varc.Stages), varc.getBaseInfo(maxVUsRange))90}91// Validate makes sure all options are configured and valid92func (varc *RampingArrivalRateConfig) Validate() []error {93 errors := varc.BaseConfig.Validate()94 if varc.StartRate.Int64 < 0 {95 errors = append(errors, fmt.Errorf("the startRate value can't be negative"))96 }97 if varc.TimeUnit.TimeDuration() < 0 {98 errors = append(errors, fmt.Errorf("the timeUnit must be more than 0"))99 }100 errors = append(errors, validateStages(varc.Stages)...)101 if !varc.PreAllocatedVUs.Valid {102 errors = append(errors, fmt.Errorf("the number of preAllocatedVUs isn't specified"))103 } else if varc.PreAllocatedVUs.Int64 < 0 {104 errors = append(errors, fmt.Errorf("the number of preAllocatedVUs can't be negative"))105 }106 if !varc.MaxVUs.Valid {107 // TODO: don't change the config while validating108 varc.MaxVUs.Int64 = varc.PreAllocatedVUs.Int64109 } else if varc.MaxVUs.Int64 < varc.PreAllocatedVUs.Int64 {110 errors = append(errors, fmt.Errorf("maxVUs can't be less than preAllocatedVUs"))111 }112 return errors113}114// GetExecutionRequirements returns the number of required VUs to run the115// executor for its whole duration (disregarding any startTime), including the116// maximum waiting time for any iterations to gracefully stop. This is used by117// the execution scheduler in its VU reservation calculations, so it knows how118// many VUs to pre-initialize.119func (varc RampingArrivalRateConfig) GetExecutionRequirements(et *lib.ExecutionTuple) []lib.ExecutionStep {120 return []lib.ExecutionStep{121 {122 TimeOffset: 0,123 PlannedVUs: uint64(et.ScaleInt64(varc.PreAllocatedVUs.Int64)),124 MaxUnplannedVUs: uint64(et.ScaleInt64(varc.MaxVUs.Int64 - varc.PreAllocatedVUs.Int64)),125 },126 {127 TimeOffset: sumStagesDuration(varc.Stages) + varc.GracefulStop.TimeDuration(),128 PlannedVUs: 0,129 MaxUnplannedVUs: 0,130 },131 }132}133// NewExecutor creates a new RampingArrivalRate executor134func (varc RampingArrivalRateConfig) NewExecutor(135 es *lib.ExecutionState, logger *logrus.Entry,136) (lib.Executor, error) {137 return &RampingArrivalRate{138 BaseExecutor: NewBaseExecutor(&varc, es, logger),139 config: varc,140 }, nil141}142// HasWork reports whether there is any work to be done for the given execution segment.143func (varc RampingArrivalRateConfig) HasWork(et *lib.ExecutionTuple) bool {144 return varc.GetMaxVUs(et) > 0145}146// RampingArrivalRate tries to execute a specific number of iterations for a147// specific period.148// TODO: combine with the ConstantArrivalRate?149type RampingArrivalRate struct {150 *BaseExecutor151 config RampingArrivalRateConfig152 et *lib.ExecutionTuple153}154// Make sure we implement the lib.Executor interface.155var _ lib.Executor = &RampingArrivalRate{}156// Init values needed for the execution157func (varr *RampingArrivalRate) Init(ctx context.Context) error {158 // err should always be nil, because Init() won't be called for executors159 // with no work, as determined by their config's HasWork() method.160 et, err := varr.BaseExecutor.executionState.ExecutionTuple.GetNewExecutionTupleFromValue(varr.config.MaxVUs.Int64)161 varr.et = et162 varr.iterSegIndex = lib.NewSegmentedIndex(et)163 return err //nolint: wrapcheck164}165// cal calculates the transtitions between stages and gives the next full value produced by the166// stages. In this explanation we are talking about events and in practice those events are starting167// of an iteration, but could really be anything that needs to occur at a constant or linear rate.168//169// The basic idea is that we make a graph with the X axis being time and the Y axis being170// events/s we know that the area of the figure between the graph and the X axis is equal to the171// amount of events done - we multiply time by events per time so we get events ...172// Mathematics :).173//174// Lets look at a simple example - lets say we start with 2 events and the first stage is 5175// seconds to 2 events/s and then we have a second stage for 5 second that goes up to 3 events176// (using small numbers because ... well it is easier :D). This will look something like:177// ^178// 7|179// 6|180// 5|181// 4|182// 3| ,-+183// 2|----+-' |184// 1| | |185// +----+----+---------------------------------->186// 0s 5s 10s187// TODO: bigger and more stages188//189// Now the question is when(where on the graph) does the first event happen? Well in this simple190// case it is easy it will be at 0.5 seconds as we are doing 2 events/s. If we want to know when191// event n will happen we need to calculate n = 2 * x, where x is the time it will happen, so we192// need to calculate x = n/2as we are interested in the time, x.193// So if we just had a constant function for each event n we can calculate n/2 and find out when194// it needs to start.195// As we can see though the graph changes as stages change. But we can calculate how many events196// each stage will have, again it is the area from the start of the stage to it's end and between197// the graph and the X axis. So in this case we know that the first stage will have 10 full events198// in it and no more or less. So we are trying to find out when the 12 event will happen the answer199// will be after the 5th second.200//201// The graph doesn't show this well but we are ramping up linearly (we could possibly add202// other ramping up/down functions later). So at 7.5 seconds for example we should be doing 2.5203// events/s. You could start slicing the graph constantly and in this way to represent the ramping204// up/down as a multiple constant functions, and you will get mostly okayish results. But here is205// where calculus comes into play. Calculus gives us a way of exactly calculate the area for any206// given function and linear ramp up/downs just happen to be pretty easy(actual math prove in207// https://github.com/k6io/k6/issues/1299#issuecomment-575661084).208//209// One tricky last point is what happens if stage only completes 9.8 events? Let's say that the210// first stage above was 4.9 seconds long 2 * 4.9 is 9.8, we have 9 events and .8 of an event, what211// do with do with that? Well the 10th even will happen in the next stage (if any) and will happen212// when the are from the start till time x is 0.2 (instead of 1) as 0.2 + 0.8 is 10. So the 12th for213// example will be when the area is 2.2 as 9.8+2.2. So we just carry this around.214//215// So in the end what calis doing is to get formulas which will tell it when216// a given event n in order will happen. It helps itself by knowing that in a given217// stage will do some given amount (the area of the stage) events and if we past that one we218// know we are not in that stage.219//220// The specific implementation here can only go forward and does incorporate221// the striping algorithm from the lib.ExecutionTuple for additional speed up but this could222// possibly be refactored if need for this arises.223func (varc RampingArrivalRateConfig) cal(et *lib.ExecutionTuple, ch chan<- time.Duration) {224 start, offsets, _ := et.GetStripedOffsets()225 li := -1226 // TODO: move this to a utility function, or directly what GetStripedOffsets uses once we see everywhere we will use it227 next := func() int64 {228 li++229 return offsets[li%len(offsets)]230 }231 defer close(ch) // TODO: maybe this is not a good design - closing a channel we get232 var (233 stageStart time.Duration234 timeUnit = float64(varc.TimeUnit.Duration)235 doneSoFar, endCount, to, dur float64236 from = float64(varc.StartRate.ValueOrZero()) / timeUnit237 // start .. starts at 0 but the algorithm works with area so we need to start from 1 not 0238 i = float64(start + 1)239 )240 for _, stage := range varc.Stages {241 to = float64(stage.Target.ValueOrZero()) / timeUnit242 dur = float64(stage.Duration.Duration)243 if from != to { // ramp up/down244 endCount += dur * ((to-from)/2 + from)245 for ; i <= endCount; i += float64(next()) {246 // TODO: try to twist this in a way to be able to get i (the only changing part)247 // somewhere where it is less in the middle of the equation248 x := (from*dur - noNegativeSqrt(dur*(from*from*dur+2*(i-doneSoFar)*(to-from)))) / (from - to)249 ch <- time.Duration(x) + stageStart250 }251 } else {252 endCount += dur * to253 for ; i <= endCount; i += float64(next()) {254 ch <- time.Duration((i-doneSoFar)/to) + stageStart255 }256 }257 doneSoFar = endCount258 from = to259 stageStart += stage.Duration.TimeDuration()260 }261}262// This is needed because, on some platforms (arm64), sometimes, even though we263// in *reality* don't get negative results due to the nature of how float64 is264// implemented, we get negative values (very close to the 0). This would get an265// sqrt which is *even* smaller and likely will have negligible effects on the266// final result.267//268// TODO: this is probably going to be less necessary if we do some kind of of269// optimization above and the operations with the float64 are more "accurate"270// even on arm platforms.271func noNegativeSqrt(f float64) float64 {272 if !math.Signbit(f) {273 return math.Sqrt(f)274 }275 return 0276}277// Run executes a variable number of iterations per second.278//279// TODO: Split this up and make an independent component that can be reused280// between the constant and ramping arrival rate executors - that way we can281// keep the complexity in one well-architected part (with short methods and few282// lambdas :D), while having both config frontends still be present for maximum283// UX benefits. Basically, keep the progress bars and scheduling (i.e. at what284// time should iteration X begin) different, but keep everyhing else the same.285// This will allow us to implement https://github.com/k6io/k6/issues/1386286// and things like all of the TODOs below in one place only.287//nolint:funlen,cyclop288func (varr RampingArrivalRate) Run(parentCtx context.Context, out chan<- metrics.SampleContainer) (err error) {289 segment := varr.executionState.ExecutionTuple.Segment290 gracefulStop := varr.config.GetGracefulStop()291 duration := sumStagesDuration(varr.config.Stages)292 preAllocatedVUs := varr.config.GetPreAllocatedVUs(varr.executionState.ExecutionTuple)293 maxVUs := varr.config.GetMaxVUs(varr.executionState.ExecutionTuple)294 // TODO: refactor and simplify295 timeUnit := varr.config.TimeUnit.TimeDuration()296 startArrivalRate := getScaledArrivalRate(segment, varr.config.StartRate.Int64, timeUnit)297 maxUnscaledRate := getStagesUnscaledMaxTarget(varr.config.StartRate.Int64, varr.config.Stages)298 maxArrivalRatePerSec, _ := getArrivalRatePerSec(getScaledArrivalRate(segment, maxUnscaledRate, timeUnit)).Float64()299 startTickerPeriod := getTickerPeriod(startArrivalRate)300 // Make sure the log and the progress bar have accurate information301 varr.logger.WithFields(logrus.Fields{302 "maxVUs": maxVUs, "preAllocatedVUs": preAllocatedVUs, "duration": duration, "numStages": len(varr.config.Stages),303 "startTickerPeriod": startTickerPeriod.Duration, "type": varr.config.GetType(),304 }).Debug("Starting executor run...")305 activeVUsWg := &sync.WaitGroup{}...

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1import (2type Executor struct {3}4type Stage struct {5}6func main() {7 e := Executor{8 Stages: []Stage{9 {Duration: time.Second},10 {Duration: time.Second},11 },12 }13 fmt.Println(e.sumStagesDuration())14}15import "time"16func (e Executor) sumStagesDuration() time.Duration {17 for _, stage := range e.Stages {18 }19}20./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)21./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)22./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)23./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)24./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)25./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)26./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)27./2.go:6:6: e.sumStagesDuration undefined (type Executor has no field or method sumStagesDuration)

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1executor := NewExecutor()2executor.sumStagesDuration()3executor := NewExecutor()4executor.sumStagesDuration()5executor := NewExecutor()6executor.sumStagesDuration()7executor := NewExecutor()8executor.sumStagesDuration()9executor := NewExecutor()10executor.sumStagesDuration()11executor := NewExecutor()12executor.sumStagesDuration()13executor := NewExecutor()14executor.sumStagesDuration()15executor := NewExecutor()16executor.sumStagesDuration()17executor := NewExecutor()18executor.sumStagesDuration()19executor := NewExecutor()20executor.sumStagesDuration()21executor := NewExecutor()22executor.sumStagesDuration()23executor := NewExecutor()24executor.sumStagesDuration()25executor := NewExecutor()26executor.sumStagesDuration()27executor := NewExecutor()28executor.sumStagesDuration()29executor := NewExecutor()30executor.sumStagesDuration()31executor := NewExecutor()32executor.sumStagesDuration()

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 e := executor{4 stage1: stage{5 start: time.Now(),6 end: time.Now().Add(1 * time.Second),7 },8 stage2: stage{9 start: time.Now().Add(1 * time.Second),10 end: time.Now().Add(2 * time.Second),11 },12 stage3: stage{13 start: time.Now().Add(2 * time.Second),14 end: time.Now().Add(3 * time.Second),15 },16 }17 fmt.Println(e.sumStagesDuration())18}19import (20type executor struct {21}22type stage struct {23}24func (e executor) sumStagesDuration() time.Duration {25 return e.stage1.end.Sub(e.stage1.start) +26 e.stage2.end.Sub(e.stage2.start) +27 e.stage3.end.Sub(e.stage3.start)28}29func main() {30 e := executor{31 stage1: stage{32 start: time.Now(),33 end: time.Now().Add(1 * time.Second),34 },35 stage2: stage{36 start: time.Now().Add(1 * time.Second),37 end: time.Now().Add(2 * time.Second),38 },39 stage3: stage{40 start: time.Now().Add(2 * time.Second),41 end: time.Now().Add(3 * time.Second),42 },43 }44 fmt.Println(e.sumStagesDuration())45}46import (47type executor struct {48}49type stage struct {50}51func (e *executor) sumStagesDuration() time.Duration {52 return e.stage1.end.Sub(e.stage1.start) +53 e.stage2.end.Sub(e.stage2.start) +54 e.stage3.end.Sub(e.stage3.start)55}56func main() {57 e := executor{58 stage1: stage{59 start: time.Now(),60 end: time.Now().Add(1

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1func main() {2 var executor = new(Executor)3 var stages = []Stage{4 Stage{Duration: 1},5 Stage{Duration: 2},6 Stage{Duration: 3},7 }8 var sum = executor.sumStagesDuration(stages)9 fmt.Println(sum)10}11func main() {12 var executor = new(Executor)13 var stages = []Stage{14 Stage{Duration: 1},15 Stage{Duration: 2},16 Stage{Duration: 3},17 }18 var sum = executor.sumStagesDuration(stages)19 fmt.Println(sum)20}21func main() {22 var executor = new(Executor)23 var stages = []Stage{24 Stage{Duration: 1},25 Stage{Duration: 2},26 Stage{Duration: 3},27 }28 var sum = executor.sumStagesDuration(stages)29 fmt.Println(sum)30}31func main() {32 var executor = new(Executor)33 var stages = []Stage{34 Stage{Duration: 1},35 Stage{Duration: 2},36 Stage{Duration: 3},37 }38 var sum = executor.sumStagesDuration(stages)39 fmt.Println(sum)40}41func main() {42 var executor = new(Executor)43 var stages = []Stage{44 Stage{Duration: 1},45 Stage{Duration: 2},46 Stage{Duration: 3},47 }48 var sum = executor.sumStagesDuration(stages)49 fmt.Println(sum)50}51func main() {52 var executor = new(Executor)53 var stages = []Stage{54 Stage{Duration: 1},55 Stage{Duration: 2},56 Stage{Duration: 3},57 }58 var sum = executor.sumStagesDuration(stages)59 fmt.Println(sum)60}

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 e := executor{}5 e.addStage(1, 2, 3)6 e.addStage(4, 5, 6)7 e.addStage(7, 8, 9)8 fmt.Println(e.sumStagesDuration())9}10import (11func main() {12 fmt.Println("Hello, playground")13 e := executor{}14 e.addStage(1, 2, 3)15 e.addStage(4, 5, 6)16 e.addStage(7, 8, 9)17 fmt.Println(e.sumStagesDuration())18}19import (20func main() {21 fmt.Println("Hello, playground")22 e := executor{}23 e.addStage(1, 2, 3)24 e.addStage(4, 5, 6)25 e.addStage(7, 8, 9)26 fmt.Println(e.sumStagesDuration())27}28import (29func main() {30 fmt.Println("Hello, playground")31 e := executor{}32 e.addStage(1, 2, 3)33 e.addStage(4, 5, 6)34 e.addStage(7, 8, 9)35 fmt.Println(e.sumStagesDuration())36}37import (38func main() {39 fmt.Println("Hello, playground")40 e := executor{}41 e.addStage(1, 2, 3)42 e.addStage(4, 5, 6)43 e.addStage(7, 8, 9)44 fmt.Println(e.sumStagesDuration())45}46import (47func main() {48 fmt.Println("Hello, playground")49 e := executor{}50 e.addStage(1, 2, 3)51 e.addStage(4, 5, 6)52 e.addStage(7

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 executor = new(ExecutorImpl)4 executor.sumStagesDuration()5 fmt.Println("Total time taken by executor to run stages is: ", executor.sumStagesDuration())6}7import (8func main() {9 executor = new(ExecutorImpl)10 executor.sumStagesDuration()11 fmt.Println("Total time taken by executor to run stages is: ", executor.sumStagesDuration())12}

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 e := NewExecutor()4 s := NewStage("stage1", 1)5 s2 := NewStage("stage2", 2)6 e.AddStage(s)7 e.AddStage(s2)8 e.StartStage("stage1")9 time.Sleep(2 * time.Second)10 e.StartStage("stage2")11 time.Sleep(2 * time.Second)12 e.EndStage("stage1")13 time.Sleep(2 * time.Second)14 e.EndStage("stage2")15 fmt.Println(e.sumStagesDuration())16}17import (18func main() {19 e := NewExecutor()20 s := NewStage("stage1", 1)21 s2 := NewStage("stage2", 2)22 e.AddStage(s)23 e.AddStage(s2)24 e.StartStage("stage1")25 time.Sleep(2 * time.Second)26 e.StartStage("stage2")27 time.Sleep(2 * time.Second)28 e.EndStage("stage1")29 time.Sleep(2 * time.Second)30 e.EndStage("stage2")31 fmt.Println(e.sumStagesDuration())32 e.StartStage("stage1")33 time.Sleep(2 * time.Second)

Full Screen

Full Screen

sumStagesDuration

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 executor := NewExecutor(5, 1000)4 stage := NewStage(1, "stage1", 10, 1000)5 executor.AddStage(stage)6 stage = NewStage(2, "stage2", 10, 1000)7 executor.AddStage(stage)8 stage = NewStage(3, "stage3", 10, 1000)9 executor.AddStage(stage)10 stage = NewStage(4, "stage4", 10, 1000)11 executor.AddStage(stage)12 stage = NewStage(5, "stage5", 10, 1000)13 executor.AddStage(stage)14 stage = NewStage(6, "stage6", 10, 1000)15 executor.AddStage(stage)16 stage = NewStage(7, "stage7", 10, 1000)17 executor.AddStage(stage)18 stage = NewStage(8, "stage8", 10, 1000)19 executor.AddStage(stage)20 stage = NewStage(9, "stage9", 10, 1000)21 executor.AddStage(stage)22 stage = NewStage(10, "stage10", 10, 1000)23 executor.AddStage(stage)24 stage = NewStage(11, "stage11", 10, 1000)25 executor.AddStage(stage)26 stage = NewStage(12, "stage12",

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful