Best K6 code snippet using executor.getRawExecutionSteps
ramping_vus_test.go
Source:ramping_vus_test.go
...346 {TimeOffset: 17 * time.Second, PlannedVUs: 3},347 {TimeOffset: 18 * time.Second, PlannedVUs: 4},348 {TimeOffset: 20 * time.Second, PlannedVUs: 1},349 }350 rawStepsNoZeroEnd := conf.getRawExecutionSteps(et, false)351 assert.Equal(t, expRawStepsNoZeroEnd, rawStepsNoZeroEnd)352 endOffset, isFinal := lib.GetEndOffset(rawStepsNoZeroEnd)353 assert.Equal(t, 20*time.Second, endOffset)354 assert.Equal(t, false, isFinal)355 rawStepsZeroEnd := conf.getRawExecutionSteps(et, true)356 assert.Equal(t,357 append(expRawStepsNoZeroEnd, lib.ExecutionStep{TimeOffset: 23 * time.Second, PlannedVUs: 0}),358 rawStepsZeroEnd,359 )360 endOffset, isFinal = lib.GetEndOffset(rawStepsZeroEnd)361 assert.Equal(t, 23*time.Second, endOffset)362 assert.Equal(t, true, isFinal)363 // GracefulStop and GracefulRampDown equal to the default 30 sec364 assert.Equal(t, []lib.ExecutionStep{365 {TimeOffset: 0 * time.Second, PlannedVUs: 4},366 {TimeOffset: 1 * time.Second, PlannedVUs: 5},367 {TimeOffset: 2 * time.Second, PlannedVUs: 6},368 {TimeOffset: 33 * time.Second, PlannedVUs: 5},369 {TimeOffset: 42 * time.Second, PlannedVUs: 4},370 {TimeOffset: 50 * time.Second, PlannedVUs: 1},371 {TimeOffset: 53 * time.Second, PlannedVUs: 0},372 }, conf.GetExecutionRequirements(et))373 // Try a longer GracefulStop than the GracefulRampDown374 conf.GracefulStop = types.NullDurationFrom(80 * time.Second)375 assert.Equal(t, []lib.ExecutionStep{376 {TimeOffset: 0 * time.Second, PlannedVUs: 4},377 {TimeOffset: 1 * time.Second, PlannedVUs: 5},378 {TimeOffset: 2 * time.Second, PlannedVUs: 6},379 {TimeOffset: 33 * time.Second, PlannedVUs: 5},380 {TimeOffset: 42 * time.Second, PlannedVUs: 4},381 {TimeOffset: 50 * time.Second, PlannedVUs: 1},382 {TimeOffset: 103 * time.Second, PlannedVUs: 0},383 }, conf.GetExecutionRequirements(et))384 // Try a much shorter GracefulStop than the GracefulRampDown385 conf.GracefulStop = types.NullDurationFrom(3 * time.Second)386 assert.Equal(t, []lib.ExecutionStep{387 {TimeOffset: 0 * time.Second, PlannedVUs: 4},388 {TimeOffset: 1 * time.Second, PlannedVUs: 5},389 {TimeOffset: 2 * time.Second, PlannedVUs: 6},390 {TimeOffset: 26 * time.Second, PlannedVUs: 0},391 }, conf.GetExecutionRequirements(et))392 // Try a zero GracefulStop393 conf.GracefulStop = types.NullDurationFrom(0 * time.Second)394 assert.Equal(t, []lib.ExecutionStep{395 {TimeOffset: 0 * time.Second, PlannedVUs: 4},396 {TimeOffset: 1 * time.Second, PlannedVUs: 5},397 {TimeOffset: 2 * time.Second, PlannedVUs: 6},398 {TimeOffset: 23 * time.Second, PlannedVUs: 0},399 }, conf.GetExecutionRequirements(et))400 // Try a zero GracefulStop and GracefulRampDown, i.e. raw steps with 0 end cap401 conf.GracefulRampDown = types.NullDurationFrom(0 * time.Second)402 assert.Equal(t, rawStepsZeroEnd, conf.GetExecutionRequirements(et))403}404func TestRampingVUsConfigExecutionPlanExampleOneThird(t *testing.T) {405 t.Parallel()406 et, err := lib.NewExecutionTuple(newExecutionSegmentFromString("0:1/3"), nil)407 require.NoError(t, err)408 conf := NewRampingVUsConfig("test")409 conf.StartVUs = null.IntFrom(4)410 conf.Stages = []Stage{411 {Target: null.IntFrom(6), Duration: types.NullDurationFrom(2 * time.Second)},412 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(5 * time.Second)},413 {Target: null.IntFrom(5), Duration: types.NullDurationFrom(4 * time.Second)},414 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(4 * time.Second)},415 {Target: null.IntFrom(4), Duration: types.NullDurationFrom(3 * time.Second)},416 {Target: null.IntFrom(4), Duration: types.NullDurationFrom(2 * time.Second)},417 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(0 * time.Second)},418 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(3 * time.Second)},419 }420 expRawStepsNoZeroEnd := []lib.ExecutionStep{421 {TimeOffset: 0 * time.Second, PlannedVUs: 1},422 {TimeOffset: 1 * time.Second, PlannedVUs: 2},423 {TimeOffset: 4 * time.Second, PlannedVUs: 1},424 {TimeOffset: 7 * time.Second, PlannedVUs: 0},425 {TimeOffset: 8 * time.Second, PlannedVUs: 1},426 {TimeOffset: 11 * time.Second, PlannedVUs: 2},427 {TimeOffset: 12 * time.Second, PlannedVUs: 1},428 {TimeOffset: 15 * time.Second, PlannedVUs: 0},429 {TimeOffset: 16 * time.Second, PlannedVUs: 1},430 {TimeOffset: 20 * time.Second, PlannedVUs: 0},431 }432 rawStepsNoZeroEnd := conf.getRawExecutionSteps(et, false)433 assert.Equal(t, expRawStepsNoZeroEnd, rawStepsNoZeroEnd)434 endOffset, isFinal := lib.GetEndOffset(rawStepsNoZeroEnd)435 assert.Equal(t, 20*time.Second, endOffset)436 assert.Equal(t, true, isFinal)437 rawStepsZeroEnd := conf.getRawExecutionSteps(et, true)438 assert.Equal(t, expRawStepsNoZeroEnd, rawStepsZeroEnd)439 endOffset, isFinal = lib.GetEndOffset(rawStepsZeroEnd)440 assert.Equal(t, 20*time.Second, endOffset)441 assert.Equal(t, true, isFinal)442 // GracefulStop and GracefulRampDown equal to the default 30 sec443 assert.Equal(t, []lib.ExecutionStep{444 {TimeOffset: 0 * time.Second, PlannedVUs: 1},445 {TimeOffset: 1 * time.Second, PlannedVUs: 2},446 {TimeOffset: 42 * time.Second, PlannedVUs: 1},447 {TimeOffset: 50 * time.Second, PlannedVUs: 0},448 }, conf.GetExecutionRequirements(et))449 // Try a longer GracefulStop than the GracefulRampDown450 conf.GracefulStop = types.NullDurationFrom(80 * time.Second)451 assert.Equal(t, []lib.ExecutionStep{452 {TimeOffset: 0 * time.Second, PlannedVUs: 1},453 {TimeOffset: 1 * time.Second, PlannedVUs: 2},454 {TimeOffset: 42 * time.Second, PlannedVUs: 1},455 {TimeOffset: 50 * time.Second, PlannedVUs: 0},456 }, conf.GetExecutionRequirements(et))457 // Try a much shorter GracefulStop than the GracefulRampDown458 conf.GracefulStop = types.NullDurationFrom(3 * time.Second)459 assert.Equal(t, []lib.ExecutionStep{460 {TimeOffset: 0 * time.Second, PlannedVUs: 1},461 {TimeOffset: 1 * time.Second, PlannedVUs: 2},462 {TimeOffset: 26 * time.Second, PlannedVUs: 0},463 }, conf.GetExecutionRequirements(et))464 // Try a zero GracefulStop465 conf.GracefulStop = types.NullDurationFrom(0 * time.Second)466 assert.Equal(t, []lib.ExecutionStep{467 {TimeOffset: 0 * time.Second, PlannedVUs: 1},468 {TimeOffset: 1 * time.Second, PlannedVUs: 2},469 {TimeOffset: 23 * time.Second, PlannedVUs: 0},470 }, conf.GetExecutionRequirements(et))471 // Try a zero GracefulStop and GracefulRampDown, i.e. raw steps with 0 end cap472 conf.GracefulRampDown = types.NullDurationFrom(0 * time.Second)473 assert.Equal(t, rawStepsZeroEnd, conf.GetExecutionRequirements(et))474}475func TestRampingVUsExecutionTupleTests(t *testing.T) {476 t.Parallel()477 conf := NewRampingVUsConfig("test")478 conf.StartVUs = null.IntFrom(4)479 conf.Stages = []Stage{480 {Target: null.IntFrom(6), Duration: types.NullDurationFrom(2 * time.Second)},481 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(5 * time.Second)},482 {Target: null.IntFrom(5), Duration: types.NullDurationFrom(4 * time.Second)},483 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(4 * time.Second)},484 {Target: null.IntFrom(4), Duration: types.NullDurationFrom(3 * time.Second)},485 {Target: null.IntFrom(4), Duration: types.NullDurationFrom(2 * time.Second)},486 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(0 * time.Second)},487 {Target: null.IntFrom(1), Duration: types.NullDurationFrom(3 * time.Second)},488 {Target: null.IntFrom(5), Duration: types.NullDurationFrom(0 * time.Second)},489 {Target: null.IntFrom(5), Duration: types.NullDurationFrom(3 * time.Second)},490 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(0 * time.Second)},491 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},492 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},493 {Target: null.IntFrom(4), Duration: types.NullDurationFrom(4 * time.Second)},494 }495 /*496 Graph of the above:497 ^498 8 |499 7 |500 6 | +501 5 |/ \ + +--+502 4 + \ / \ +-+ | | *503 3 | \ / \ / | | | /504 2 | \ / \ / | | | + /505 1 | + + +--+ |/ \ /506 0 +-------------------------+---+------------------------------>507 01234567890123456789012345678901234567890508 */509 testCases := []struct {510 expectedSteps []lib.ExecutionStep511 et *lib.ExecutionTuple512 }{513 {514 et: mustNewExecutionTuple(nil, nil),515 expectedSteps: []lib.ExecutionStep{516 {TimeOffset: 0 * time.Second, PlannedVUs: 4},517 {TimeOffset: 1 * time.Second, PlannedVUs: 5},518 {TimeOffset: 2 * time.Second, PlannedVUs: 6},519 {TimeOffset: 3 * time.Second, PlannedVUs: 5},520 {TimeOffset: 4 * time.Second, PlannedVUs: 4},521 {TimeOffset: 5 * time.Second, PlannedVUs: 3},522 {TimeOffset: 6 * time.Second, PlannedVUs: 2},523 {TimeOffset: 7 * time.Second, PlannedVUs: 1},524 {TimeOffset: 8 * time.Second, PlannedVUs: 2},525 {TimeOffset: 9 * time.Second, PlannedVUs: 3},526 {TimeOffset: 10 * time.Second, PlannedVUs: 4},527 {TimeOffset: 11 * time.Second, PlannedVUs: 5},528 {TimeOffset: 12 * time.Second, PlannedVUs: 4},529 {TimeOffset: 13 * time.Second, PlannedVUs: 3},530 {TimeOffset: 14 * time.Second, PlannedVUs: 2},531 {TimeOffset: 15 * time.Second, PlannedVUs: 1},532 {TimeOffset: 16 * time.Second, PlannedVUs: 2},533 {TimeOffset: 17 * time.Second, PlannedVUs: 3},534 {TimeOffset: 18 * time.Second, PlannedVUs: 4},535 {TimeOffset: 20 * time.Second, PlannedVUs: 1},536 {TimeOffset: 23 * time.Second, PlannedVUs: 5},537 {TimeOffset: 26 * time.Second, PlannedVUs: 0},538 {TimeOffset: 27 * time.Second, PlannedVUs: 1},539 {TimeOffset: 28 * time.Second, PlannedVUs: 2},540 {TimeOffset: 29 * time.Second, PlannedVUs: 1},541 {TimeOffset: 30 * time.Second, PlannedVUs: 0},542 {TimeOffset: 31 * time.Second, PlannedVUs: 1},543 {TimeOffset: 32 * time.Second, PlannedVUs: 2},544 {TimeOffset: 33 * time.Second, PlannedVUs: 3},545 {TimeOffset: 34 * time.Second, PlannedVUs: 4},546 },547 },548 {549 et: mustNewExecutionTuple(newExecutionSegmentFromString("0:1/3"), nil),550 expectedSteps: []lib.ExecutionStep{551 {TimeOffset: 0 * time.Second, PlannedVUs: 1},552 {TimeOffset: 1 * time.Second, PlannedVUs: 2},553 {TimeOffset: 4 * time.Second, PlannedVUs: 1},554 {TimeOffset: 7 * time.Second, PlannedVUs: 0},555 {TimeOffset: 8 * time.Second, PlannedVUs: 1},556 {TimeOffset: 11 * time.Second, PlannedVUs: 2},557 {TimeOffset: 12 * time.Second, PlannedVUs: 1},558 {TimeOffset: 15 * time.Second, PlannedVUs: 0},559 {TimeOffset: 16 * time.Second, PlannedVUs: 1},560 {TimeOffset: 20 * time.Second, PlannedVUs: 0},561 {TimeOffset: 23 * time.Second, PlannedVUs: 2},562 {TimeOffset: 26 * time.Second, PlannedVUs: 0},563 {TimeOffset: 28 * time.Second, PlannedVUs: 1},564 {TimeOffset: 29 * time.Second, PlannedVUs: 0},565 {TimeOffset: 32 * time.Second, PlannedVUs: 1},566 },567 },568 {569 et: mustNewExecutionTuple(newExecutionSegmentFromString("0:1/3"), newExecutionSegmentSequenceFromString("0,1/3,1")),570 expectedSteps: []lib.ExecutionStep{571 {TimeOffset: 0 * time.Second, PlannedVUs: 1},572 {TimeOffset: 1 * time.Second, PlannedVUs: 2},573 {TimeOffset: 4 * time.Second, PlannedVUs: 1},574 {TimeOffset: 7 * time.Second, PlannedVUs: 0},575 {TimeOffset: 8 * time.Second, PlannedVUs: 1},576 {TimeOffset: 11 * time.Second, PlannedVUs: 2},577 {TimeOffset: 12 * time.Second, PlannedVUs: 1},578 {TimeOffset: 15 * time.Second, PlannedVUs: 0},579 {TimeOffset: 16 * time.Second, PlannedVUs: 1},580 {TimeOffset: 20 * time.Second, PlannedVUs: 0},581 {TimeOffset: 23 * time.Second, PlannedVUs: 2},582 {TimeOffset: 26 * time.Second, PlannedVUs: 0},583 {TimeOffset: 28 * time.Second, PlannedVUs: 1},584 {TimeOffset: 29 * time.Second, PlannedVUs: 0},585 {TimeOffset: 32 * time.Second, PlannedVUs: 1},586 },587 },588 {589 et: mustNewExecutionTuple(newExecutionSegmentFromString("1/3:2/3"), nil),590 expectedSteps: []lib.ExecutionStep{591 {TimeOffset: 0 * time.Second, PlannedVUs: 1},592 {TimeOffset: 1 * time.Second, PlannedVUs: 2},593 {TimeOffset: 4 * time.Second, PlannedVUs: 1},594 {TimeOffset: 7 * time.Second, PlannedVUs: 0},595 {TimeOffset: 8 * time.Second, PlannedVUs: 1},596 {TimeOffset: 11 * time.Second, PlannedVUs: 2},597 {TimeOffset: 12 * time.Second, PlannedVUs: 1},598 {TimeOffset: 15 * time.Second, PlannedVUs: 0},599 {TimeOffset: 16 * time.Second, PlannedVUs: 1},600 {TimeOffset: 20 * time.Second, PlannedVUs: 0},601 {TimeOffset: 23 * time.Second, PlannedVUs: 2},602 {TimeOffset: 26 * time.Second, PlannedVUs: 0},603 {TimeOffset: 28 * time.Second, PlannedVUs: 1},604 {TimeOffset: 29 * time.Second, PlannedVUs: 0},605 {TimeOffset: 32 * time.Second, PlannedVUs: 1},606 },607 },608 {609 et: mustNewExecutionTuple(newExecutionSegmentFromString("2/3:1"), nil),610 expectedSteps: []lib.ExecutionStep{611 {TimeOffset: 0 * time.Second, PlannedVUs: 1},612 {TimeOffset: 1 * time.Second, PlannedVUs: 2},613 {TimeOffset: 4 * time.Second, PlannedVUs: 1},614 {TimeOffset: 7 * time.Second, PlannedVUs: 0},615 {TimeOffset: 8 * time.Second, PlannedVUs: 1},616 {TimeOffset: 11 * time.Second, PlannedVUs: 2},617 {TimeOffset: 12 * time.Second, PlannedVUs: 1},618 {TimeOffset: 15 * time.Second, PlannedVUs: 0},619 {TimeOffset: 16 * time.Second, PlannedVUs: 1},620 {TimeOffset: 20 * time.Second, PlannedVUs: 0},621 {TimeOffset: 23 * time.Second, PlannedVUs: 2},622 {TimeOffset: 26 * time.Second, PlannedVUs: 0},623 {TimeOffset: 28 * time.Second, PlannedVUs: 1},624 {TimeOffset: 29 * time.Second, PlannedVUs: 0},625 {TimeOffset: 32 * time.Second, PlannedVUs: 1},626 },627 },628 {629 et: mustNewExecutionTuple(newExecutionSegmentFromString("0:1/3"), newExecutionSegmentSequenceFromString("0,1/3,2/3,1")),630 expectedSteps: []lib.ExecutionStep{631 {TimeOffset: 0 * time.Second, PlannedVUs: 2},632 {TimeOffset: 5 * time.Second, PlannedVUs: 1},633 {TimeOffset: 10 * time.Second, PlannedVUs: 2},634 {TimeOffset: 13 * time.Second, PlannedVUs: 1},635 {TimeOffset: 18 * time.Second, PlannedVUs: 2},636 {TimeOffset: 20 * time.Second, PlannedVUs: 1},637 {TimeOffset: 23 * time.Second, PlannedVUs: 2},638 {TimeOffset: 26 * time.Second, PlannedVUs: 0},639 {TimeOffset: 27 * time.Second, PlannedVUs: 1},640 {TimeOffset: 30 * time.Second, PlannedVUs: 0},641 {TimeOffset: 31 * time.Second, PlannedVUs: 1},642 {TimeOffset: 34 * time.Second, PlannedVUs: 2},643 },644 },645 {646 et: mustNewExecutionTuple(newExecutionSegmentFromString("1/3:2/3"), newExecutionSegmentSequenceFromString("0,1/3,2/3,1")),647 expectedSteps: []lib.ExecutionStep{648 {TimeOffset: 0 * time.Second, PlannedVUs: 1},649 {TimeOffset: 1 * time.Second, PlannedVUs: 2},650 {TimeOffset: 4 * time.Second, PlannedVUs: 1},651 {TimeOffset: 7 * time.Second, PlannedVUs: 0},652 {TimeOffset: 8 * time.Second, PlannedVUs: 1},653 {TimeOffset: 11 * time.Second, PlannedVUs: 2},654 {TimeOffset: 12 * time.Second, PlannedVUs: 1},655 {TimeOffset: 15 * time.Second, PlannedVUs: 0},656 {TimeOffset: 16 * time.Second, PlannedVUs: 1},657 {TimeOffset: 20 * time.Second, PlannedVUs: 0},658 {TimeOffset: 23 * time.Second, PlannedVUs: 2},659 {TimeOffset: 26 * time.Second, PlannedVUs: 0},660 {TimeOffset: 28 * time.Second, PlannedVUs: 1},661 {TimeOffset: 29 * time.Second, PlannedVUs: 0},662 {TimeOffset: 32 * time.Second, PlannedVUs: 1},663 },664 },665 {666 et: mustNewExecutionTuple(newExecutionSegmentFromString("2/3:1"), newExecutionSegmentSequenceFromString("0,1/3,2/3,1")),667 expectedSteps: []lib.ExecutionStep{668 {TimeOffset: 0 * time.Second, PlannedVUs: 1},669 {TimeOffset: 2 * time.Second, PlannedVUs: 2},670 {TimeOffset: 3 * time.Second, PlannedVUs: 1},671 {TimeOffset: 6 * time.Second, PlannedVUs: 0},672 {TimeOffset: 9 * time.Second, PlannedVUs: 1},673 {TimeOffset: 14 * time.Second, PlannedVUs: 0},674 {TimeOffset: 17 * time.Second, PlannedVUs: 1},675 {TimeOffset: 20 * time.Second, PlannedVUs: 0},676 {TimeOffset: 23 * time.Second, PlannedVUs: 1},677 {TimeOffset: 26 * time.Second, PlannedVUs: 0},678 {TimeOffset: 33 * time.Second, PlannedVUs: 1},679 },680 },681 }682 for _, testCase := range testCases {683 et := testCase.et684 expectedSteps := testCase.expectedSteps685 t.Run(et.String(), func(t *testing.T) {686 t.Parallel()687 rawStepsNoZeroEnd := conf.getRawExecutionSteps(et, false)688 assert.Equal(t, expectedSteps, rawStepsNoZeroEnd)689 })690 }691}692func TestRampingVUsGetRawExecutionStepsCornerCases(t *testing.T) {693 t.Parallel()694 testCases := []struct {695 name string696 expectedSteps []lib.ExecutionStep697 et *lib.ExecutionTuple698 stages []Stage699 start int64700 }{701 {702 name: "going up then down straight away",703 expectedSteps: []lib.ExecutionStep{704 {TimeOffset: 0 * time.Second, PlannedVUs: 2},705 {TimeOffset: 0 * time.Second, PlannedVUs: 5},706 {TimeOffset: 1 * time.Second, PlannedVUs: 4},707 {TimeOffset: 2 * time.Second, PlannedVUs: 3},708 },709 stages: []Stage{710 {Target: null.IntFrom(5), Duration: types.NullDurationFrom(0 * time.Second)},711 {Target: null.IntFrom(3), Duration: types.NullDurationFrom(2 * time.Second)},712 },713 start: 2,714 },715 {716 name: "jump up then go up again",717 expectedSteps: []lib.ExecutionStep{718 {TimeOffset: 0 * time.Second, PlannedVUs: 3},719 {TimeOffset: 1 * time.Second, PlannedVUs: 4},720 {TimeOffset: 2 * time.Second, PlannedVUs: 5},721 },722 stages: []Stage{723 {Target: null.IntFrom(5), Duration: types.NullDurationFrom(2 * time.Second)},724 },725 start: 3,726 },727 {728 name: "up down up down",729 expectedSteps: []lib.ExecutionStep{730 {TimeOffset: 0 * time.Second, PlannedVUs: 0},731 {TimeOffset: 1 * time.Second, PlannedVUs: 1},732 {TimeOffset: 2 * time.Second, PlannedVUs: 2},733 {TimeOffset: 3 * time.Second, PlannedVUs: 1},734 {TimeOffset: 4 * time.Second, PlannedVUs: 0},735 {TimeOffset: 5 * time.Second, PlannedVUs: 1},736 {TimeOffset: 6 * time.Second, PlannedVUs: 2},737 {TimeOffset: 7 * time.Second, PlannedVUs: 1},738 {TimeOffset: 8 * time.Second, PlannedVUs: 0},739 },740 stages: []Stage{741 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},742 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},743 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},744 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},745 },746 },747 {748 name: "up down up down in half",749 expectedSteps: []lib.ExecutionStep{750 {TimeOffset: 0 * time.Second, PlannedVUs: 0},751 {TimeOffset: 1 * time.Second, PlannedVUs: 1},752 {TimeOffset: 4 * time.Second, PlannedVUs: 0},753 {TimeOffset: 5 * time.Second, PlannedVUs: 1},754 {TimeOffset: 8 * time.Second, PlannedVUs: 0},755 },756 et: mustNewExecutionTuple(newExecutionSegmentFromString("0:1/2"), nil),757 stages: []Stage{758 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},759 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},760 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},761 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},762 },763 },764 {765 name: "up down up down in the other half",766 expectedSteps: []lib.ExecutionStep{767 {TimeOffset: 0 * time.Second, PlannedVUs: 0},768 {TimeOffset: 2 * time.Second, PlannedVUs: 1},769 {TimeOffset: 3 * time.Second, PlannedVUs: 0},770 {TimeOffset: 6 * time.Second, PlannedVUs: 1},771 {TimeOffset: 7 * time.Second, PlannedVUs: 0},772 },773 et: mustNewExecutionTuple(newExecutionSegmentFromString("1/2:1"), nil),774 stages: []Stage{775 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},776 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},777 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},778 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},779 },780 },781 {782 name: "up down up down in with nothing",783 expectedSteps: []lib.ExecutionStep{784 {TimeOffset: 0 * time.Second, PlannedVUs: 0},785 },786 et: mustNewExecutionTuple(newExecutionSegmentFromString("2/3:1"), newExecutionSegmentSequenceFromString("0,1/3,2/3,1")),787 stages: []Stage{788 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},789 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},790 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},791 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},792 },793 },794 {795 name: "up down up down in with funky sequence", // panics if there are no localIndex == 0 guards796 expectedSteps: []lib.ExecutionStep{797 {TimeOffset: 0 * time.Second, PlannedVUs: 0},798 {TimeOffset: 1 * time.Second, PlannedVUs: 1},799 {TimeOffset: 4 * time.Second, PlannedVUs: 0},800 {TimeOffset: 5 * time.Second, PlannedVUs: 1},801 {TimeOffset: 8 * time.Second, PlannedVUs: 0},802 },803 et: mustNewExecutionTuple(newExecutionSegmentFromString("0:1/3"), newExecutionSegmentSequenceFromString("0,1/3,1/2,2/3,1")),804 stages: []Stage{805 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},806 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},807 {Target: null.IntFrom(2), Duration: types.NullDurationFrom(2 * time.Second)},808 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(2 * time.Second)},809 },810 },811 {812 name: "strange",813 expectedSteps: []lib.ExecutionStep{814 {TimeOffset: 0 * time.Second, PlannedVUs: 0},815 {TimeOffset: 1 * time.Second, PlannedVUs: 1},816 {TimeOffset: 5 * time.Second, PlannedVUs: 2},817 {TimeOffset: 8 * time.Second, PlannedVUs: 3},818 {TimeOffset: 11 * time.Second, PlannedVUs: 4},819 {TimeOffset: 15 * time.Second, PlannedVUs: 5},820 {TimeOffset: 18 * time.Second, PlannedVUs: 6},821 {TimeOffset: 23 * time.Second, PlannedVUs: 7},822 {TimeOffset: 35 * time.Second, PlannedVUs: 8},823 {TimeOffset: 44 * time.Second, PlannedVUs: 9},824 },825 et: mustNewExecutionTuple(newExecutionSegmentFromString("0:0.3"), newExecutionSegmentSequenceFromString("0,0.3,0.6,0.9,1")),826 stages: []Stage{827 {Target: null.IntFrom(20), Duration: types.NullDurationFrom(20 * time.Second)},828 {Target: null.IntFrom(30), Duration: types.NullDurationFrom(30 * time.Second)},829 },830 },831 {832 name: "more up and down",833 expectedSteps: []lib.ExecutionStep{834 {TimeOffset: 0 * time.Second, PlannedVUs: 0},835 {TimeOffset: 1 * time.Second, PlannedVUs: 1},836 {TimeOffset: 2 * time.Second, PlannedVUs: 2},837 {TimeOffset: 3 * time.Second, PlannedVUs: 3},838 {TimeOffset: 4 * time.Second, PlannedVUs: 4},839 {TimeOffset: 5 * time.Second, PlannedVUs: 5},840 {TimeOffset: 6 * time.Second, PlannedVUs: 4},841 {TimeOffset: 7 * time.Second, PlannedVUs: 3},842 {TimeOffset: 8 * time.Second, PlannedVUs: 2},843 {TimeOffset: 9 * time.Second, PlannedVUs: 1},844 {TimeOffset: 10 * time.Second, PlannedVUs: 0},845 },846 stages: []Stage{847 {Target: null.IntFrom(5), Duration: types.NullDurationFrom(5 * time.Second)},848 {Target: null.IntFrom(0), Duration: types.NullDurationFrom(5 * time.Second)},849 },850 },851 }852 for _, testCase := range testCases {853 testCase := testCase854 conf := NewRampingVUsConfig("test")855 conf.StartVUs = null.IntFrom(testCase.start)856 conf.Stages = testCase.stages857 et := testCase.et858 if et == nil {859 et = mustNewExecutionTuple(nil, nil)860 }861 expectedSteps := testCase.expectedSteps862 t.Run(testCase.name, func(t *testing.T) {863 t.Parallel()864 rawStepsNoZeroEnd := conf.getRawExecutionSteps(et, false)865 assert.Equal(t, expectedSteps, rawStepsNoZeroEnd)866 })867 }868}869func BenchmarkRampingVUsGetRawExecutionSteps(b *testing.B) {870 testCases := []struct {871 seq string872 seg string873 }{874 {},875 {seg: "0:1"},876 {seq: "0,0.3,0.5,0.6,0.7,0.8,0.9,1", seg: "0:0.3"},877 {seq: "0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1", seg: "0:0.1"},878 {seg: "2/5:4/5"},879 {seg: "2235/5213:4/5"}, // just wanted it to be ugly ;D880 }881 stageCases := []struct {882 name string883 stages string884 }{885 {886 name: "normal",887 stages: `[{"duration":"5m", "target":5000},{"duration":"5m", "target":5000},{"duration":"5m", "target":10000},{"duration":"5m", "target":10000}]`,888 }, {889 name: "rollercoaster",890 stages: `[{"duration":"5m", "target":5000},{"duration":"5m", "target":0},891 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},892 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},893 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},894 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},895 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},896 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},897 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},898 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},899 {"duration":"5m", "target":5000},{"duration":"5m", "target":0},900 {"duration":"5m", "target":5000},{"duration":"5m", "target":0}]`,901 },902 }903 for _, tc := range testCases {904 tc := tc905 b.Run(fmt.Sprintf("seq:%s;segment:%s", tc.seq, tc.seg), func(b *testing.B) {906 ess, err := lib.NewExecutionSegmentSequenceFromString(tc.seq)907 require.NoError(b, err)908 segment, err := lib.NewExecutionSegmentFromString(tc.seg)909 require.NoError(b, err)910 if tc.seg == "" {911 segment = nil // specifically for the optimization912 }913 et, err := lib.NewExecutionTuple(segment, &ess)914 require.NoError(b, err)915 for _, stageCase := range stageCases {916 var st []Stage917 require.NoError(b, json.Unmarshal([]byte(stageCase.stages), &st))918 vlvc := RampingVUsConfig{919 Stages: st,920 }921 b.Run(stageCase.name, func(b *testing.B) {922 for i := 0; i < b.N; i++ {923 _ = vlvc.getRawExecutionSteps(et, false)924 }925 })926 }927 })928 }929}930// TODO: delete in favor of lib.generateRandomSequence() after931// https://github.com/k6io/k6/issues/1302 is done (can't import now due to932// import loops...)933func generateRandomSequence(t testing.TB, n, m int64, r *rand.Rand) lib.ExecutionSegmentSequence {934 var err error935 ess := lib.ExecutionSegmentSequence(make([]*lib.ExecutionSegment, n))936 numerators := make([]int64, n)937 var denominator int64938 for i := int64(0); i < n; i++ {939 numerators[i] = 1 + r.Int63n(m)940 denominator += numerators[i]941 }942 from := big.NewRat(0, 1)943 for i := int64(0); i < n; i++ {944 to := new(big.Rat).Add(big.NewRat(numerators[i], denominator), from)945 ess[i], err = lib.NewExecutionSegment(from, to)946 require.NoError(t, err)947 from = to948 }949 return ess950}951func TestSumRandomSegmentSequenceMatchesNoSegment(t *testing.T) {952 t.Parallel()953 const (954 numTests = 10955 maxStages = 10956 minStageDuration = 1 * time.Second957 maxStageDuration = 10 * time.Minute958 maxVUs = 300959 segmentSeqMaxLen = 15960 maxNumerator = 300961 )962 getTestConfig := func(r *rand.Rand, name string) RampingVUsConfig {963 stagesCount := 1 + r.Int31n(maxStages)964 stages := make([]Stage, stagesCount)965 for s := int32(0); s < stagesCount; s++ {966 dur := (minStageDuration + time.Duration(r.Int63n(int64(maxStageDuration-minStageDuration)))).Round(time.Second)967 stages[s] = Stage{Duration: types.NullDurationFrom(dur), Target: null.IntFrom(r.Int63n(maxVUs))}968 }969 c := NewRampingVUsConfig(name)970 c.GracefulRampDown = types.NullDurationFrom(0)971 c.GracefulStop = types.NullDurationFrom(0)972 c.StartVUs = null.IntFrom(r.Int63n(maxVUs))973 c.Stages = stages974 return c975 }976 subtractChildSteps := func(t *testing.T, parent, child []lib.ExecutionStep) {977 t.Logf("subtractChildSteps()")978 for _, step := range child {979 t.Logf(" child planned VUs for time offset %s: %d", step.TimeOffset, step.PlannedVUs)980 }981 sub := uint64(0)982 ci := 0983 for pi, p := range parent {984 // We iterate over all parent steps and match them to child steps.985 // Once we have a match, we remove the child step's plannedVUs from986 // the parent steps until a new match, when we adjust the subtracted987 // amount again.988 if p.TimeOffset > child[ci].TimeOffset && ci != len(child)-1 {989 t.Errorf("ERR Could not match child offset %s with any parent time offset", child[ci].TimeOffset)990 }991 if p.TimeOffset == child[ci].TimeOffset {992 t.Logf("Setting sub to %d at t=%s", child[ci].PlannedVUs, child[ci].TimeOffset)993 sub = child[ci].PlannedVUs994 if ci != len(child)-1 {995 ci++996 }997 }998 t.Logf("Subtracting %d VUs (out of %d) at t=%s", sub, p.PlannedVUs, p.TimeOffset)999 parent[pi].PlannedVUs -= sub1000 }1001 }1002 for i := 0; i < numTests; i++ {1003 name := fmt.Sprintf("random%02d", i)1004 t.Run(name, func(t *testing.T) {1005 t.Parallel()1006 seed := time.Now().UnixNano()1007 r := rand.New(rand.NewSource(seed)) //nolint:gosec1008 t.Logf("Random source seeded with %d\n", seed)1009 c := getTestConfig(r, name)1010 ranSeqLen := 2 + r.Int63n(segmentSeqMaxLen-1)1011 t.Logf("Config: %#v, ranSeqLen: %d", c, ranSeqLen)1012 randomSequence := generateRandomSequence(t, ranSeqLen, maxNumerator, r)1013 t.Logf("Random sequence: %s", randomSequence)1014 fullSeg, err := lib.NewExecutionTuple(nil, nil)1015 require.NoError(t, err)1016 fullRawSteps := c.getRawExecutionSteps(fullSeg, false)1017 for _, step := range fullRawSteps {1018 t.Logf("original planned VUs for time offset %s: %d", step.TimeOffset, step.PlannedVUs)1019 }1020 for s := 0; s < len(randomSequence); s++ {1021 et, err := lib.NewExecutionTuple(randomSequence[s], &randomSequence)1022 require.NoError(t, err)1023 segRawSteps := c.getRawExecutionSteps(et, false)1024 subtractChildSteps(t, fullRawSteps, segRawSteps)1025 }1026 for _, step := range fullRawSteps {1027 if step.PlannedVUs != 0 {1028 t.Errorf("ERR Remaining planned VUs for time offset %s are not 0 but %d", step.TimeOffset, step.PlannedVUs)1029 }1030 }1031 })1032 }1033}...
getRawExecutionSteps
Using AI Code Generation
1import (2func main() {3 if err != nil {4 fmt.Println("Failed to connect to the Ethereum client: %v", err)5 }6 txHash := common.HexToHash("0x6f7a0a1d6a0f6e8c6a7d6f0c6f7a6a0f6e8c6a7d6f0c6f7a6a0f6e8c6a7d6f0")7 tx, _, err := client.TransactionByHash(context.Background(), txHash)8 if err != nil {9 fmt.Println("Failed to get transaction: %v", err)10 }11 rawTx, err := rlp.EncodeToBytes(tx)12 if err != nil {13 fmt.Println("Failed to encode transaction: %v", err)14 }15 steps, err := executor.GetRawExecutionSteps(rawTx)16 if err != nil {17 fmt.Println("Failed to get execution steps: %v", err)18 }19 fmt.Println("Execution steps:")20 for i, step := range steps {21 fmt.Println(i, step)22 }23}240 {0x6f7a0a1d6a0f6e8c6a7d6f0c6f7a6a0f6e8c6a7d6f0c6f7a6a0
getRawExecutionSteps
Using AI Code Generation
1import (2func main() {3 tagQ, err := tagquery.NewQueryFromStrings([]string{"name=~server[0-9]+.instance-a.*"})4 if err != nil {5 log.Fatal(4, "failed to parse tag query. %s", err.Error())6 }7 tagQ1, err := tagquery.NewQueryFromStrings([]string{"name=~server[0-9]+.instance-b.*"})8 if err != nil {9 log.Fatal(4, "failed to parse tag query. %s", err.Error())10 }11 tagQ2, err := tagquery.NewQueryFromStrings([]string{"name=~server[0-9]+.instance-c.*"})12 if err != nil {13 log.Fatal(4, "failed to parse tag query. %s", err.Error())14 }15 tagQ3, err := tagquery.NewQueryFromStrings([]string{"name=~server[0-9]+.instance-d.*"})16 if err != nil {17 log.Fatal(4, "failed to parse tag query. %s", err.Error())18 }19 tagQ4, err := tagquery.NewQueryFromStrings([]string{"name=~server[0-9]+.instance-e.*"})20 if err != nil {21 log.Fatal(4, "failed to parse tag query. %s", err.Error())22 }23 tagQ5, err := tagquery.NewQueryFromStrings([]string{"name=~server[0-9]+.instance-f.*"})24 if err != nil {25 log.Fatal(4, "failed to parse tag query. %s", err.Error())26 }27 tagQ6, err := tagquery.NewQueryFromStrings([]string{"name=~server[0-9]+.instance-g.*"})28 if err != nil {
getRawExecutionSteps
Using AI Code Generation
1import (2func main() {3 c := cron.New()4 c.AddFunc(expr, func() {5 fmt.Println("Cron job running...")6 })7 c.Start()8 executionSteps := c.RawExecutionSteps()9 fmt.Println(executionSteps)10 time.Sleep(5 * time.Second)11 c.Stop()12}
getRawExecutionSteps
Using AI Code Generation
1import (2func main() {3 executor := aws.NewExecutor()4 execution_steps := executor.GetRawExecutionSteps()5 fmt.Println(execution_steps)6}
getRawExecutionSteps
Using AI Code Generation
1func main() {2 e := executor.NewExecutor()3 steps, err := e.GetRawExecutionSteps("1.go")4 for _, step := range steps {5 fmt.Println(step)6 }7}
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!!