/
constant_looping_vus.go
113 lines (98 loc) · 3.72 KB
/
constant_looping_vus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2019 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package scheduler
import (
"fmt"
"time"
"github.com/loadimpact/k6/lib/types"
null "gopkg.in/guregu/null.v3"
)
const constantLoopingVUsType = "constant-looping-vus"
func init() {
RegisterConfigType(constantLoopingVUsType, func(name string, rawJSON []byte) (Config, error) {
config := NewConstantLoopingVUsConfig(name)
err := strictJSONUnmarshal(rawJSON, &config)
return config, err
})
}
// The minimum duration we'll allow users to schedule. This doesn't affect the stages
// configuration, where 0-duration virtual stages are allowed for instantaneous VU jumps
const minDuration = 1 * time.Second
// ConstantLoopingVUsConfig stores VUs and duration
type ConstantLoopingVUsConfig struct {
BaseConfig
VUs null.Int `json:"vus"`
Duration types.NullDuration `json:"duration"`
}
// NewConstantLoopingVUsConfig returns a ConstantLoopingVUsConfig with default values
func NewConstantLoopingVUsConfig(name string) ConstantLoopingVUsConfig {
return ConstantLoopingVUsConfig{
BaseConfig: NewBaseConfig(name, constantLoopingVUsType, false),
VUs: null.NewInt(1, false),
}
}
// Make sure we implement the Config interface
var _ Config = &ConstantLoopingVUsConfig{}
// Validate makes sure all options are configured and valid
func (lcv ConstantLoopingVUsConfig) Validate() []error {
errors := lcv.BaseConfig.Validate()
if lcv.VUs.Int64 <= 0 {
errors = append(errors, fmt.Errorf("the number of VUs should be more than 0"))
}
if !lcv.Duration.Valid {
errors = append(errors, fmt.Errorf("the duration is unspecified"))
} else if time.Duration(lcv.Duration.Duration) < minDuration {
errors = append(errors, fmt.Errorf(
"the duration should be at least %s, but is %s", minDuration, lcv.Duration,
))
}
return errors
}
// GetMaxVUs returns the absolute maximum number of possible concurrently running VUs
func (lcv ConstantLoopingVUsConfig) GetMaxVUs() int64 {
return lcv.VUs.Int64
}
// GetMaxDuration returns the maximum duration time for this scheduler, including
// the specified iterationTimeout, if the iterations are uninterruptible
func (lcv ConstantLoopingVUsConfig) GetMaxDuration() time.Duration {
maxDuration := lcv.Duration.Duration
if !lcv.Interruptible.Bool {
maxDuration += lcv.IterationTimeout.Duration
}
return time.Duration(maxDuration)
}
// Split divides the VUS as best it can, but keeps the same duration
func (lcv ConstantLoopingVUsConfig) Split(percentages []float64) ([]Config, error) {
if err := checkPercentagesSum(percentages); err != nil {
return nil, err
}
configs := make([]Config, len(percentages))
for i, p := range percentages {
//TODO: figure out a better approach for the proportional distribution
// of the VUs (which are indivisible items)...
// Some sort of "pick closest match to percentage and adjust remaining"?
configs[i] = &ConstantLoopingVUsConfig{
BaseConfig: *lcv.BaseConfig.CopyWithPercentage(p),
VUs: null.IntFrom(int64(float64(lcv.VUs.Int64) / p)),
Duration: lcv.Duration,
}
}
return configs, nil
}