Best K6 code snippet using executor.GetGracefulRampDown
ramping_vus.go
Source:ramping_vus.go
...69// GetStartVUs is just a helper method that returns the scaled starting VUs.70func (vlvc RampingVUsConfig) GetStartVUs(et *lib.ExecutionTuple) int64 {71 return et.ScaleInt64(vlvc.StartVUs.Int64)72}73// GetGracefulRampDown is just a helper method that returns the graceful74// ramp-down period as a standard Go time.Duration value...75func (vlvc RampingVUsConfig) GetGracefulRampDown() time.Duration {76 return time.Duration(vlvc.GracefulRampDown.Duration)77}78// GetDescription returns a human-readable description of the executor options79func (vlvc RampingVUsConfig) GetDescription(et *lib.ExecutionTuple) string {80 maxVUs := et.ScaleInt64(getStagesUnscaledMaxTarget(vlvc.StartVUs.Int64, vlvc.Stages))81 return fmt.Sprintf("Up to %d looping VUs for %s over %d stages%s",82 maxVUs, sumStagesDuration(vlvc.Stages), len(vlvc.Stages),83 vlvc.getBaseInfo(fmt.Sprintf("gracefulRampDown: %s", vlvc.GetGracefulRampDown())))84}85// Validate makes sure all options are configured and valid86func (vlvc RampingVUsConfig) Validate() []error {87 errors := vlvc.BaseConfig.Validate()88 if vlvc.StartVUs.Int64 < 0 {89 errors = append(errors, fmt.Errorf("the number of start VUs shouldn't be negative"))90 }91 if getStagesUnscaledMaxTarget(vlvc.StartVUs.Int64, vlvc.Stages) <= 0 {92 errors = append(errors, fmt.Errorf("either startVUs or one of the stages' target value should be greater than 0"))93 }94 return append(errors, validateStages(vlvc.Stages)...)95}96// getRawExecutionSteps calculates and returns as execution steps the number of97// actively running VUs the executor should have at every moment.98//99// It doesn't take into account graceful ramp-downs. It also doesn't deal with100// the end-of-executor drop to 0 VUs, whether graceful or not. These are101// handled by GetExecutionRequirements(), which internally uses this method and102// reserveVUsForGracefulRampDowns().103//104// The zeroEnd argument tells the method if we should artificially add a step105// with 0 VUs at offset sum(stages.duration), i.e. when the executor is106// supposed to end.107//108// It's also important to note how scaling works. Say, we ramp up from 0 to 10109// VUs over 10 seconds and then back to 0, and we want to split the execution in110// 2 equal segments (i.e. execution segments "0:0.5" and "0.5:1"). The original111// execution steps would look something like this:112//113// VUs ^114// 10| *115// 9| ***116// 8| *****117// 7| *******118// 6| *********119// 5| ***********120// 4| *************121// 3| ***************122// 2| *****************123// 1| *******************124// 0------------------------> time(s)125// 01234567890123456789012 (t%10)126// 00000000001111111111222 (t/10)127//128// The chart for one of the execution segments would look like this:129//130// VUs ^131// 5| XXX132// 4| XXXXXXX133// 3| XXXXXXXXXXX134// 2| XXXXXXXXXXXXXXX135// 1| XXXXXXXXXXXXXXXXXXX136// 0------------------------> time(s)137// 01234567890123456789012 (t%10)138// 00000000001111111111222 (t/10)139//140// And the chart for the other execution segment would look like this:141//142// VUs ^143// 5| Y144// 4| YYYYY145// 3| YYYYYYYYY146// 2| YYYYYYYYYYYYY147// 1| YYYYYYYYYYYYYYYYY148// 0------------------------> time(s)149// 01234567890123456789012 (t%10)150// 00000000001111111111222 (t/10)151//152// Notice the time offsets and the slower ramping up and down. All of that is153// because the sum of the two execution segments has to produce exactly the154// original shape, as if the test ran on a single machine:155//156// VUs ^157// 10| Y158// 9| XXX159// 8| YYYYY160// 7| XXXXXXX161// 6| YYYYYYYYY162// 5| XXXXXXXXXXX163// 4| YYYYYYYYYYYYY164// 3| XXXXXXXXXXXXXXX165// 2| YYYYYYYYYYYYYYYYY166// 1| XXXXXXXXXXXXXXXXXXX167// 0------------------------> time(s)168// 01234567890123456789012 (t%10)169// 00000000001111111111222 (t/10)170//171// More information: https://github.com/k6io/k6/issues/997#issuecomment-484416866172func (vlvc RampingVUsConfig) getRawExecutionSteps(et *lib.ExecutionTuple, zeroEnd bool) []lib.ExecutionStep {173 var (174 timeTillEnd time.Duration175 fromVUs = vlvc.StartVUs.Int64176 steps = make([]lib.ExecutionStep, 0, vlvc.precalculateTheRequiredSteps(et, zeroEnd))177 index = lib.NewSegmentedIndex(et)178 )179 // Reserve the scaled StartVUs at the beginning180 scaled, unscaled := index.GoTo(fromVUs)181 steps = append(steps, lib.ExecutionStep{TimeOffset: 0, PlannedVUs: uint64(scaled)})182 addStep := func(timeOffset time.Duration, plannedVUs uint64) {183 if steps[len(steps)-1].PlannedVUs != plannedVUs {184 steps = append(steps, lib.ExecutionStep{TimeOffset: timeOffset, PlannedVUs: plannedVUs})185 }186 }187 for _, stage := range vlvc.Stages {188 stageEndVUs := stage.Target.Int64189 stageDuration := time.Duration(stage.Duration.Duration)190 timeTillEnd += stageDuration191 stageVUDiff := stageEndVUs - fromVUs192 if stageVUDiff == 0 {193 continue194 }195 if stageDuration == 0 {196 scaled, unscaled = index.GoTo(stageEndVUs)197 addStep(timeTillEnd, uint64(scaled))198 fromVUs = stageEndVUs199 continue200 }201 // VU reservation for gracefully ramping down is handled as a202 // separate method: reserveVUsForGracefulRampDowns()203 if unscaled > stageEndVUs { // ramp down204 // here we don't want to emit for the equal to stageEndVUs as it doesn't go below it205 // it will just go to it206 for ; unscaled > stageEndVUs; scaled, unscaled = index.Prev() {207 addStep(208 // this is the time that we should go up 1 if we are ramping up209 // but we are ramping down so we should go 1 down, but because we want to not210 // stop VUs immediately we stop it on the next unscaled VU's time211 timeTillEnd-time.Duration(int64(stageDuration)*(stageEndVUs-unscaled+1)/stageVUDiff),212 uint64(scaled-1),213 )214 }215 } else {216 for ; unscaled <= stageEndVUs; scaled, unscaled = index.Next() {217 addStep(218 timeTillEnd-time.Duration(int64(stageDuration)*(stageEndVUs-unscaled)/stageVUDiff),219 uint64(scaled),220 )221 }222 }223 fromVUs = stageEndVUs224 }225 if zeroEnd && steps[len(steps)-1].PlannedVUs != 0 {226 // If the last PlannedVUs value wasn't 0, add a last step with 0227 steps = append(steps, lib.ExecutionStep{TimeOffset: timeTillEnd, PlannedVUs: 0})228 }229 return steps230}231func absInt64(a int64) int64 {232 if a < 0 {233 return -a234 }235 return a236}237func (vlvc RampingVUsConfig) precalculateTheRequiredSteps(et *lib.ExecutionTuple, zeroEnd bool) int {238 p := et.ScaleInt64(vlvc.StartVUs.Int64)239 var result int64240 result++ // for the first one241 if zeroEnd {242 result++ // for the last one - this one can be more then needed243 }244 for _, stage := range vlvc.Stages {245 stageEndVUs := et.ScaleInt64(stage.Target.Int64)246 if stage.Duration.Duration == 0 {247 result++248 } else {249 result += absInt64(p - stageEndVUs)250 }251 p = stageEndVUs252 }253 return int(result)254}255// If the graceful ramp-downs are enabled, we need to reserve any VUs that may256// potentially have to finish running iterations when we're scaling their number257// down. This would prevent attempts from other executors to use them while the258// iterations are finishing up during their allotted gracefulRampDown periods.259//260// But we also need to be careful to not over-allocate more VUs than we actually261// need. We should never have more PlannedVUs than the max(startVUs,262// stage[n].target), even if we're quickly scaling VUs up and down multiple263// times, one after the other. In those cases, any previously reserved VUs264// finishing up interrupted iterations should be reused by the executor,265// instead of new ones being requested from the execution state.266//267// Here's an example with graceful ramp-down (i.e. "uninterruptible"268// iterations), where stars represent actively scheduled VUs and dots are used269// for VUs that are potentially finishing up iterations:270//271//272// ^273// |274// VUs 6| *..............................275// 5| ***.......*..............................276// 4|*****.....***.....**..............................277// 3|******...*****...***..............................278// 2|*******.*******.****..............................279// 1|***********************..............................280// 0--------------------------------------------------------> time(s)281// 012345678901234567890123456789012345678901234567890123 (t%10)282// 000000000011111111112222222222333333333344444444445555 (t/10)283//284// We start with 4 VUs, scale to 6, scale down to 1, scale up to 5, scale down285// to 1 again, scale up to 4, back to 1, and finally back down to 0. If our286// gracefulStop timeout was 30s (the default), then we'll stay with 6 PlannedVUs287// until t=32 in the test above, and the actual executor could run until t=52.288// See TestRampingVUsConfigExecutionPlanExample() for the above example289// as a unit test.290//291// The algorithm we use below to reserve VUs so that ramping-down VUs can finish292// their last iterations is pretty simple. It just traverses the raw execution293// steps and whenever there's a scaling down of VUs, it prevents the number of294// VUs from decreasing for the configured gracefulRampDown period.295//296// Finishing up the test, i.e. making sure we have a step with 0 VUs at time297// executorEndOffset, is not handled here. Instead GetExecutionRequirements()298// takes care of that. But to make its job easier, this method won't add any299// steps with an offset that's greater or equal to executorEndOffset.300func (vlvc RampingVUsConfig) reserveVUsForGracefulRampDowns( //nolint:funlen301 rawSteps []lib.ExecutionStep, executorEndOffset time.Duration,302) []lib.ExecutionStep {303 rawStepsLen := len(rawSteps)304 gracefulRampDownPeriod := vlvc.GetGracefulRampDown()305 newSteps := []lib.ExecutionStep{}306 lastPlannedVUs := uint64(0)307 lastDownwardSlopeStepNum := 0308 for rawStepNum := 0; rawStepNum < rawStepsLen; rawStepNum++ {309 rawStep := rawSteps[rawStepNum]310 // Add the first step or any step where the number of planned VUs is311 // greater than the ones in the previous step. We don't need to worry312 // about reserving time for ramping-down VUs when the number of planned313 // VUs is growing. That's because the gracefulRampDown period is a fixed314 // value and any timeouts from early steps with fewer VUs will get315 // overshadowed by timeouts from latter steps with more VUs.316 if rawStepNum == 0 || rawStep.PlannedVUs > lastPlannedVUs {317 newSteps = append(newSteps, rawStep)318 lastPlannedVUs = rawStep.PlannedVUs...
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 exec := executor.New(4 executor.WithUpdates(updates.HandlerFunc(func(_ updates.Interface) error {5 })),6 executor.WithMessages(messages.HandlerFunc(func(_ messages.Interface) error {7 })),8 executor.WithEvents(events.HandlerFunc(func(_ events.Interface) error {9 })),10 executor.WithCalls(calls.HandlerFunc(func(_ calls.Interface) (mesos.Response, error) {11 })),12 gracefulRampDown := exec.GetGracefulRampDown()13 fmt.Println("The graceful ramp down value is: ", gracefulRampDown)14}15import (16func main() {17 exec := executor.New(18 executor.WithUpdates(updates.HandlerFunc(func(_ updates.Interface) error {19 })),20 executor.WithMessages(messages.HandlerFunc(func(_ messages.Interface) error {21 })),
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 executor := executor.New(4 executor.Driver(executor.NewMesosExecutorDriver("default")),5 executor.TaskFunc(func(task *mesos.TaskInfo) {6 fmt.Println("Graceful Ramp Down: ", executor.GetGracefulRampDown())7 }),8 executor.Run()9}
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 executor := executor.New(executor.Config{})4 gracefulRampdown := executor.GetGracefulRampDown()5 fmt.Println(gracefulRampdown)6}7import (8func main() {9 executor := executor.New(executor.Config{})10 gracefulShutdown := executor.GetGracefulShutdown()11 fmt.Println(gracefulShutdown)12}13import (14func main() {15 executor := executor.New(executor.Config{})16 executorID := executor.GetID()17 fmt.Println(executorID)18}19import (20func main() {21 executor := executor.New(executor.Config{})22 executorInfo := executor.GetInfo()23 fmt.Println(executorInfo)24}25import (
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 klog.InitFlags(nil)4 patchHandler := &patch.AdmissionHandler{5 SparkPodPatch: func(pod *v1.Pod, _ *v1beta2.SparkApplication) (*patch.PodPatch, error) {6 return &patch.PodPatch{7 PatchOps: []patch.PodPatchOperation{8 {9 Value: v1.EnvVar{Name: "FOO", Value: "bar"},10 },11 },12 }, nil13 },14 SparkDriverPodPatch: func(pod *v1.Pod, _ *v1beta2.SparkApplication) (*patch.PodPatch, error) {15 return &patch.PodPatch{16 PatchOps: []patch.PodPatchOperation{17 {18 Value: v1.EnvVar{Name: "FOO", Value: "bar"},19 },20 },21 }, nil22 },23 SparkExecutorPodPatch: func(pod *v1.Pod, app *v1beta2.SparkApplication) (*patch.PodPatch, error) {24 executorID, err := util.GetExecutorID(pod)25 if err != nil {26 }27 gracefulRampDown := config.GetGracefulRampDown(app)28 return &patch.PodPatch{29 PatchOps: []patch.PodPatchOperation{30 {
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 host, err := sysinfo.Host()4 if err != nil {5 panic(err)6 }7 fmt.Printf("Graceful Ramp Down: %v8", host.OperatingSystem().GetGracefulRampDown())9}
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 gracefulRampDown := executor.GetGracefulRampDown()4 fmt.Println(gracefulRampDown)5}6import (7func main() {8 gracefulRampDown := executor.GetGracefulRampDown()9 fmt.Println(gracefulRampDown)10 fmt.Println(gracefulRampDown)11}
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 fmt.Println(executor.GetGracefulRampDown())4}5import (6func main() {7 fmt.Println(executor.GetGracefulShutdownTimeout())8}9import (10func main() {11 fmt.Println(executor.GetGracefulTerminationTimeout())12}13import (14func main() {15 fmt.Println(executor.GetMaxGracefulTerminationTimeout())16}17import (18func main() {19 fmt.Println(executor.GetMinGracefulTerminationTimeout())20}21import (22func main() {23 fmt.Println(executor.GetMinGracefulTerminationTimeout())24}25import (26func main() {27 fmt.Println(executor.GetShutdownGracePeriod())28}
GetGracefulRampDown
Using AI Code Generation
1func main() {2 executor := NewExecutor()3 gracePeriod := executor.GetGracefulRampDown()4 fmt.Println(gracePeriod)5}6func main() {7 executor := NewExecutor()8 gracePeriod := executor.GetGracefulRampDown()9 fmt.Println(gracePeriod)10}11func main() {12 executor := NewExecutor()13 gracePeriod := executor.GetGracefulRampDown()14 fmt.Println(gracePeriod)15}16func main() {17 executor := NewExecutor()18 gracePeriod := executor.GetGracefulRampDown()19 fmt.Println(gracePeriod)20}21func main() {22 executor := NewExecutor()23 gracePeriod := executor.GetGracefulRampDown()24 fmt.Println(gracePeriod)25}26func main() {27 executor := NewExecutor()28 gracePeriod := executor.GetGracefulRampDown()29 fmt.Println(gracePeriod)30}31func main() {32 executor := NewExecutor()33 gracePeriod := executor.GetGracefulRampDown()34 fmt.Println(gracePeriod)35}36func main() {37 executor := NewExecutor()38 gracePeriod := executor.GetGracefulRampDown()39 fmt.Println(gracePeriod)40}41func main() {42 executor := NewExecutor()43 gracePeriod := executor.GetGracefulRampDown()44 fmt.Println(gracePeriod)45}46func main() {47 executor := NewExecutor()48 gracePeriod := executor.GetGracefulRampDown()49 fmt.Println(gracePeriod)50}
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 executorInfo := &mesos.ExecutorInfo{}4 executorDriver := &mesos.ExecutorDriver{}5 executorConfig := &mesos.ExecutorConfig{}6 executorConfig.GetGracefulRampDown(executorInfo, executorDriver)7}
GetGracefulRampDown
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 executor := runtime.GOMAXPROCS(2)5 fmt.Println("Number of CPUs: ", executor)6}7import (8func main() {9 fmt.Println("Hello, playground")10 executor := runtime.GOMAXPROCS(1)11 fmt.Println("Number of CPUs: ", executor)12}13import (14func main() {15 fmt.Println("Hello, playground")16 executor := runtime.NumCPU()17 fmt.Println("Number of CPUs: ", executor)18}19import (20func main() {21 fmt.Println("Hello, playground")22 executor := runtime.NumCPU()23 fmt.Println("Number of CPUs: ", executor)24}25import (26func main() {27 fmt.Println("Hello, playground")28 executor := runtime.NumCgoCall()29 fmt.Println("Number of CPUs: ", executor)30}31import (32func main() {33 fmt.Println("Hello, playground")34 executor := runtime.NumGoroutine()35 fmt.Println("Number of CPUs: ", executor)36}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!