Skip to content

Commit

Permalink
Fix some formatting issues by running gofmt -w -s
Browse files Browse the repository at this point in the history
The only exceptions were some `go generate`d code.
  • Loading branch information
na-- committed Mar 31, 2023
1 parent 4b30bf4 commit 07630ed
Show file tree
Hide file tree
Showing 22 changed files with 166 additions and 136 deletions.
2 changes: 2 additions & 0 deletions js/common/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

// if a fieldName is the key of this map exactly than the value for the given key should be used as
// the name of the field in js
//
//nolint:gochecknoglobals
var fieldNameExceptions = map[string]string{
"OCSP": "ocsp",
Expand Down Expand Up @@ -41,6 +42,7 @@ func FieldName(t reflect.Type, f reflect.StructField) string {

// if a methodName is the key of this map exactly than the value for the given key should be used as
// the name of the method in js
//
//nolint:gochecknoglobals
var methodNameExceptions = map[string]string{
"JSON": "json",
Expand Down
40 changes: 20 additions & 20 deletions js/eventloop/eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ func (e *EventLoop) wakeup() {
//
// A common pattern for async work is something like this:
//
// func doAsyncWork(vu modules.VU) *goja.Promise {
// enqueueCallback := vu.RegisterCallback()
// p, resolve, reject := vu.Runtime().NewPromise()
// func doAsyncWork(vu modules.VU) *goja.Promise {
// enqueueCallback := vu.RegisterCallback()
// p, resolve, reject := vu.Runtime().NewPromise()
//
// // Do the actual async work in a new independent goroutine, but make
// // sure that the Promise resolution is done on the main thread:
// go func() {
// // Also make sure to abort early if the context is cancelled, so
// // the VU is not stuck when the scenario ends or Ctrl+C is used:
// result, err := doTheActualAsyncWork(vu.Context())
// enqueueCallback(func() error {
// if err != nil {
// reject(err)
// } else {
// resolve(result)
// }
// return nil // do not abort the iteration
// })
// }()
// // Do the actual async work in a new independent goroutine, but make
// // sure that the Promise resolution is done on the main thread:
// go func() {
// // Also make sure to abort early if the context is cancelled, so
// // the VU is not stuck when the scenario ends or Ctrl+C is used:
// result, err := doTheActualAsyncWork(vu.Context())
// enqueueCallback(func() error {
// if err != nil {
// reject(err)
// } else {
// resolve(result)
// }
// return nil // do not abort the iteration
// })
// }()
//
// return p
// }
// return p
// }
//
// This ensures that the actual work happens asynchronously, while the Promise
// is immediately returned and the main thread resumes execution. It also
Expand Down
1 change: 1 addition & 0 deletions js/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

// Copied from https://github.com/k6io/jslib.k6.io/tree/master/lib/k6-summary
//
//go:embed summary.js
var jslibSummaryCode string //nolint:gochecknoglobals

Expand Down
43 changes: 24 additions & 19 deletions lib/execution_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ func stringToRat(s string) (*big.Rat, error) {
// quickly running an arbitrarily scaled-down version of a test.
//
// The parsing logic is that values with a colon, i.e. ':', are full segments:
// `1/2:3/4`, `0.5:0.75`, `50%:75%`, and even `2/4:75%` should be (1/2, 3/4]
//
// `1/2:3/4`, `0.5:0.75`, `50%:75%`, and even `2/4:75%` should be (1/2, 3/4]
//
// And values without a colon are the end of a first segment:
// `20%`, `0.2`, and `1/5` should be converted to (0, 1/5]
//
// `20%`, `0.2`, and `1/5` should be converted to (0, 1/5]
//
// empty values should probably be treated as "1", i.e. the whole execution
func NewExecutionSegmentFromString(toStr string) (result *ExecutionSegment, err error) {
from := zeroRat
Expand Down Expand Up @@ -210,8 +214,9 @@ func (es *ExecutionSegment) Equal(other *ExecutionSegment) bool {
// (0:1/2], then a.SubSegment(b) will return a new segment (1/2, 3/4].
//
// The basic formula for c = a.SubSegment(b) is:
// c.from = a.from + b.from * (a.to - a.from)
// c.to = c.from + (b.to - b.from) * (a.to - a.from)
//
// c.from = a.from + b.from * (a.to - a.from)
// c.to = c.from + (b.to - b.from) * (a.to - a.from)
func (es *ExecutionSegment) SubSegment(child *ExecutionSegment) *ExecutionSegment {
if child == nil {
return es // 100% sub-segment is the original segment
Expand Down Expand Up @@ -584,12 +589,12 @@ func (essw *ExecutionSegmentSequenceWrapper) ScaleInt64(segmentIndex int, value

// GetStripedOffsets returns the stripped offsets for the given segment
// the returned values are as follows in order:
// - start: the first value that is for the segment
// - offsets: a list of offsets from the previous value for the segment. This are only the offsets
// to from the start to the next start if we chunk the elements we are going to strip
// into lcd sized chunks
// - lcd: the LCD of the lengths of all segments in the sequence. This is also the number of
// elements after which the algorithm starts to loop and give the same values
// - start: the first value that is for the segment
// - offsets: a list of offsets from the previous value for the segment. This are only the offsets
// to from the start to the next start if we chunk the elements we are going to strip
// into lcd sized chunks
// - lcd: the LCD of the lengths of all segments in the sequence. This is also the number of
// elements after which the algorithm starts to loop and give the same values
func (essw *ExecutionSegmentSequenceWrapper) GetStripedOffsets(segmentIndex int) (int64, []int64, int64) {
offsets := essw.offsets[segmentIndex]
return offsets[0], offsets[1:], essw.lcd
Expand Down Expand Up @@ -670,22 +675,22 @@ func (essw *ExecutionSegmentSequenceWrapper) GetNewExecutionSegmentSequenceFromV
//
// The segmented iterators (i.e. SegmentedIndex values below) will be like this:
//
// Normal iterator: 0 1 2 3 4 5 6 7 8 9 10 11 ...
// Instance 1 (0:1/2) iterator: 0 2 4 6 8 10 ...
// Instance 2 (1/2:3/4) iterator: 1 5 9 ...
// Instance 2 (3/4:1) iterator: 3 7 11 ...
// Normal iterator: 0 1 2 3 4 5 6 7 8 9 10 11 ...
// Instance 1 (0:1/2) iterator: 0 2 4 6 8 10 ...
// Instance 2 (1/2:3/4) iterator: 1 5 9 ...
// Instance 2 (3/4:1) iterator: 3 7 11 ...
//
// See how every instance has its own uniqe non-overlapping iterator, but when
// we combine all of them, we cover every possible value in the original one.
//
// We also can use this property to scale integer numbers proportionally, as
// fairly as possible, between the instances, like this:
//
// Global int value to scale: 1 2 3 4 5 6 7 8 9 10 11 12 ...
// Calling ScaleInt64():
// - Instance 1 (0:1/2) value: 1 1 2 2 3 3 4 4 5 5 6 6 ...
// - Instance 2 (1/2:3/4) value: 0 1 1 1 1 2 2 2 2 3 3 3 ...
// - Instance 2 (3/4:1) value: 0 0 0 1 1 1 1 2 2 2 2 3 ...
// Global int value to scale: 1 2 3 4 5 6 7 8 9 10 11 12 ...
// Calling ScaleInt64():
// - Instance 1 (0:1/2) value: 1 1 2 2 3 3 4 4 5 5 6 6 ...
// - Instance 2 (1/2:3/4) value: 0 1 1 1 1 2 2 2 2 3 3 3 ...
// - Instance 2 (3/4:1) value: 0 0 0 1 1 1 1 2 2 2 2 3 ...
//
// Notice how the sum of the per-instance values is always equal to the global
// value - this is what ExecutionTuple.ScaleInt64() does. Also compare both
Expand Down
1 change: 1 addition & 0 deletions lib/executor/constant_arrival_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (car *ConstantArrivalRate) Init(ctx context.Context) error {
// time should iteration X begin) different, but keep everything else the same.
// This will allow us to implement https://github.com/k6io/k6/issues/1386
// and things like all of the TODOs below in one place only.
//
//nolint:funlen,cyclop
func (car ConstantArrivalRate) Run(parentCtx context.Context, out chan<- metrics.SampleContainer) (err error) {
gracefulStop := car.config.GetGracefulStop()
Expand Down
1 change: 1 addition & 0 deletions lib/executor/externally_controlled.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ func (rs *externallyControlledRunState) handleConfigChange(oldCfg, newCfg Extern
// Run constantly loops through as many iterations as possible on a variable
// dynamically controlled number of VUs either for the specified duration, or
// until the test is manually stopped.
//
//nolint:funlen,gocognit
func (mex *ExternallyControlled) Run(parentCtx context.Context, out chan<- metrics.SampleContainer) (err error) {
mex.configLock.RLock()
Expand Down
22 changes: 11 additions & 11 deletions lib/executor/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ func getIterationRunner(
// only the regular duration.
//
// In either case, the usage of these contexts should be like this:
// - As long as the regDurationCtx isn't done, new iterations can be started.
// - After regDurationCtx is done, no new iterations should be started; every
// VU that finishes an iteration from now on can be returned to the buffer
// pool in the ExecutionState struct.
// - After maxDurationCtx is done, any VUs with iterations will be
// interrupted by the context's closing and will be returned to the buffer.
// - If you want to interrupt the execution of all VUs prematurely (e.g. there
// was an error or something like that), trigger maxDurationCancel().
// - If the whole test is aborted, the parent context will be cancelled, so
// that will also cancel these contexts, thus the "general abort" case is
// handled transparently.
// - As long as the regDurationCtx isn't done, new iterations can be started.
// - After regDurationCtx is done, no new iterations should be started; every
// VU that finishes an iteration from now on can be returned to the buffer
// pool in the ExecutionState struct.
// - After maxDurationCtx is done, any VUs with iterations will be
// interrupted by the context's closing and will be returned to the buffer.
// - If you want to interrupt the execution of all VUs prematurely (e.g. there
// was an error or something like that), trigger maxDurationCancel().
// - If the whole test is aborted, the parent context will be cancelled, so
// that will also cancel these contexts, thus the "general abort" case is
// handled transparently.
func getDurationContexts(parentCtx context.Context, regularDuration, gracefulStop time.Duration) (
startTime time.Time, maxDurationCtx, regDurationCtx context.Context, maxDurationCancel func(),
) {
Expand Down
1 change: 1 addition & 0 deletions lib/executor/per_vu_iterations.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type PerVUIterations struct {
var _ lib.Executor = &PerVUIterations{}

// Run executes a specific number of iterations with each configured VU.
//
//nolint:funlen
func (pvi PerVUIterations) Run(parentCtx context.Context, out chan<- metrics.SampleContainer) (err error) {
numVUs := pvi.config.GetVUs(pvi.executionState.ExecutionTuple)
Expand Down
23 changes: 13 additions & 10 deletions lib/executor/ramping_arrival_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,18 @@ func (varr *RampingArrivalRate) Init(ctx context.Context) error {
// Lets look at a simple example - lets say we start with 2 events and the first stage is 5
// seconds to 2 events/s and then we have a second stage for 5 second that goes up to 3 events
// (using small numbers because ... well it is easier :D). This will look something like:
// ^
// 7|
// 6|
// 5|
// 4|
// 3| ,-+
// 2|----+-' |
// 1| | |
// +----+----+---------------------------------->
// 0s 5s 10s
//
// ^
// 7|
// 6|
// 5|
// 4|
// 3| ,-+
// 2|----+-' |
// 1| | |
// +----+----+---------------------------------->
// 0s 5s 10s
//
// TODO: bigger and more stages
//
// Now the question is when(where on the graph) does the first event happen? Well in this simple
Expand Down Expand Up @@ -298,6 +300,7 @@ func noNegativeSqrt(f float64) float64 {
// time should iteration X begin) different, but keep everyhing else the same.
// This will allow us to implement https://github.com/k6io/k6/issues/1386
// and things like all of the TODOs below in one place only.
//
//nolint:funlen,cyclop
func (varr RampingArrivalRate) Run(parentCtx context.Context, out chan<- metrics.SampleContainer) (err error) {
segment := varr.executionState.ExecutionTuple.Segment
Expand Down
123 changes: 61 additions & 62 deletions lib/executor/ramping_vus.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,63 +106,63 @@ func (vlvc RampingVUsConfig) Validate() []error {
// 2 equal segments (i.e. execution segments "0:0.5" and "0.5:1"). The original
// execution steps would look something like this:
//
// VUs ^
// 10| *
// 9| ***
// 8| *****
// 7| *******
// 6| *********
// 5| ***********
// 4| *************
// 3| ***************
// 2| *****************
// 1| *******************
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
// VUs ^
// 10| *
// 9| ***
// 8| *****
// 7| *******
// 6| *********
// 5| ***********
// 4| *************
// 3| ***************
// 2| *****************
// 1| *******************
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
//
// The chart for one of the execution segments would look like this:
//
// VUs ^
// 5| XXX
// 4| XXXXXXX
// 3| XXXXXXXXXXX
// 2| XXXXXXXXXXXXXXX
// 1| XXXXXXXXXXXXXXXXXXX
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
// VUs ^
// 5| XXX
// 4| XXXXXXX
// 3| XXXXXXXXXXX
// 2| XXXXXXXXXXXXXXX
// 1| XXXXXXXXXXXXXXXXXXX
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
//
// And the chart for the other execution segment would look like this:
//
// VUs ^
// 5| Y
// 4| YYYYY
// 3| YYYYYYYYY
// 2| YYYYYYYYYYYYY
// 1| YYYYYYYYYYYYYYYYY
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
// VUs ^
// 5| Y
// 4| YYYYY
// 3| YYYYYYYYY
// 2| YYYYYYYYYYYYY
// 1| YYYYYYYYYYYYYYYYY
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
//
// Notice the time offsets and the slower ramping up and down. All of that is
// because the sum of the two execution segments has to produce exactly the
// original shape, as if the test ran on a single machine:
//
// VUs ^
// 10| Y
// 9| XXX
// 8| YYYYY
// 7| XXXXXXX
// 6| YYYYYYYYY
// 5| XXXXXXXXXXX
// 4| YYYYYYYYYYYYY
// 3| XXXXXXXXXXXXXXX
// 2| YYYYYYYYYYYYYYYYY
// 1| XXXXXXXXXXXXXXXXXXX
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
// VUs ^
// 10| Y
// 9| XXX
// 8| YYYYY
// 7| XXXXXXX
// 6| YYYYYYYYY
// 5| XXXXXXXXXXX
// 4| YYYYYYYYYYYYY
// 3| XXXXXXXXXXXXXXX
// 2| YYYYYYYYYYYYYYYYY
// 1| XXXXXXXXXXXXXXXXXXX
// 0------------------------> time(s)
// 01234567890123456789012 (t%10)
// 00000000001111111111222 (t/10)
//
// More information: https://github.com/k6io/k6/issues/997#issuecomment-484416866
func (vlvc RampingVUsConfig) getRawExecutionSteps(et *lib.ExecutionTuple, zeroEnd bool) []lib.ExecutionStep {
Expand Down Expand Up @@ -273,18 +273,17 @@ func (vlvc RampingVUsConfig) precalculateTheRequiredSteps(et *lib.ExecutionTuple
// iterations), where stars represent actively scheduled VUs and dots are used
// for VUs that are potentially finishing up iterations:
//
//
// ^
// |
// VUs 6| *..............................
// 5| ***.......*..............................
// 4|*****.....***.....**..............................
// 3|******...*****...***..............................
// 2|*******.*******.****..............................
// 1|***********************..............................
// 0--------------------------------------------------------> time(s)
// 012345678901234567890123456789012345678901234567890123 (t%10)
// 000000000011111111112222222222333333333344444444445555 (t/10)
// VUs ^
// |
// 6| *..............................
// 5| ***.......*..............................
// 4|*****.....***.....**..............................
// 3|******...*****...***..............................
// 2|*******.*******.****..............................
// 1|***********************..............................
// 0--------------------------------------------------------> time(s)
// 012345678901234567890123456789012345678901234567890123 (t%10)
// 000000000011111111112222222222333333333344444444445555 (t/10)
//
// We start with 4 VUs, scale to 6, scale down to 1, scale up to 5, scale down
// to 1 again, scale up to 4, back to 1, and finally back down to 0. If our
Expand Down Expand Up @@ -425,10 +424,10 @@ func (vlvc RampingVUsConfig) reserveVUsForGracefulRampDowns( //nolint:funlen
// - gracefulStop can be less than gracefulRampDown and can cut the graceful
// ramp-down periods of the last VUs short.
// - gracefulRampDown can be more than gracefulStop:
// - If the user manually ramped down VUs at the end of the test (i.e. the
// last stage's target is 0), then this will have no effect.
// - If the last stage's target is more than 0, the VUs at the end of the
// executor's life will have more time to finish their last iterations.
// - If the user manually ramped down VUs at the end of the test (i.e. the
// last stage's target is 0), then this will have no effect.
// - If the last stage's target is more than 0, the VUs at the end of the
// executor's life will have more time to finish their last iterations.
func (vlvc RampingVUsConfig) GetExecutionRequirements(et *lib.ExecutionTuple) []lib.ExecutionStep {
steps := vlvc.getRawExecutionSteps(et, false)

Expand Down
Loading

0 comments on commit 07630ed

Please sign in to comment.