mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
feat(cli): make minor improvements to speedtest (#6266)
- 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
This commit is contained in:
@ -19,9 +19,9 @@ import (
|
|||||||
|
|
||||||
func speedtest() *cobra.Command {
|
func speedtest() *cobra.Command {
|
||||||
var (
|
var (
|
||||||
direct bool
|
direct bool
|
||||||
duration time.Duration
|
duration time.Duration
|
||||||
reverse bool
|
direction string
|
||||||
)
|
)
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Annotations: workspaceCommand,
|
Annotations: workspaceCommand,
|
||||||
@ -48,7 +48,7 @@ func speedtest() *cobra.Command {
|
|||||||
return client.WorkspaceAgent(ctx, workspaceAgent.ID)
|
return client.WorkspaceAgent(ctx, workspaceAgent.ID)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil && !xerrors.Is(err, cliui.AgentStartError) {
|
||||||
return xerrors.Errorf("await agent: %w", err)
|
return xerrors.Errorf("await agent: %w", err)
|
||||||
}
|
}
|
||||||
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
|
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
|
||||||
@ -94,17 +94,22 @@ func speedtest() *cobra.Command {
|
|||||||
} else {
|
} else {
|
||||||
conn.AwaitReachable(ctx)
|
conn.AwaitReachable(ctx)
|
||||||
}
|
}
|
||||||
dir := tsspeedtest.Download
|
var tsDir tsspeedtest.Direction
|
||||||
if reverse {
|
switch direction {
|
||||||
dir = tsspeedtest.Upload
|
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)
|
cmd.Printf("Starting a %ds %s test...\n", int(duration.Seconds()), tsDir)
|
||||||
results, err := conn.Speedtest(ctx, dir, duration)
|
results, err := conn.Speedtest(ctx, tsDir, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tableWriter := cliui.Table()
|
tableWriter := cliui.Table()
|
||||||
tableWriter.AppendHeader(table.Row{"Interval", "Transfer", "Bandwidth"})
|
tableWriter.AppendHeader(table.Row{"Interval", "Throughput"})
|
||||||
startTime := results[0].IntervalStart
|
startTime := results[0].IntervalStart
|
||||||
for _, r := range results {
|
for _, r := range results {
|
||||||
if r.Total {
|
if r.Total {
|
||||||
@ -112,7 +117,6 @@ func speedtest() *cobra.Command {
|
|||||||
}
|
}
|
||||||
tableWriter.AppendRow(table.Row{
|
tableWriter.AppendRow(table.Row{
|
||||||
fmt.Sprintf("%.2f-%.2f sec", r.IntervalStart.Sub(startTime).Seconds(), r.IntervalEnd.Sub(startTime).Seconds()),
|
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()),
|
fmt.Sprintf("%.4f Mbits/sec", r.MBitsPerSecond()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -122,8 +126,9 @@ func speedtest() *cobra.Command {
|
|||||||
}
|
}
|
||||||
cliflag.BoolVarP(cmd.Flags(), &direct, "direct", "d", "", false,
|
cliflag.BoolVarP(cmd.Flags(), &direct, "direct", "d", "", false,
|
||||||
"Specifies whether to wait for a direct connection before testing speed.")
|
"Specifies whether to wait for a direct connection before testing speed.")
|
||||||
cliflag.BoolVarP(cmd.Flags(), &reverse, "reverse", "r", "", false,
|
cliflag.StringVarP(cmd.Flags(), &direction, "direction", "", "", "down",
|
||||||
"Specifies whether to run in reverse mode where the client receives and the server sends.")
|
"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,
|
cmd.Flags().DurationVarP(&duration, "time", "t", tsspeedtest.DefaultDuration,
|
||||||
"Specifies the duration to monitor traffic.")
|
"Specifies the duration to monitor traffic.")
|
||||||
return cmd
|
return cmd
|
||||||
|
10
cli/testdata/coder_speedtest_--help.golden
vendored
10
cli/testdata/coder_speedtest_--help.golden
vendored
@ -4,11 +4,11 @@ Usage:
|
|||||||
coder speedtest <workspace> [flags]
|
coder speedtest <workspace> [flags]
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
-d, --direct Specifies whether to wait for a direct connection before testing speed.
|
-d, --direct Specifies whether to wait for a direct connection before testing speed.
|
||||||
-h, --help help for speedtest
|
--direction string Specifies whether to run in reverse mode where the client receives
|
||||||
-r, --reverse Specifies whether to run in reverse mode where the client receives and
|
and the server sends. (up|down) (default "down")
|
||||||
the server sends.
|
-h, --help help for speedtest
|
||||||
-t, --time duration Specifies the duration to monitor traffic. (default 5s)
|
-t, --time duration Specifies the duration to monitor traffic. (default 5s)
|
||||||
|
|
||||||
Global Flags:
|
Global Flags:
|
||||||
--global-config coder Path to the global coder config directory.
|
--global-config coder Path to the global coder config directory.
|
||||||
|
@ -9,10 +9,10 @@ coder speedtest <workspace> [flags]
|
|||||||
### Options
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
-d, --direct Specifies whether to wait for a direct connection before testing speed.
|
-d, --direct Specifies whether to wait for a direct connection before testing speed.
|
||||||
-h, --help help for speedtest
|
--direction string Specifies whether to run in reverse mode where the client receives and the server sends. (up|down) (default "down")
|
||||||
-r, --reverse Specifies whether to run in reverse mode where the client receives and the server sends.
|
-h, --help help for speedtest
|
||||||
-t, --time duration Specifies the duration to monitor traffic. (default 5s)
|
-t, --time duration Specifies the duration to monitor traffic. (default 5s)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Options inherited from parent commands
|
### Options inherited from parent commands
|
||||||
|
Reference in New Issue
Block a user