Skip to content

Commit 787e2cd

Browse files
committed
feat(cli): make minor improvements to speedtest
- Remove mostly redundant "Transferred" column - Rename "Bandwidth" to "Throughput" - Replace "--reverse" (which has an ambiguous starting state) with "--direction=(up|down)" - Tolerate AgentStartErrors which may be caused by failing startup script
1 parent 19ae411 commit 787e2cd

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

cli/speedtest.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919

2020
func speedtest() *cobra.Command {
2121
var (
22-
direct bool
23-
duration time.Duration
24-
reverse bool
22+
direct bool
23+
duration time.Duration
24+
direction string
2525
)
2626
cmd := &cobra.Command{
2727
Annotations: workspaceCommand,
@@ -48,7 +48,7 @@ func speedtest() *cobra.Command {
4848
return client.WorkspaceAgent(ctx, workspaceAgent.ID)
4949
},
5050
})
51-
if err != nil {
51+
if err != nil && !xerrors.Is(err, cliui.AgentStartError) {
5252
return xerrors.Errorf("await agent: %w", err)
5353
}
5454
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
@@ -94,25 +94,29 @@ func speedtest() *cobra.Command {
9494
} else {
9595
conn.AwaitReachable(ctx)
9696
}
97-
dir := tsspeedtest.Download
98-
if reverse {
99-
dir = tsspeedtest.Upload
97+
var tsDir tsspeedtest.Direction
98+
switch direction {
99+
case "up":
100+
tsDir = tsspeedtest.Upload
101+
case "down":
102+
tsDir = tsspeedtest.Download
103+
default:
104+
return xerrors.Errorf("invalid direction: %q", direction)
100105
}
101-
cmd.Printf("Starting a %ds %s test...\n", int(duration.Seconds()), dir)
102-
results, err := conn.Speedtest(ctx, dir, duration)
106+
cmd.Printf("Starting a %ds %s test...\n", int(duration.Seconds()), tsDir)
107+
results, err := conn.Speedtest(ctx, tsDir, duration)
103108
if err != nil {
104109
return err
105110
}
106111
tableWriter := cliui.Table()
107-
tableWriter.AppendHeader(table.Row{"Interval", "Transfer", "Bandwidth"})
112+
tableWriter.AppendHeader(table.Row{"Interval", "Throughput"})
108113
startTime := results[0].IntervalStart
109114
for _, r := range results {
110115
if r.Total {
111116
tableWriter.AppendSeparator()
112117
}
113118
tableWriter.AppendRow(table.Row{
114119
fmt.Sprintf("%.2f-%.2f sec", r.IntervalStart.Sub(startTime).Seconds(), r.IntervalEnd.Sub(startTime).Seconds()),
115-
fmt.Sprintf("%.4f MBits", r.MegaBits()),
116120
fmt.Sprintf("%.4f Mbits/sec", r.MBitsPerSecond()),
117121
})
118122
}
@@ -122,8 +126,9 @@ func speedtest() *cobra.Command {
122126
}
123127
cliflag.BoolVarP(cmd.Flags(), &direct, "direct", "d", "", false,
124128
"Specifies whether to wait for a direct connection before testing speed.")
125-
cliflag.BoolVarP(cmd.Flags(), &reverse, "reverse", "r", "", false,
126-
"Specifies whether to run in reverse mode where the client receives and the server sends.")
129+
cliflag.StringVarP(cmd.Flags(), &direction, "direction", "", "", "down",
130+
"Specifies whether to run in reverse mode where the client receives and the server sends. (up|down)",
131+
)
127132
cmd.Flags().DurationVarP(&duration, "time", "t", tsspeedtest.DefaultDuration,
128133
"Specifies the duration to monitor traffic.")
129134
return cmd

cli/testdata/coder_speedtest_--help.golden

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Usage:
44
coder speedtest <workspace> [flags]
55

66
Flags:
7-
-d, --direct Specifies whether to wait for a direct connection before testing speed.
8-
-h, --help help for speedtest
9-
-r, --reverse Specifies whether to run in reverse mode where the client receives and
10-
the server sends.
11-
-t, --time duration Specifies the duration to monitor traffic. (default 5s)
7+
-d, --direct Specifies whether to wait for a direct connection before testing speed.
8+
--direction string Specifies whether to run in reverse mode where the client receives
9+
and the server sends. (up|down) (default "down")
10+
-h, --help help for speedtest
11+
-t, --time duration Specifies the duration to monitor traffic. (default 5s)
1212

1313
Global Flags:
1414
--global-config coder Path to the global coder config directory.

0 commit comments

Comments
 (0)