Skip to content

feat(scaletest/dashboard): integrate chromedp #9927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 2, 2023
Merged
Prev Previous commit
Next Next commit
replace --min-wait and --max-wait with --interval and --jitter
  • Loading branch information
johnstcn committed Sep 29, 2023
commit 0805f2ee03318db474d7a06e8d17290f04a04078
38 changes: 19 additions & 19 deletions cli/exp_scaletest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,8 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {

func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
var (
minWait time.Duration
maxWait time.Duration
interval time.Duration
jitter time.Duration
headless bool
randSeed int64

Expand All @@ -1067,11 +1067,11 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
if !(minWait > 0) {
return xerrors.Errorf("--min-wait must be greater than zero")
if !(interval > 0) {
return xerrors.Errorf("--interval must be greater than zero")
}
if !(maxWait > minWait) {
return xerrors.Errorf("--max-wait must be greater than --min-wait")
if !(jitter < interval) {
return xerrors.Errorf("--jitter must be less than --interval")
}
ctx := inv.Context()
logger := slog.Make(sloghuman.Sink(inv.Stdout)).Leveled(slog.LevelInfo)
Expand Down Expand Up @@ -1124,16 +1124,16 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
userClient.SetSessionToken(userTokResp.Key)

config := dashboard.Config{
MinWait: minWait,
MaxWait: maxWait,
Interval: interval,
Jitter: jitter,
Trace: tracingEnabled,
Logger: logger.Named(name),
Headless: headless,
ActionFunc: dashboard.ClickRandomElement,
RandIntn: rndGen.Intn,
}
//nolint:gocritic
logger.Info(ctx, "runner config", slog.F("min_wait", minWait), slog.F("max_wait", maxWait), slog.F("headless", headless), slog.F("trace", tracingEnabled))
logger.Info(ctx, "runner config", slog.F("min_wait", interval), slog.F("max_wait", jitter), slog.F("headless", headless), slog.F("trace", tracingEnabled))
if err := config.Validate(); err != nil {
return err
}
Expand Down Expand Up @@ -1174,18 +1174,18 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {

cmd.Options = []clibase.Option{
{
Flag: "min-wait",
Env: "CODER_SCALETEST_DASHBOARD_MIN_WAIT",
Default: "1s",
Description: "Minimum wait between fetches.",
Value: clibase.DurationOf(&minWait),
Flag: "interval",
Env: "CODER_SCALETEST_DASHBOARD_INTERVAL",
Default: "3s",
Description: "Interval between actions.",
Value: clibase.DurationOf(&interval),
},
{
Flag: "max-wait",
Env: "CODER_SCALETEST_DASHBOARD_MAX_WAIT",
Default: "10s",
Description: "Maximum wait between fetches.",
Value: clibase.DurationOf(&maxWait),
Flag: "jitter",
Env: "CODER_SCALETEST_DASHBOARD_JITTER",
Default: "2s",
Description: "Jitter between actions.",
Value: clibase.DurationOf(&jitter),
},
{
Flag: "headless",
Expand Down
14 changes: 7 additions & 7 deletions cli/exp_scaletest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ func TestScaleTestDashboard(t *testing.T) {
_ = coderdtest.CreateFirstUser(t, client)

inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
"--min-wait", "0s",
"--interval", "0s",
)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
inv.Stdout = pty.Output()
inv.Stderr = pty.Output()

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

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

inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
"--min-wait", "1s",
"--max-wait", "1s",
"--interval", "1s",
"--jitter", "1s",
)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
inv.Stdout = pty.Output()
inv.Stderr = pty.Output()

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

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

inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
"--min-wait", "100ms",
"--max-wait", "1s",
"--interval", "1s",
"--jitter", "500ms",
"--timeout", "5s",
"--scaletest-prometheus-address", "127.0.0.1:0",
"--scaletest-prometheus-wait", "0s",
Expand Down
20 changes: 12 additions & 8 deletions scaletest/dashboard/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

type Config struct {
// MinWait is the minimum interval between fetches.
MinWait time.Duration `json:"min_wait"`
// MaxWait is the maximum interval between fetches.
MaxWait time.Duration `json:"max_wait"`
// Interval is the minimum interval between fetches.
Interval time.Duration `json:"interval"`
// Jitter is the maximum interval between fetches.
Jitter time.Duration `json:"jitter"`
// Trace is whether to trace the requests.
Trace bool `json:"trace"`
// Logger is the logger to use.
Expand All @@ -27,17 +27,21 @@ type Config struct {
}

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

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

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

if c.RandIntn == nil {
return xerrors.Errorf("validate rand intn: must not be nil")
}

return nil
}
14 changes: 6 additions & 8 deletions scaletest/dashboard/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dashboard
import (
"context"
"io"
"math/rand"
"time"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -59,7 +58,12 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
case <-cdpCtx.Done():
return nil
case <-t.C:
t.Reset(r.randWait())
var offset time.Duration
if r.cfg.Jitter > 0 {
offset = time.Duration(r.cfg.RandIntn(int(2*r.cfg.Jitter)) - int(r.cfg.Jitter))
}
wait := r.cfg.Interval + offset
t.Reset(wait)
l, act, err := r.cfg.ActionFunc(cdpCtx, r.cfg.RandIntn)
if err != nil {
r.cfg.Logger.Error(ctx, "calling ActionFunc", slog.Error(err))
Expand All @@ -84,9 +88,3 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
func (*Runner) Cleanup(_ context.Context, _ string) error {
return nil
}

func (r *Runner) randWait() time.Duration {
//nolint:gosec // This is not for cryptographic purposes. Chill, gosec. Chill.
wait := time.Duration(rand.Intn(int(r.cfg.MaxWait) - int(r.cfg.MinWait)))
return r.cfg.MinWait + wait
}
4 changes: 2 additions & 2 deletions scaletest/dashboard/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func Test_Run(t *testing.T) {
})
m := &testMetrics{}
cfg := dashboard.Config{
MinWait: 100 * time.Millisecond,
MaxWait: 500 * time.Millisecond,
Interval: 500 * time.Millisecond,
Jitter: 100 * time.Millisecond,
Logger: log,
Headless: true,
ActionFunc: func(_ context.Context, rnd func(int) int) (dashboard.Label, dashboard.Action, error) {
Expand Down