How to use runIter method of executor Package

Best K6 code snippet using executor.runIter

vu_handle_test.go

Source:vu_handle_test.go Github

copy

Full Screen

...60 // do something61 }62 var interruptedIter int6463 var fullIterations int6464 runIter := func(ctx context.Context, vu lib.ActiveVU) bool {65 _ = vu.RunOnce()66 select {67 case <-ctx.Done():68 // Don't log errors or emit iterations metrics from cancelled iterations69 atomic.AddInt64(&interruptedIter, 1)70 return false71 default:72 atomic.AddInt64(&fullIterations, 1)73 return true74 }75 }76 vuHandle := newStoppedVUHandle(ctx, getVU, returnVU, mockNextIterations, &BaseConfig{}, logEntry)77 go vuHandle.runLoopsIfPossible(runIter)78 var wg sync.WaitGroup79 wg.Add(3)80 go func() {81 defer wg.Done()82 for i := 0; i < 10000; i++ {83 err := vuHandle.start()84 require.NoError(t, err)85 }86 }()87 go func() {88 defer wg.Done()89 for i := 0; i < 1000; i++ {90 vuHandle.gracefulStop()91 time.Sleep(1 * time.Nanosecond)92 }93 }()94 go func() {95 defer wg.Done()96 for i := 0; i < 100; i++ {97 vuHandle.hardStop()98 time.Sleep(10 * time.Nanosecond)99 }100 }()101 wg.Wait()102 vuHandle.hardStop() // STOP it103 time.Sleep(time.Millisecond * 50)104 interruptedBefore := atomic.LoadInt64(&interruptedIter)105 fullBefore := atomic.LoadInt64(&fullIterations)106 _ = vuHandle.start()107 time.Sleep(time.Millisecond * 50) // just to be sure an iteration will squeeze in108 cancel()109 time.Sleep(time.Millisecond * 50)110 interruptedAfter := atomic.LoadInt64(&interruptedIter)111 fullAfter := atomic.LoadInt64(&fullIterations)112 assert.True(t, interruptedBefore >= interruptedAfter-1,113 "too big of a difference %d >= %d - 1", interruptedBefore, interruptedAfter)114 assert.True(t, fullBefore+1 <= fullAfter,115 "too small of a difference %d + 1 <= %d", fullBefore, fullAfter)116 require.Equal(t, atomic.LoadInt64(&getVUCount), atomic.LoadInt64(&returnVUCount))117}118// this test is mostly interesting when -race is enabled119func TestVUHandleStartStopRace(t *testing.T) {120 t.Parallel()121 ctx, cancel := context.WithCancel(context.Background())122 defer cancel()123 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}124 testLog := logrus.New()125 testLog.AddHook(logHook)126 testLog.SetOutput(testutils.NewTestOutput(t))127 // testLog.Level = logrus.DebugLevel128 logEntry := logrus.NewEntry(testLog)129 runner := &minirunner.MiniRunner{}130 runner.Fn = func(ctx context.Context, out chan<- stats.SampleContainer) error {131 return nil132 }133 var vuID uint64134 testIterations := 10000135 returned := make(chan struct{})136 getVU := func() (lib.InitializedVU, error) {137 returned = make(chan struct{})138 return runner.NewVU(atomic.AddUint64(&vuID, 1), 0, nil)139 }140 returnVU := func(v lib.InitializedVU) {141 require.Equal(t, atomic.LoadUint64(&vuID), v.(*minirunner.VU).ID)142 close(returned)143 }144 var interruptedIter int64145 var fullIterations int64146 runIter := func(ctx context.Context, vu lib.ActiveVU) bool {147 _ = vu.RunOnce()148 select {149 case <-ctx.Done():150 // Don't log errors or emit iterations metrics from cancelled iterations151 atomic.AddInt64(&interruptedIter, 1)152 return false153 default:154 atomic.AddInt64(&fullIterations, 1)155 return true156 }157 }158 vuHandle := newStoppedVUHandle(ctx, getVU, returnVU, mockNextIterations, &BaseConfig{}, logEntry)159 go vuHandle.runLoopsIfPossible(runIter)160 for i := 0; i < testIterations; i++ {161 err := vuHandle.start()162 vuHandle.gracefulStop()163 require.NoError(t, err)164 select {165 case <-returned:166 case <-time.After(100 * time.Millisecond):167 go panic("returning took too long")168 time.Sleep(time.Second)169 }170 }171 vuHandle.hardStop() // STOP it172 time.Sleep(time.Millisecond * 5)173 interruptedBefore := atomic.LoadInt64(&interruptedIter)174 fullBefore := atomic.LoadInt64(&fullIterations)175 _ = vuHandle.start()176 time.Sleep(time.Millisecond * 50) // just to be sure an iteration will squeeze in177 cancel()178 time.Sleep(time.Millisecond * 5)179 interruptedAfter := atomic.LoadInt64(&interruptedIter)180 fullAfter := atomic.LoadInt64(&fullIterations)181 assert.True(t, interruptedBefore >= interruptedAfter-1,182 "too big of a difference %d >= %d - 1", interruptedBefore, interruptedAfter)183 assert.True(t, fullBefore+1 <= fullAfter,184 "too small of a difference %d + 1 <= %d", fullBefore, fullAfter)185}186type handleVUTest struct {187 runner *minirunner.MiniRunner188 getVUCount uint32189 returnVUCount uint32190 interruptedIter int64191 fullIterations int64192}193func (h *handleVUTest) getVU() (lib.InitializedVU, error) {194 return h.runner.NewVU(uint64(atomic.AddUint32(&h.getVUCount, 1)), 0, nil)195}196func (h *handleVUTest) returnVU(_ lib.InitializedVU) {197 atomic.AddUint32(&h.returnVUCount, 1)198}199func (h *handleVUTest) runIter(ctx context.Context, _ lib.ActiveVU) bool {200 select {201 case <-time.After(time.Second):202 case <-ctx.Done():203 }204 select {205 case <-ctx.Done():206 // Don't log errors or emit iterations metrics from cancelled iterations207 atomic.AddInt64(&h.interruptedIter, 1)208 return false209 default:210 atomic.AddInt64(&h.fullIterations, 1)211 return true212 }213}214func TestVUHandleSimple(t *testing.T) {215 t.Parallel()216 t.Run("start before gracefulStop finishes", func(t *testing.T) {217 t.Parallel()218 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}219 testLog := logrus.New()220 testLog.AddHook(logHook)221 testLog.SetOutput(testutils.NewTestOutput(t))222 // testLog.Level = logrus.DebugLevel223 logEntry := logrus.NewEntry(testLog)224 test := &handleVUTest{runner: &minirunner.MiniRunner{}}225 ctx, cancel := context.WithCancel(context.Background())226 defer cancel()227 vuHandle := newStoppedVUHandle(ctx, test.getVU, test.returnVU, mockNextIterations, &BaseConfig{}, logEntry)228 var wg sync.WaitGroup229 wg.Add(1)230 go func() {231 defer wg.Done()232 vuHandle.runLoopsIfPossible(test.runIter)233 }()234 err := vuHandle.start()235 require.NoError(t, err)236 time.Sleep(time.Millisecond * 50)237 vuHandle.gracefulStop()238 // time.Sleep(time.Millisecond * 5) // No sleep as we want to always not return the VU239 err = vuHandle.start()240 require.NoError(t, err)241 time.Sleep(time.Millisecond * 1500)242 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))243 assert.EqualValues(t, 0, atomic.LoadUint32(&test.returnVUCount))244 assert.EqualValues(t, 0, atomic.LoadInt64(&test.interruptedIter))245 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))246 cancel()247 wg.Wait()248 time.Sleep(time.Millisecond * 5)249 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))250 assert.EqualValues(t, 1, atomic.LoadUint32(&test.returnVUCount))251 assert.EqualValues(t, 1, atomic.LoadInt64(&test.interruptedIter))252 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))253 })254 t.Run("start after gracefulStop finishes", func(t *testing.T) {255 t.Parallel()256 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}257 testLog := logrus.New()258 testLog.AddHook(logHook)259 testLog.SetOutput(testutils.NewTestOutput(t))260 // testLog.Level = logrus.DebugLevel261 logEntry := logrus.NewEntry(testLog)262 test := &handleVUTest{runner: &minirunner.MiniRunner{}}263 ctx, cancel := context.WithCancel(context.Background())264 defer cancel()265 vuHandle := newStoppedVUHandle(ctx, test.getVU, test.returnVU, mockNextIterations, &BaseConfig{}, logEntry)266 var wg sync.WaitGroup267 wg.Add(1)268 go func() {269 defer wg.Done()270 vuHandle.runLoopsIfPossible(test.runIter)271 }()272 err := vuHandle.start()273 require.NoError(t, err)274 time.Sleep(time.Millisecond * 50)275 vuHandle.gracefulStop()276 time.Sleep(time.Millisecond * 1500)277 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))278 assert.EqualValues(t, 1, atomic.LoadUint32(&test.returnVUCount))279 assert.EqualValues(t, 0, atomic.LoadInt64(&test.interruptedIter))280 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))281 err = vuHandle.start()282 require.NoError(t, err)283 time.Sleep(time.Millisecond * 1500)284 cancel()285 wg.Wait()286 time.Sleep(time.Millisecond * 50)287 assert.EqualValues(t, 2, atomic.LoadUint32(&test.getVUCount))288 assert.EqualValues(t, 2, atomic.LoadUint32(&test.returnVUCount))289 assert.EqualValues(t, 1, atomic.LoadInt64(&test.interruptedIter))290 assert.EqualValues(t, 2, atomic.LoadInt64(&test.fullIterations))291 })292 t.Run("start after hardStop", func(t *testing.T) {293 t.Parallel()294 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}295 testLog := logrus.New()296 testLog.AddHook(logHook)297 testLog.SetOutput(testutils.NewTestOutput(t))298 // testLog.Level = logrus.DebugLevel299 logEntry := logrus.NewEntry(testLog)300 test := &handleVUTest{runner: &minirunner.MiniRunner{}}301 ctx, cancel := context.WithCancel(context.Background())302 defer cancel()303 vuHandle := newStoppedVUHandle(ctx, test.getVU, test.returnVU, mockNextIterations, &BaseConfig{}, logEntry)304 var wg sync.WaitGroup305 wg.Add(1)306 go func() {307 defer wg.Done()308 vuHandle.runLoopsIfPossible(test.runIter)309 }()310 err := vuHandle.start()311 require.NoError(t, err)312 time.Sleep(time.Millisecond * 5)313 vuHandle.hardStop()314 time.Sleep(time.Millisecond * 15)315 assert.EqualValues(t, 1, atomic.LoadUint32(&test.getVUCount))316 assert.EqualValues(t, 1, atomic.LoadUint32(&test.returnVUCount))317 assert.EqualValues(t, 1, atomic.LoadInt64(&test.interruptedIter))318 assert.EqualValues(t, 0, atomic.LoadInt64(&test.fullIterations))319 err = vuHandle.start()320 require.NoError(t, err)321 time.Sleep(time.Millisecond * 1500)322 cancel()323 wg.Wait()324 time.Sleep(time.Millisecond * 5)325 assert.EqualValues(t, 2, atomic.LoadUint32(&test.getVUCount))326 assert.EqualValues(t, 2, atomic.LoadUint32(&test.returnVUCount))327 assert.EqualValues(t, 2, atomic.LoadInt64(&test.interruptedIter))328 assert.EqualValues(t, 1, atomic.LoadInt64(&test.fullIterations))329 })330}331func BenchmarkVUHandleIterations(b *testing.B) {332 logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.DebugLevel}}333 testLog := logrus.New()334 testLog.AddHook(logHook)335 // testLog.Level = logrus.DebugLevel336 logEntry := logrus.NewEntry(testLog)337 var (338 getVUCount uint32339 returnVUCount uint32340 interruptedIter int64341 fullIterations int64342 )343 reset := func() {344 getVUCount = 0345 returnVUCount = 0346 interruptedIter = 0347 fullIterations = 0348 }349 runner := &minirunner.MiniRunner{}350 runner.Fn = func(ctx context.Context, out chan<- stats.SampleContainer) error {351 return nil352 }353 getVU := func() (lib.InitializedVU, error) {354 return runner.NewVU(uint64(atomic.AddUint32(&getVUCount, 1)), 0, nil)355 }356 returnVU := func(_ lib.InitializedVU) {357 atomic.AddUint32(&returnVUCount, 1)358 }359 runIter := func(ctx context.Context, _ lib.ActiveVU) bool {360 // Do nothing361 select {362 case <-ctx.Done():363 // Don't log errors or emit iterations metrics from cancelled iterations364 atomic.AddInt64(&interruptedIter, 1)365 return false366 default:367 atomic.AddInt64(&fullIterations, 1)368 return true369 }370 }371 reset()372 ctx, cancel := context.WithCancel(context.Background())373 defer cancel()374 vuHandle := newStoppedVUHandle(ctx, getVU, returnVU, mockNextIterations, &BaseConfig{}, logEntry)375 var wg sync.WaitGroup376 wg.Add(1)377 go func() {378 defer wg.Done()379 vuHandle.runLoopsIfPossible(runIter)380 }()381 start := time.Now()382 b.ResetTimer()383 err := vuHandle.start()384 require.NoError(b, err)385 time.Sleep(time.Second)386 cancel()387 wg.Wait()388 b.StopTimer()389 took := time.Since(start)390 b.ReportMetric(float64(atomic.LoadInt64(&fullIterations))/float64(took), "iterations/ns")391}...

