Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0805f2e

Browse files
committedSep 29, 2023
replace --min-wait and --max-wait with --interval and --jitter
1 parent cf10151 commit 0805f2e

File tree

5 files changed

+46
-44
lines changed

5 files changed

+46
-44
lines changed
 

‎cli/exp_scaletest.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,8 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
10471047

10481048
func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
10491049
var (
1050-
minWait time.Duration
1051-
maxWait time.Duration
1050+
interval time.Duration
1051+
jitter time.Duration
10521052
headless bool
10531053
randSeed int64
10541054

@@ -1067,11 +1067,11 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
10671067
r.InitClient(client),
10681068
),
10691069
Handler: func(inv *clibase.Invocation) error {
1070-
if !(minWait > 0) {
1071-
return xerrors.Errorf("--min-wait must be greater than zero")
1070+
if !(interval > 0) {
1071+
return xerrors.Errorf("--interval must be greater than zero")
10721072
}
1073-
if !(maxWait > minWait) {
1074-
return xerrors.Errorf("--max-wait must be greater than --min-wait")
1073+
if !(jitter < interval) {
1074+
return xerrors.Errorf("--jitter must be less than --interval")
10751075
}
10761076
ctx := inv.Context()
10771077
logger := slog.Make(sloghuman.Sink(inv.Stdout)).Leveled(slog.LevelInfo)
@@ -1124,16 +1124,16 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
11241124
userClient.SetSessionToken(userTokResp.Key)
11251125

11261126
config := dashboard.Config{
1127-
MinWait: minWait,
1128-
MaxWait: maxWait,
1127+
Interval: interval,
1128+
Jitter: jitter,
11291129
Trace: tracingEnabled,
11301130
Logger: logger.Named(name),
11311131
Headless: headless,
11321132
ActionFunc: dashboard.ClickRandomElement,
11331133
RandIntn: rndGen.Intn,
11341134
}
11351135
//nolint:gocritic
1136-
logger.Info(ctx, "runner config", slog.F("min_wait", minWait), slog.F("max_wait", maxWait), slog.F("headless", headless), slog.F("trace", tracingEnabled))
1136+
logger.Info(ctx, "runner config", slog.F("min_wait", interval), slog.F("max_wait", jitter), slog.F("headless", headless), slog.F("trace", tracingEnabled))
11371137
if err := config.Validate(); err != nil {
11381138
return err
11391139
}
@@ -1174,18 +1174,18 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
11741174

11751175
cmd.Options = []clibase.Option{
11761176
{
1177-
Flag: "min-wait",
1178-
Env: "CODER_SCALETEST_DASHBOARD_MIN_WAIT",
1179-
Default: "1s",
1180-
Description: "Minimum wait between fetches.",
1181-
Value: clibase.DurationOf(&minWait),
1177+
Flag: "interval",
1178+
Env: "CODER_SCALETEST_DASHBOARD_INTERVAL",
1179+
Default: "3s",
1180+
Description: "Interval between actions.",
1181+
Value: clibase.DurationOf(&interval),
11821182
},
11831183
{
1184-
Flag: "max-wait",
1185-
Env: "CODER_SCALETEST_DASHBOARD_MAX_WAIT",
1186-
Default: "10s",
1187-
Description: "Maximum wait between fetches.",
1188-
Value: clibase.DurationOf(&maxWait),
1184+
Flag: "jitter",
1185+
Env: "CODER_SCALETEST_DASHBOARD_JITTER",
1186+
Default: "2s",
1187+
Description: "Jitter between actions.",
1188+
Value: clibase.DurationOf(&jitter),
11891189
},
11901190
{
11911191
Flag: "headless",

‎cli/exp_scaletest_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ func TestScaleTestDashboard(t *testing.T) {
104104
_ = coderdtest.CreateFirstUser(t, client)
105105

106106
inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
107-
"--min-wait", "0s",
107+
"--interval", "0s",
108108
)
109109
clitest.SetupConfig(t, client, root)
110110
pty := ptytest.New(t)
111111
inv.Stdout = pty.Output()
112112
inv.Stderr = pty.Output()
113113

114114
err := inv.WithContext(ctx).Run()
115-
require.ErrorContains(t, err, "--min-wait must be greater than zero")
115+
require.ErrorContains(t, err, "--interval must be greater than zero")
116116
})
117117

118118
t.Run("MaxWait", func(t *testing.T) {
@@ -127,16 +127,16 @@ func TestScaleTestDashboard(t *testing.T) {
127127
_ = coderdtest.CreateFirstUser(t, client)
128128

129129
inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
130-
"--min-wait", "1s",
131-
"--max-wait", "1s",
130+
"--interval", "1s",
131+
"--jitter", "1s",
132132
)
133133
clitest.SetupConfig(t, client, root)
134134
pty := ptytest.New(t)
135135
inv.Stdout = pty.Output()
136136
inv.Stderr = pty.Output()
137137

138138
err := inv.WithContext(ctx).Run()
139-
require.ErrorContains(t, err, "--max-wait must be greater than --min-wait")
139+
require.ErrorContains(t, err, "--jitter must be less than --interval")
140140
})
141141

142142
t.Run("OK", func(t *testing.T) {
@@ -151,8 +151,8 @@ func TestScaleTestDashboard(t *testing.T) {
151151
_ = coderdtest.CreateFirstUser(t, client)
152152

153153
inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
154-
"--min-wait", "100ms",
155-
"--max-wait", "1s",
154+
"--interval", "1s",
155+
"--jitter", "500ms",
156156
"--timeout", "5s",
157157
"--scaletest-prometheus-address", "127.0.0.1:0",
158158
"--scaletest-prometheus-wait", "0s",

‎scaletest/dashboard/config.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
)
1111

1212
type Config struct {
13-
// MinWait is the minimum interval between fetches.
14-
MinWait time.Duration `json:"min_wait"`
15-
// MaxWait is the maximum interval between fetches.
16-
MaxWait time.Duration `json:"max_wait"`
13+
// Interval is the minimum interval between fetches.
14+
Interval time.Duration `json:"interval"`
15+
// Jitter is the maximum interval between fetches.
16+
Jitter time.Duration `json:"jitter"`
1717
// Trace is whether to trace the requests.
1818
Trace bool `json:"trace"`
1919
// Logger is the logger to use.
@@ -27,17 +27,21 @@ type Config struct {
2727
}
2828

2929
func (c Config) Validate() error {
30-
if !(c.MinWait > 0) {
31-
return xerrors.Errorf("validate min_wait: must be greater than zero")
30+
if !(c.Interval > 0) {
31+
return xerrors.Errorf("validate interval: must be greater than zero")
3232
}
3333

34-
if !(c.MaxWait > c.MinWait) {
35-
return xerrors.Errorf("validate max_wait: must be greater than min_wait")
34+
if !(c.Jitter < c.Interval) {
35+
return xerrors.Errorf("validate jitter: must be less than interval")
3636
}
3737

3838
if c.ActionFunc == nil {
3939
return xerrors.Errorf("validate action func: must not be nil")
4040
}
4141

42+
if c.RandIntn == nil {
43+
return xerrors.Errorf("validate rand intn: must not be nil")
44+
}
45+
4246
return nil
4347
}

‎scaletest/dashboard/run.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dashboard
33
import (
44
"context"
55
"io"
6-
"math/rand"
76
"time"
87

98
"golang.org/x/xerrors"
@@ -59,7 +58,12 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
5958
case <-cdpCtx.Done():
6059
return nil
6160
case <-t.C:
62-
t.Reset(r.randWait())
61+
var offset time.Duration
62+
if r.cfg.Jitter > 0 {
63+
offset = time.Duration(r.cfg.RandIntn(int(2*r.cfg.Jitter)) - int(r.cfg.Jitter))
64+
}
65+
wait := r.cfg.Interval + offset
66+
t.Reset(wait)
6367
l, act, err := r.cfg.ActionFunc(cdpCtx, r.cfg.RandIntn)
6468
if err != nil {
6569
r.cfg.Logger.Error(ctx, "calling ActionFunc", slog.Error(err))
@@ -84,9 +88,3 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
8488
func (*Runner) Cleanup(_ context.Context, _ string) error {
8589
return nil
8690
}
87-
88-
func (r *Runner) randWait() time.Duration {
89-
//nolint:gosec // This is not for cryptographic purposes. Chill, gosec. Chill.
90-
wait := time.Duration(rand.Intn(int(r.cfg.MaxWait) - int(r.cfg.MinWait)))
91-
return r.cfg.MinWait + wait
92-
}

‎scaletest/dashboard/run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func Test_Run(t *testing.T) {
4747
})
4848
m := &testMetrics{}
4949
cfg := dashboard.Config{
50-
MinWait: 100 * time.Millisecond,
51-
MaxWait: 500 * time.Millisecond,
50+
Interval: 500 * time.Millisecond,
51+
Jitter: 100 * time.Millisecond,
5252
Logger: log,
5353
Headless: true,
5454
ActionFunc: func(_ context.Context, rnd func(int) int) (dashboard.Label, dashboard.Action, error) {

0 commit comments

Comments
 (0)
Failed to load comments.