mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: Improve speedtest by adding direct connection toggle (#3919)
It's weird to test connection speeds over DERP, because most connections will eventually migrate to direct.
This commit is contained in:
+40
-15
@@ -5,20 +5,23 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/cli/cliflag"
|
||||
"github.com/coder/coder/cli/cliui"
|
||||
"github.com/coder/coder/codersdk"
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/xerrors"
|
||||
tsspeedtest "tailscale.com/net/speedtest"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/agent"
|
||||
"github.com/coder/coder/cli/cliflag"
|
||||
"github.com/coder/coder/cli/cliui"
|
||||
"github.com/coder/coder/codersdk"
|
||||
)
|
||||
|
||||
func speedtest() *cobra.Command {
|
||||
var (
|
||||
reverse bool
|
||||
timeStr string
|
||||
direct bool
|
||||
duration time.Duration
|
||||
reverse bool
|
||||
)
|
||||
cmd := &cobra.Command{
|
||||
Annotations: workspaceCommand,
|
||||
@@ -28,11 +31,6 @@ func speedtest() *cobra.Command {
|
||||
ctx, cancel := context.WithCancel(cmd.Context())
|
||||
defer cancel()
|
||||
|
||||
dur, err := time.ParseDuration(timeStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client, err := CreateClient(cmd)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("create codersdk client: %w", err)
|
||||
@@ -57,13 +55,38 @@ func speedtest() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
_, _ = conn.Ping()
|
||||
if direct {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-ticker.C:
|
||||
}
|
||||
dur, err := conn.Ping()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
tc, _ := conn.(*agent.TailnetConn)
|
||||
status := tc.Status()
|
||||
if len(status.Peers()) != 1 {
|
||||
continue
|
||||
}
|
||||
peer := status.Peer[status.Peers()[0]]
|
||||
if peer.CurAddr == "" {
|
||||
cmd.Printf("Waiting for a direct connection... (%dms via %s)\n", dur.Milliseconds(), peer.Relay)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
dir := tsspeedtest.Download
|
||||
if reverse {
|
||||
dir = tsspeedtest.Upload
|
||||
}
|
||||
cmd.Printf("Starting a %ds %s test...\n", int(dur.Seconds()), dir)
|
||||
results, err := conn.Speedtest(dir, dur)
|
||||
cmd.Printf("Starting a %ds %s test...\n", int(duration.Seconds()), dir)
|
||||
results, err := conn.Speedtest(dir, duration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -83,9 +106,11 @@ func speedtest() *cobra.Command {
|
||||
return err
|
||||
},
|
||||
}
|
||||
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(), &timeStr, "time", "t", "", "5s",
|
||||
cmd.Flags().DurationVarP(&duration, "time", "t", tsspeedtest.DefaultDuration,
|
||||
"Specifies the duration to monitor traffic.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
"github.com/coder/coder/agent"
|
||||
"github.com/coder/coder/cli/clitest"
|
||||
@@ -11,7 +13,6 @@ import (
|
||||
"github.com/coder/coder/codersdk"
|
||||
"github.com/coder/coder/pty/ptytest"
|
||||
"github.com/coder/coder/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSpeedtest(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user