Full Screen

Full Screen

runIter

Using AI Code Generation

copy

Full Screen

1for i := 0; i < 10; i++ {2 err := exec.runIter()3 if err != nil {4 log.Fatal(err)5 }6}7for i := 0; i < 20; i++ {8 err := exec.runIter()9 if err != nil {10 log.Fatal(err)11 }12}13for i := 0; i < 30; i++ {14 err := exec.runIter()15 if err != nil {16 log.Fatal(err)17 }18}19for i := 0; i < 40; i++ {20 err := exec.runIter()21 if err != nil {22 log.Fatal(err)23 }24}25for i := 0; i < 50; i++ {26 err := exec.runIter()27 if err != nil {28 log.Fatal(err)29 }30}31for i := 0; i < 60; i++ {32 err := exec.runIter()33 if err != nil {34 log.Fatal(err)35 }36}37for i := 0; i < 70; i++ {38 err := exec.runIter()39 if err != nil {40 log.Fatal(err)41 }42}43for i := 0; i < 80; i++ {44 err := exec.runIter()45 if err != nil {46 log.Fatal(err)47 }48}49for i := 0; i < 90; i++ {50 err := exec.runIter()51 if err != nil {52 log.Fatal(err)53 }54}55for i := 0; i < 100; i++ {56 err := exec.runIter()

Full Screen

Full Screen

runIter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 exec := executor.Executor{}4 exec.RunIter()5 fmt.Println("Done")6}

Full Screen

Full Screen

runIter

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "reflect"3import "reflect"4import "reflect"5type Executor interface {6 RunIter() (int, error)7}8type executor struct {9}10func (e *executor) RunIter() (int, error) {11 fmt.Println("Running ", e.name)12}13func main() {14 e := &executor{"test"}15 fmt.Println("Type of e is ", reflect.TypeOf(e))16 fmt.Println("Value of e is ", reflect.ValueOf(e))17 fmt.Println("Type of e is ", reflect.TypeOf(e).Kind())18 fmt.Println("Value of e is ", reflect.ValueOf(e).Kind())19 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem())20 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem())21 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().Kind())22 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().Kind())23 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().Name())24 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().Name())25 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().NumMethod())26 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().NumMethod())27 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().NumField())28 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().NumField())29 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().Field(0))30 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().Field(0))31 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().Field(0).Name)32 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().Field(0).Name)33 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().Field(0).Type)34 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().Field(0).Type)35 fmt.Println("Type of e is ", reflect.TypeOf(e).Elem().Field(0).Type.Kind())36 fmt.Println("Value of e is ", reflect.ValueOf(e).Elem().Field(0).Type.Kind())37 fmt.Println("Type of e is ",

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