Skip to content

feat(cli/exp): add target workspace/users to scaletest commands #11701

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 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
extend to dashboard traffic (users)
  • Loading branch information
mafredri committed Jan 19, 2024
commit fc5869ac75f365bb27ea1ce8b0cded981ad455b9
44 changes: 31 additions & 13 deletions cli/exp_scaletest.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
return xerrors.Errorf("parse template: %w", err)
}
}
targetWorkspaceStart, targetWorkspaceEnd, err := parseTargetWorkspaces(targetWorkspaces)
targetWorkspaceStart, targetWorkspaceEnd, err := parseTargetRange("workspaces", targetWorkspaces)
if err != nil {
return xerrors.Errorf("parse target workspaces: %w", err)
}
Expand Down Expand Up @@ -1102,10 +1102,11 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {

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

client = &codersdk.Client{}
tracingFlags = &scaletestTracingFlags{}
Expand All @@ -1128,6 +1129,10 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
if !(jitter < interval) {
return xerrors.Errorf("--jitter must be less than --interval")
}
targetUserStart, targetUserEnd, err := parseTargetRange("users", targetUsers)
if err != nil {
return xerrors.Errorf("parse target users: %w", err)
}
ctx := inv.Context()
logger := inv.Logger.AppendSinks(sloghuman.Sink(inv.Stdout))
if r.verbose {
Expand Down Expand Up @@ -1164,8 +1169,15 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
if err != nil {
return xerrors.Errorf("get scaletest users")
}
if targetUserEnd == 0 {
targetUserEnd = len(users)
}

for idx, usr := range users {
if idx < targetUserStart || idx >= targetUserEnd {
continue
}

for _, usr := range users {
//nolint:gosec // not used for cryptographic purposes
rndGen := rand.New(rand.NewSource(randSeed))
name := fmt.Sprintf("dashboard-%s", usr.Username)
Expand Down Expand Up @@ -1236,6 +1248,12 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
}

cmd.Options = []clibase.Option{
{
Flag: "target-users",
Env: "CODER_SCALETEST_DASHBOARD_TARGET_USERS",
Description: "Target a specific range of users in the format [START]:[END] (exclusive). Example: 0:10 will target the 10 first alphabetically sorted users (0-9).",
Value: clibase.StringOf(&targetUsers),
},
{
Flag: "interval",
Env: "CODER_SCALETEST_DASHBOARD_INTERVAL",
Expand Down Expand Up @@ -1452,28 +1470,28 @@ func parseTemplate(ctx context.Context, client *codersdk.Client, organizationIDs
return tpl, nil
}

func parseTargetWorkspaces(targetWorkspaces string) (start, end int, err error) {
if targetWorkspaces == "" {
func parseTargetRange(name, targets string) (start, end int, err error) {
if targets == "" {
return 0, 0, nil
}

parts := strings.Split(targetWorkspaces, ":")
parts := strings.Split(targets, ":")
if len(parts) != 2 {
return 0, 0, xerrors.Errorf("invalid target workspaces %q", targetWorkspaces)
return 0, 0, xerrors.Errorf("invalid target %s %q", name, targets)
}

start, err = strconv.Atoi(parts[0])
if err != nil {
return 0, 0, xerrors.Errorf("invalid target workspaces %q: %w", targetWorkspaces, err)
return 0, 0, xerrors.Errorf("invalid target %s %q: %w", name, targets, err)
}

end, err = strconv.Atoi(parts[1])
if err != nil {
return 0, 0, xerrors.Errorf("invalid target workspaces %q: %w", targetWorkspaces, err)
return 0, 0, xerrors.Errorf("invalid target %s %q: %w", name, targets, err)
}

if start == end {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check also end < start?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍🏻

return 0, 0, xerrors.Errorf("invalid target workspaces %q: start and end cannot be equal", targetWorkspaces)
return 0, 0, xerrors.Errorf("invalid target %s %q: start and end cannot be equal", name, targets)
}

return start, end, nil
Expand Down
23 changes: 23 additions & 0 deletions cli/exp_scaletest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,27 @@ func TestScaleTestDashboard(t *testing.T) {
err := inv.WithContext(ctx).Run()
require.NoError(t, err, "")
})

t.Run("TargetUsers", func(t *testing.T) {
t.Parallel()
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancelFunc()

log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
client := coderdtest.New(t, &coderdtest.Options{
Logger: &log,
})
_ = coderdtest.CreateFirstUser(t, client)

inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
"--target-users", "0:0",
)
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, "invalid target users \"0:0\": start and end cannot be equal")
})
}