How to use NewExecutionSegmentSequence method of lib Package

Best K6 code snippet using lib.NewExecutionSegmentSequence

execution_segment.go

Source:execution_segment.go Github

copy

Full Screen

...275// as a comma-separated string of rational numbers "r1,r2,r3,...,rn", which276// represents the sequence (r1, r2], (r2, r3], (r3, r4], ..., (r{n-1}, rn].277// The empty value should be treated as if there is a single (0, 1] segment.278type ExecutionSegmentSequence []*ExecutionSegment279// NewExecutionSegmentSequence validates the that the supplied execution280// segments are non-overlapping and without gaps. It will return a new execution281// segment sequence if that is true, and an error if it's not.282func NewExecutionSegmentSequence(segments ...*ExecutionSegment) (ExecutionSegmentSequence, error) {283 if len(segments) > 1 {284 to := segments[0].to285 for i, segment := range segments[1:] {286 if segment.from.Cmp(to) != 0 {287 return nil, fmt.Errorf(288 "the start value %s of segment #%d should be equal to the end value of the previous one, but it is %s",289 segment.from, i+1, to,290 )291 }292 to = segment.to293 }294 }295 return ExecutionSegmentSequence(segments), nil296}297// NewExecutionSegmentSequenceFromString parses strings of the format298// "r1,r2,r3,...,rn", which represents the sequences like (r1, r2], (r2, r3],299// (r3, r4], ..., (r{n-1}, rn].300func NewExecutionSegmentSequenceFromString(strSeq string) (ExecutionSegmentSequence, error) {301 if len(strSeq) == 0 {302 return nil, nil303 }304 points := strings.Split(strSeq, ",")305 if len(points) < 2 {306 return nil, fmt.Errorf("at least 2 points are needed for an execution segment sequence, %d given", len(points))307 }308 var start *big.Rat309 segments := make([]*ExecutionSegment, 0, len(points)-1)310 for i, point := range points {311 rat, err := stringToRat(point)312 if err != nil {313 return nil, err314 }315 if i == 0 {316 start = rat317 continue318 }319 segment, err := NewExecutionSegment(start, rat)320 if err != nil {321 return nil, err322 }323 segments = append(segments, segment)324 start = rat325 }326 return NewExecutionSegmentSequence(segments...)327}328// UnmarshalText implements the encoding.TextUnmarshaler interface, so that329// execution segment sequences can be specified as CLI flags, environment330// variables, and JSON strings.331func (ess *ExecutionSegmentSequence) UnmarshalText(text []byte) (err error) {332 seq, err := NewExecutionSegmentSequenceFromString(string(text))333 if err != nil {334 return err335 }336 *ess = seq337 return nil338}339// MarshalText implements the encoding.TextMarshaler interface, so is used for340// text and JSON encoding of the execution segment sequences.341func (ess ExecutionSegmentSequence) MarshalText() ([]byte, error) {342 return []byte(ess.String()), nil343}344// String just implements the fmt.Stringer interface, encoding the sequence of345// segments as "start1,end1,end2,end3,...,endn".346func (ess ExecutionSegmentSequence) String() string {347 result := make([]string, 0, len(ess)+1)348 for i, s := range ess {349 if i == 0 {350 result = append(result, s.from.RatString())351 }352 result = append(result, s.to.RatString())353 }354 return strings.Join(result, ",")355}356// LCD calculates the lowest common denominator of the sequence.357// https://en.wikipedia.org/wiki/Least_common_multiple#Using_the_greatest_common_divisor358func (ess ExecutionSegmentSequence) LCD() int64 {359 acc := ess[0].length.Denom().Int64()360 var n int64361 for _, seg := range ess[1:] {362 n = seg.length.Denom().Int64()363 if acc == n || acc%n == 0 { // short circuit364 continue365 }366 acc *= (n / gcd(acc, n))367 }368 return acc369}370// Greatest common divisor371// https://en.wikipedia.org/wiki/Euclidean_algorithm372func gcd(a, b int64) int64 {373 for a != b {374 if a > b {375 a -= b376 } else {377 b -= a378 }379 }380 return a381}382// IsFull returns whether the sequences is full, that is, whether it starts at 0383// and ends at 1. Use GetFilledExecutionSegmentSequence() to get a full sequence.384func (ess ExecutionSegmentSequence) IsFull() bool {385 return ess != nil && len(ess) != 0 && ess[0].from.Cmp(zeroRat) == 0 && ess[len(ess)-1].to.Cmp(oneRat) == 0386}387// FindSegmentPosition returns the index of the supplied execution segment in388// the sequence, or an error if the segment isn't present. This shouldn't be389// used on a nil or empty sequence, it's best to use this method on the result390// of GetFilledExecutionSegmentSequence().391func (ess ExecutionSegmentSequence) FindSegmentPosition(segment *ExecutionSegment) (int, error) {392 from := zeroRat393 if segment != nil {394 from = segment.from395 }396 index := sort.Search(len(ess), func(i int) bool {397 return ess[i].from.Cmp(from) >= 0398 })399 if index < 0 || index >= len(ess) || !ess[index].Equal(segment) {400 return -1, fmt.Errorf("couldn't find segment %s in sequence %s", segment, ess)401 }402 return index, nil403}404// GetFilledExecutionSegmentSequence makes sure we don't have any gaps in the405// given execution segment sequence, or a nil one. It makes sure that the whole406// 0-1 range is filled.407func GetFilledExecutionSegmentSequence(408 sequence *ExecutionSegmentSequence, fallback *ExecutionSegment,409) (result ExecutionSegmentSequence) {410 if sequence == nil || len(*sequence) == 0 {411 if fallback == nil || fallback.length.Cmp(oneRat) == 0 {412 // There is no sequence or a segment, so it means the whole test run413 // is being planned/executed. So we make sure not to have a nil414 // sequence, returning a full; "0,1" sequence instead, otherwise we415 // will need to check for nil everywhere...416 return ExecutionSegmentSequence{newExecutionSegment(zeroRat, oneRat)}417 }418 // We don't have a sequence, but we have a defined segment, so we419 // fill around it with the missing pieces for a full sequence.420 result = ExecutionSegmentSequence{fallback}421 } else {422 result = *sequence423 }424 if result[0].from.Cmp(zeroRat) != 0 {425 es := newExecutionSegment(zeroRat, result[0].from)426 result = append(ExecutionSegmentSequence{es}, result...)427 }428 if result[len(result)-1].to.Cmp(oneRat) != 0 {429 es := newExecutionSegment(result[len(result)-1].to, oneRat)430 result = append(result, es)431 }432 return result433}434// ExecutionSegmentSequenceWrapper is a caching layer on top of the execution435// segment sequence that allows us to make fast and useful calculations, after436// a somewhat slow initialization.437type ExecutionSegmentSequenceWrapper struct {438 ExecutionSegmentSequence // a filled-out segment sequence439 lcd int64 // pre-calculated least common denominator440 // The striped offsets, i.e. the repeating indexes that "belong" to each441 // execution segment in the sequence.442 offsets [][]int64443}444// NewExecutionSegmentSequenceWrapper expects a filled-out execution segment445// sequence. It pre-calculates the initial caches of and returns a new446// ExecutionSegmentSequenceWrapper, but doesn't calculate the striped offsets.447func NewExecutionSegmentSequenceWrapper(ess ExecutionSegmentSequence) *ExecutionSegmentSequenceWrapper {448 if !ess.IsFull() {449 panic(fmt.Sprintf("Cannot wrap around a non-full execution segment sequence '%s'", ess))450 }451 sequenceLength := len(ess)452 offsets := make([][]int64, sequenceLength)453 lcd := ess.LCD()454 // This will contain the normalized numerator values (i.e. what they would have455 // been if all denominators were equal to the LCD), sorted in descending456 // order (i.e. biggest segments are first), with references to their actual457 // indexes in the execution segment sequence (i.e. `seq` above).458 sortedNormalizedIndexes := make([]struct {459 normNumerator int64460 originalIndex int461 }, sequenceLength)462 for i := range ess {463 normalizedNumerator := ess[i].length.Num().Int64() * (lcd / ess[i].length.Denom().Int64())464 sortedNormalizedIndexes[i].normNumerator = normalizedNumerator465 sortedNormalizedIndexes[i].originalIndex = i466 offsets[i] = make([]int64, 0, normalizedNumerator+1)467 }468 sort.SliceStable(sortedNormalizedIndexes, func(i, j int) bool {469 return sortedNormalizedIndexes[i].normNumerator > sortedNormalizedIndexes[j].normNumerator470 })471 // This is the striping algorithm. Imagine you have a number of rational472 // numbers which all add up to 1 (or less), and call them segments. If you473 // want each to get proportional amount of anything, you need to give them474 // their numerator count of elements for each denominator amount from the475 // original elements. So, for 1/3, you give 1 element for each 3 elements.476 // For 3/5 - 3 elements for each 5. If you have, for example, a sequence477 // with elements with length 3/5 and 1/3, in order to know how to distribute478 // it accurately, you need to get the LCD(lowest common denominitor). In479 // this case, between 3 and 5, the LCD is 15. Then to transform the numbers480 // to have the same, LCD equal, denominator. So 3/5 becomes 9/15 and 1/3481 // becomes 5/15. So now for each 15 elements 9 need to go to the 3/5, and 5482 // need to go to 1/3. This is what we did above in sortedNormalizedIndexes.483 //484 // We use the algorithm below to split elements between ExecutionSegments by485 // using their length as the rational number. As we would like to get486 // non-sequential elements, we try to get the maximum distance between them.487 // That is the number of elements divided by the number of elements for any488 // given segment, which concidently is the length of the segment reversed.489 // The algorithm below does the following:490 // 1. Goes through the elements from 0 to the lcd-1491 // 2. For each of element, it goes through the segments and looks if the492 // amount of already taken elements by the given segment, multiplied by493 // that segment's length inverted, is equal to or less to the current494 // element index. If it is, give that element to that segment. If not,495 // continue with the next element.496 // The code below specifically avoids using big.Rat, for performance497 // reasons, which complicates the code somewhat. As additional note, the498 // sorting of the segments from biggest to smallest helps with the fact that499 // the biggest elements will need to take the most elements, and for them it500 // will be the hardest to not get sequential elements.501 prev := make([]int64, sequenceLength)502 chosenCounts := make([]int64, sequenceLength)503 saveIndex := func(iteration int64, index int, numerator int64) {504 offsets[index] = append(offsets[index], iteration-prev[index])505 prev[index] = iteration506 if int64(len(offsets[index])) == numerator {507 offsets[index] = append(offsets[index], offsets[index][0]+lcd-iteration)508 }509 }510 for i := int64(0); i < lcd; i++ {511 for sortedIndex, chosenCount := range chosenCounts {512 num := chosenCount * lcd513 denom := sortedNormalizedIndexes[sortedIndex].normNumerator514 if i > num/denom || (i == num/denom && num%denom == 0) {515 chosenCounts[sortedIndex]++516 saveIndex(i, sortedNormalizedIndexes[sortedIndex].originalIndex, denom)517 break518 }519 }520 }521 return &ExecutionSegmentSequenceWrapper{ExecutionSegmentSequence: ess, lcd: lcd, offsets: offsets}522}523// LCD returns the (cached) least common denominator of the sequence - no need524// to calculate it again, since we did it in the constructor.525func (essw *ExecutionSegmentSequenceWrapper) LCD() int64 {526 return essw.lcd527}528// ScaleInt64 scales the provided value for the given segment.529func (essw *ExecutionSegmentSequenceWrapper) ScaleInt64(segmentIndex int, value int64) int64 {530 start := essw.offsets[segmentIndex][0]531 offsets := essw.offsets[segmentIndex][1:]532 result := (value / essw.lcd) * int64(len(offsets))533 for gi, i := 0, start; i < value%essw.lcd; gi, i = gi+1, i+offsets[gi] {534 result++535 }536 return result537}538// GetStripedOffsets returns the stripped offsets for the given segment539// the returned values are as follows in order:540// - start: the first value that is for the segment541// - offsets: a list of offsets from the previous value for the segment. This are only the offsets542// to from the start to the next start if we chunk the elements we are going to strip543// into lcd sized chunks544// - lcd: the LCD of the lengths of all segments in the sequence. This is also the number of545// elements after which the algorithm starts to loop and give the same values546func (essw *ExecutionSegmentSequenceWrapper) GetStripedOffsets(segmentIndex int) (int64, []int64, int64) {547 offsets := essw.offsets[segmentIndex]548 return offsets[0], offsets[1:], essw.lcd549}550// GetTuple returns an ExecutionTuple for the specified segment index.551func (essw *ExecutionSegmentSequenceWrapper) GetTuple(segmentIndex int) *ExecutionTuple {552 return &ExecutionTuple{553 Sequence: essw,554 Segment: essw.ExecutionSegmentSequence[segmentIndex],555 SegmentIndex: segmentIndex,556 }557}558// GetNewExecutionSegmentSequenceFromValue uses the value provided, splits it559// between all the segments, using the striping offsets in the sequence,560// generating a new segment sequence. It then returns a new561// ExecutionSegmentSequenceWrapper, with the new sequence and segments, such562// that each new segment in the new sequence has length `Scale(value)/value`563// while keeping the order.564//565// Additionally, the position of a given segment index can be tracked (since566// empty segments are removed), so that you can reconstruct an ExecutionTuple,567// if required. If the segment with the trackedIndex is not part of the new568// sequence, or if a new sequence cannot be generated (for example, for 0569// values), an error will be returned.570func (essw *ExecutionSegmentSequenceWrapper) GetNewExecutionSegmentSequenceFromValue(value int64, trackedIndex int) (571 newSequence *ExecutionSegmentSequenceWrapper, newIndex int, err error,572) {573 if value < 1 {574 return nil, -1, fmt.Errorf("cannot generate new sequence for value %d", value)575 }576 if value%essw.lcd == 0 { // the value is perfectly divisible so we will get the same tuple577 return essw, trackedIndex, nil578 }579 newIndex = -1580 newESS := make(ExecutionSegmentSequence, 0, len(essw.ExecutionSegmentSequence)) // this can be smaller581 prev := int64(0)582 for i := range essw.ExecutionSegmentSequence {583 newValue := essw.ScaleInt64(i, value)584 if newValue == 0 {585 continue586 }587 currentES := newExecutionSegment(big.NewRat(prev, value), big.NewRat(prev+newValue, value))588 prev += newValue589 if i == trackedIndex {590 newIndex = len(newESS)591 }592 newESS = append(newESS, currentES)593 }594 if newIndex == -1 {595 return nil, -1, fmt.Errorf(596 "segment %d (%s) isn't present in the new sequence",597 trackedIndex, essw.ExecutionSegmentSequence[trackedIndex],598 )599 }600 return NewExecutionSegmentSequenceWrapper(newESS), newIndex, nil601}602// ExecutionTuple is the combination of an ExecutionSegmentSequence(Wrapper) and603// a specific ExecutionSegment from it. It gives easy access to the efficient604// scaling and striping algorithms for that specific segment, since the results605// are cached in the sequence wrapper.606type ExecutionTuple struct { // TODO rename? make fields private and have getter methods?607 Sequence *ExecutionSegmentSequenceWrapper608 Segment *ExecutionSegment609 SegmentIndex int610}611func (et *ExecutionTuple) String() string {612 return fmt.Sprintf("%s in %s", et.Segment, et.Sequence)613}614// NewExecutionTuple returns a new ExecutionTuple for the provided segment and615// sequence.616//617// TODO: don't return a pointer?618func NewExecutionTuple(segment *ExecutionSegment, sequence *ExecutionSegmentSequence) (*ExecutionTuple, error) {619 filledSeq := GetFilledExecutionSegmentSequence(sequence, segment)620 wrapper := NewExecutionSegmentSequenceWrapper(filledSeq)621 index, err := wrapper.FindSegmentPosition(segment)622 if err != nil {623 return nil, err624 }625 return &ExecutionTuple{Sequence: wrapper, Segment: segment, SegmentIndex: index}, nil626}627// ScaleInt64 scales the provided value for our execution segment.628func (et *ExecutionTuple) ScaleInt64(value int64) int64 {629 if len(et.Sequence.ExecutionSegmentSequence) == 1 {630 return value // if we don't have any segmentation, just return the original value631 }632 return et.Sequence.ScaleInt64(et.SegmentIndex, value)633}634// GetStripedOffsets returns the striped offsets for our execution segment.635func (et *ExecutionTuple) GetStripedOffsets() (int64, []int64, int64) {636 return et.Sequence.GetStripedOffsets(et.SegmentIndex)637}638// GetNewExecutionTupleFromValue re-segments the sequence, based on the given639// value (see GetNewExecutionSegmentSequenceFromValue() above), and either640// returns the new tuple, or an error if the current segment isn't present in641// the new sequence.642func (et *ExecutionTuple) GetNewExecutionTupleFromValue(value int64) (*ExecutionTuple, error) {643 newSequenceWrapper, newIndex, err := et.Sequence.GetNewExecutionSegmentSequenceFromValue(value, et.SegmentIndex)644 if err != nil {645 return nil, err646 }647 return &ExecutionTuple{648 Sequence: newSequenceWrapper,649 Segment: newSequenceWrapper.ExecutionSegmentSequence[newIndex],650 SegmentIndex: newIndex,651 }, nil652}...

