Skip to content

feat(cli): make minor improvements to speedtest #6266

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 6 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 18 additions & 13 deletions cli/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

func speedtest() *cobra.Command {
var (
direct bool
duration time.Duration
reverse bool
direct bool
duration time.Duration
direction string
)
cmd := &cobra.Command{
Annotations: workspaceCommand,
Expand All @@ -48,7 +48,7 @@ func speedtest() *cobra.Command {
return client.WorkspaceAgent(ctx, workspaceAgent.ID)
},
})
if err != nil {
if err != nil && !xerrors.Is(err, cliui.AgentStartError) {
return xerrors.Errorf("await agent: %w", err)
}
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
Expand Down Expand Up @@ -94,25 +94,29 @@ func speedtest() *cobra.Command {
} else {
conn.AwaitReachable(ctx)
}
dir := tsspeedtest.Download
if reverse {
dir = tsspeedtest.Upload
var tsDir tsspeedtest.Direction
switch direction {
case "up":
tsDir = tsspeedtest.Upload
case "down":
tsDir = tsspeedtest.Download
default:
return xerrors.Errorf("invalid direction: %q", direction)
}
cmd.Printf("Starting a %ds %s test...\n", int(duration.Seconds()), dir)
results, err := conn.Speedtest(ctx, dir, duration)
cmd.Printf("Starting a %ds %s test...\n", int(duration.Seconds()), tsDir)
results, err := conn.Speedtest(ctx, tsDir, duration)
if err != nil {
return err
}
tableWriter := cliui.Table()
tableWriter.AppendHeader(table.Row{"Interval", "Transfer", "Bandwidth"})
tableWriter.AppendHeader(table.Row{"Interval", "Throughput"})
startTime := results[0].IntervalStart
for _, r := range results {
if r.Total {
tableWriter.AppendSeparator()
}
tableWriter.AppendRow(table.Row{
fmt.Sprintf("%.2f-%.2f sec", r.IntervalStart.Sub(startTime).Seconds(), r.IntervalEnd.Sub(startTime).Seconds()),
fmt.Sprintf("%.4f MBits", r.MegaBits()),
fmt.Sprintf("%.4f Mbits/sec", r.MBitsPerSecond()),
})
}
Expand All @@ -122,8 +126,9 @@ func speedtest() *cobra.Command {
}
cliflag.BoolVarP(cmd.Flags(), &direct, "direct", "d", "", false,
"Specifies whether to wait for a direct connection before testing speed.")
cliflag.BoolVarP(cmd.Flags(), &reverse, "reverse", "r", "", false,
"Specifies whether to run in reverse mode where the client receives and the server sends.")
cliflag.StringVarP(cmd.Flags(), &direction, "direction", "", "", "down",
"Specifies whether to run in reverse mode where the client receives and the server sends. (up|down)",
)
cmd.Flags().DurationVarP(&duration, "time", "t", tsspeedtest.DefaultDuration,
"Specifies the duration to monitor traffic.")
return cmd
Expand Down
10 changes: 5 additions & 5 deletions cli/testdata/coder_speedtest_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Usage:
coder speedtest <workspace> [flags]

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

Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
8 changes: 4 additions & 4 deletions docs/cli/coder_speedtest.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ coder speedtest <workspace> [flags]
### Options

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

### Options inherited from parent commands
Expand Down