Skip to content

Commit 0e90b2c

Browse files
committed
feat(cli/exp): add --target-workspaces to scaletest workspace-traffic command
1 parent 200a87e commit 0e90b2c

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

cli/exp_scaletest.go

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -857,11 +857,12 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd {
857857

858858
func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
859859
var (
860-
tickInterval time.Duration
861-
bytesPerTick int64
862-
ssh bool
863-
app string
864-
template string
860+
tickInterval time.Duration
861+
bytesPerTick int64
862+
ssh bool
863+
app string
864+
template string
865+
targetWorkspaces string
865866

866867
client = &codersdk.Client{}
867868
tracingFlags = &scaletestTracingFlags{}
@@ -912,6 +913,10 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
912913
return xerrors.Errorf("parse template: %w", err)
913914
}
914915
}
916+
targetWorkspaceStart, targetWorkspaceEnd, err := parseTargetWorkspaces(targetWorkspaces)
917+
if err != nil {
918+
return xerrors.Errorf("parse target workspaces: %w", err)
919+
}
915920

916921
appHost, err := client.AppHost(ctx)
917922
if err != nil {
@@ -923,9 +928,16 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
923928
return err
924929
}
925930

931+
if targetWorkspaceEnd == 0 {
932+
targetWorkspaceEnd = len(workspaces)
933+
}
934+
926935
if len(workspaces) == 0 {
927936
return xerrors.Errorf("no scaletest workspaces exist")
928937
}
938+
if targetWorkspaceEnd > len(workspaces) {
939+
return xerrors.Errorf("target workspace end %d is greater than the number of workspaces %d", targetWorkspaceEnd, len(workspaces))
940+
}
929941

930942
tracerProvider, closeTracing, tracingEnabled, err := tracingFlags.provider(ctx)
931943
if err != nil {
@@ -951,6 +963,10 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
951963

952964
th := harness.NewTestHarness(strategy.toStrategy(), cleanupStrategy.toStrategy())
953965
for idx, ws := range workspaces {
966+
if idx < targetWorkspaceStart || idx >= targetWorkspaceEnd {
967+
continue
968+
}
969+
954970
var (
955971
agent codersdk.WorkspaceAgent
956972
name = "workspace-traffic"
@@ -1039,6 +1055,12 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
10391055
Description: "Name or ID of the template. Traffic generation will be limited to workspaces created from this template.",
10401056
Value: clibase.StringOf(&template),
10411057
},
1058+
{
1059+
Flag: "target-workspaces",
1060+
Env: "CODER_SCALETEST_TARGET_WORKSPACES",
1061+
Description: "Target a specific range of workspaces in the format [START]:[END] (exclusive). Example: 0:10 will target workspaces the 10 first alphabetically sorted workspaces (0-9).",
1062+
Value: clibase.StringOf(&targetWorkspaces),
1063+
},
10421064
{
10431065
Flag: "bytes-per-tick",
10441066
Env: "CODER_SCALETEST_WORKSPACE_TRAFFIC_BYTES_PER_TICK",
@@ -1430,6 +1452,33 @@ func parseTemplate(ctx context.Context, client *codersdk.Client, organizationIDs
14301452
return tpl, nil
14311453
}
14321454

1455+
func parseTargetWorkspaces(targetWorkspaces string) (start, end int, err error) {
1456+
if targetWorkspaces == "" {
1457+
return 0, 0, nil
1458+
}
1459+
1460+
parts := strings.Split(targetWorkspaces, ":")
1461+
if len(parts) != 2 {
1462+
return 0, 0, xerrors.Errorf("invalid target workspaces %q", targetWorkspaces)
1463+
}
1464+
1465+
start, err = strconv.Atoi(parts[0])
1466+
if err != nil {
1467+
return 0, 0, xerrors.Errorf("invalid target workspaces %q: %w", targetWorkspaces, err)
1468+
}
1469+
1470+
end, err = strconv.Atoi(parts[1])
1471+
if err != nil {
1472+
return 0, 0, xerrors.Errorf("invalid target workspaces %q: %w", targetWorkspaces, err)
1473+
}
1474+
1475+
if start == end {
1476+
return 0, 0, xerrors.Errorf("invalid target workspaces %q: start and end cannot be equal", targetWorkspaces)
1477+
}
1478+
1479+
return start, end, nil
1480+
}
1481+
14331482
func createWorkspaceAppConfig(client *codersdk.Client, appHost, app string, workspace codersdk.Workspace, agent codersdk.WorkspaceAgent) (workspacetraffic.AppConfig, error) {
14341483
if app == "" {
14351484
return workspacetraffic.AppConfig{}, nil

cli/exp_scaletest_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ func TestScaleTestWorkspaceTraffic_Template(t *testing.T) {
116116
require.ErrorContains(t, err, "could not find template \"doesnotexist\" in any organization")
117117
}
118118

119+
// This test just validates that the CLI command accepts its known arguments.
120+
func TestScaleTestWorkspaceTraffic_TargetWorkspaces(t *testing.T) {
121+
t.Parallel()
122+
123+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitMedium)
124+
defer cancelFunc()
125+
126+
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
127+
client := coderdtest.New(t, &coderdtest.Options{
128+
Logger: &log,
129+
})
130+
_ = coderdtest.CreateFirstUser(t, client)
131+
132+
inv, root := clitest.New(t, "exp", "scaletest", "workspace-traffic",
133+
"--target-workspaces", "0:0",
134+
)
135+
clitest.SetupConfig(t, client, root)
136+
pty := ptytest.New(t)
137+
inv.Stdout = pty.Output()
138+
inv.Stderr = pty.Output()
139+
140+
err := inv.WithContext(ctx).Run()
141+
require.ErrorContains(t, err, "invalid target workspaces \"0:0\": start and end cannot be equal")
142+
}
143+
119144
// This test just validates that the CLI command accepts its known arguments.
120145
func TestScaleTestCleanup_Template(t *testing.T) {
121146
t.Parallel()

0 commit comments

Comments
 (0)