Full Screen

Full Screen

NewExecutionSegmentSequence

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 transport := transporttest.NewRecorderTransport()4 defer transport.Close()5 tracer, err := elasticapm.NewTracer("mytracer", "1.0.0")6 if err != nil {7 }8 defer tracer.Close()9 tx := tracer.StartTransaction("name", "type")10 tx.Context.SetLabel("language", "go")11 tx.Context.SetLabel("fr

Full Screen

Full Screen

NewExecutionSegmentSequence

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 transport := transporttest.NewRecorderTransport()4 tracer, err := elasticapm.NewTracerOptions(elasticapm.TracerOptions{5 })6 if err != nil {7 }8 defer tracer.Close()9 mux := http.NewServeMux()10 mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {11 fmt.Fprintf(w, "Hello, world!")12 })13 handler := apmhttp.Wrap(mux)14 server := httptest.NewServer(handler)15 defer server.Close()16 client := server.Client()17 res, err := client.Get(server.URL)18 if err != nil {19 }20 res.Body.Close()21 tracer.Flush(nil)22 fmt.Println(transport.Payloads())23}24[{"metadata":{"service":{"name":"service","agent":{"name":"go","version":"1.0.0"}},"system":{"hostname":"localhost"}},"transactions":[{"id":"d7c0b9d3e7c0c3f5","trace_id":"d7c0b9d3e7c0c3f5","parent_id":"d7c0b9d3e7c0c3f5","name":"GET /","type":"request","result":"HTTP 2xx","duration":0.000

Full Screen

Full Screen

NewExecutionSegmentSequence

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ess := lib.NewExecutionSegmentSequence(lib.NewExecutionSegment(0, 1))4 fmt.Println(ess)5}6{[0]}7import (8func main() {9 es := lib.NewExecutionSegment(0, 1)10 fmt.Println(es)11}12{0 1}13import (14func main() {15 ess := lib.NewExecutionSegmentSequence(lib.NewExecutionSegment(0, 1))16 fmt.Println(ess)17}18{[0]}19import (20func main() {21 es := lib.NewExecutionSegment(0, 1)22 fmt.Println(es)23}24{0 1}25import (26func main() {27 ess := lib.NewExecutionSegmentSequence(lib.NewExecutionSegment(0, 1))28 fmt.Println(ess)29}30{[0]}31import (32func main() {33 es := lib.NewExecutionSegment(0, 1)34 fmt.Println(es)35}36{0 1}37import (38func main() {

Full Screen

Full Screen

NewExecutionSegmentSequence

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 segments := lib.NewExecutionSegmentSequence(lib.ExecutionSegment{Start: 0, Length: 0.3}, lib.ExecutionSegment{Start: 0.3, Length: 0.6})5 fmt.Println(segments)6}

Full Screen

Full Screen

NewExecutionSegmentSequence

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatalf("Dial: %s", err)5 }6 defer conn.Close()7 ch, err := conn.Channel()8 if err != nil {9 log.Fatalf("Channel: %s", err)10 }11 defer ch.Close()12 rabbusConn, err := rabbus.NewConnection(13 rabbus.ConnectionChannel(ch),14 if err != nil {15 log.Fatalf("Rabbus: %s", err)16 }17 defer rabbusConn.Close()18 sequence := rabbus.NewExecutionSegmentSequence(19 rabbus.NewExecutionSegment(rabbus.NewExecutionSegment("rabbus", "hello"), 1),20 rabbus.NewExecutionSegment(rabbus.NewExecutionSegment("rabbus", "hello"), 2),21 rabbusConn.SetExecutionSegmentSequence(sequence)22 err = rabbusConn.Publish("rabbus", "hello", []byte("Hello World!"), rabbus.WithContentType("text/plain"))23 if err != nil {24 log.Fatalf("Publish: %s", err)25 }26 fmt.Println("Message Published")27 time.Sleep(2 * time.Second)28}29import (30func main() {31 if err != nil {32 log.Fatalf("Dial: %s", err)33 }34 defer conn.Close()35 ch, err := conn.Channel()36 if err != nil {37 log.Fatalf("Channel: %s", err)38 }39 defer ch.Close()40 rabbusConn, err := rabbus.NewConnection(

Full Screen

Full Screen

NewExecutionSegmentSequence

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 executionSegmentSequence = libObj.NewExecutionSegmentSequence()4 fmt.Println(executionSegmentSequence)5}6import (7func main() {8 executionSegment = libObj.NewExecutionSegment()9 fmt.Println(executionSegment)10}11import (12func main() {13 executionSegmentSequence = libObj.NewExecutionSegmentSequence()14 fmt.Println(executionSegmentSequence)15}16import (17func main() {18 executionSegment = libObj.NewExecutionSegment()19 fmt.Println(executionSegment)20}21import (22func main() {23 executionSegmentSequence = libObj.NewExecutionSegmentSequence()24 fmt.Println(executionSegmentSequence)25}26import (

Full Screen

Full Screen

NewExecutionSegmentSequence

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ess := execution_segment_sequence.NewExecutionSegmentSequence()4 ess.NewExecutionSegmentSequence("1-2,4-6,7,8-10,12")5 fmt.Println(ess)6}7import (8func main() {9 ess := execution_segment_sequence.NewExecutionSegmentSequence()10 ess.NewExecutionSegmentSequence("1-2,4-6,7,8-10,12", "1-2,4-6,7,8-10,12")11 fmt.Println(ess)12}13import (14func main() {15 ess := execution_segment_sequence.NewExecutionSegmentSequence()16 ess.NewExecutionSegmentSequence("1-2,4-6,7,8-10,12", "1-2,4-6,7,8-10,12", "1-2,4-6,7,8-10,12")17 fmt.Println(ess)18}19import (

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