Skip to content

Commit

Permalink
diagnostics: add packet loss and cacheServerList for speedtest (#10259)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3inbowari authored May 12, 2024
1 parent e2c5661 commit 3e54eaf
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions erigon-lib/diagnostics/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ type NetworkSpeedTestResult struct {
Latency time.Duration `json:"latency"`
DownloadSpeed float64 `json:"downloadSpeed"`
UploadSpeed float64 `json:"uploadSpeed"`
PacketLoss float64 `json:"packetLoss"`
}

func (ti FileDownloadedStatisticsUpdate) Type() Type {
Expand Down
28 changes: 23 additions & 5 deletions erigon-lib/diagnostics/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/showwin/speedtest-go/speedtest"
"github.com/showwin/speedtest-go/speedtest/transport"
)

func (d *DiagnosticClient) setupSpeedtestDiagnostics(rootCtx context.Context) {
Expand All @@ -28,37 +29,54 @@ func (d *DiagnosticClient) setupSpeedtestDiagnostics(rootCtx context.Context) {
}()
}

var cacheServerList speedtest.Servers

func (d *DiagnosticClient) runSpeedTest(rootCtx context.Context) NetworkSpeedTestResult {
var speedtestClient = speedtest.New()
serverList, _ := speedtestClient.FetchServers()
targets, _ := serverList.FindServer([]int{})

serverList, err := speedtestClient.FetchServers()
// Ensure that the server list can rolled back to the previous cache.
if err == nil {
cacheServerList = serverList
}
targets, _ := cacheServerList.FindServer([]int{})

latency := time.Duration(0)
downloadSpeed := float64(0)
uploadSpeed := float64(0)
packetLoss := float64(-1)

analyzer := speedtest.NewPacketLossAnalyzer(nil)

if len(targets) > 0 {
s := targets[0]
err := s.PingTestContext(rootCtx, nil)
err = s.PingTestContext(rootCtx, nil)
if err == nil {
latency = s.Latency
}

err = s.DownloadTestContext(rootCtx)
if err == nil {
downloadSpeed = s.DLSpeed
downloadSpeed = s.DLSpeed.Mbps()
}

err = s.UploadTestContext(rootCtx)
if err == nil {
uploadSpeed = s.ULSpeed
uploadSpeed = s.ULSpeed.Mbps()
}

ctx, cancel := context.WithTimeout(rootCtx, time.Second*15)
defer cancel()
_ = analyzer.RunWithContext(ctx, s.Host, func(pl *transport.PLoss) {
packetLoss = pl.Loss()
})
}

return NetworkSpeedTestResult{
Latency: latency,
DownloadSpeed: downloadSpeed,
UploadSpeed: uploadSpeed,
PacketLoss: packetLoss,
}
}

Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ require (
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/showwin/speedtest-go v1.6.12
github.com/showwin/speedtest-go v1.7.5
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFt
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/showwin/speedtest-go v1.6.12 h1:q+hWNn2cM35KkqtXGGbSmuJgd67gTP8+VlneY2hq9vU=
github.com/showwin/speedtest-go v1.6.12/go.mod h1:uLgdWCNarXxlYsL2E5TOZpCIwpgSWnEANZp7gfHXHu0=
github.com/showwin/speedtest-go v1.7.5 h1:FQ3EdM2vnfw5BRCRzGCYe8aWu70rr21Az5ZFHiW9CdE=
github.com/showwin/speedtest-go v1.7.5/go.mod h1:uLgdWCNarXxlYsL2E5TOZpCIwpgSWnEANZp7gfHXHu0=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ require (
github.com/shirou/gopsutil/v3 v3.24.3 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/showwin/speedtest-go v1.6.12 // indirect
github.com/showwin/speedtest-go v1.7.5 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sosodev/duration v1.1.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/showwin/speedtest-go v1.6.12 h1:q+hWNn2cM35KkqtXGGbSmuJgd67gTP8+VlneY2hq9vU=
github.com/showwin/speedtest-go v1.6.12/go.mod h1:uLgdWCNarXxlYsL2E5TOZpCIwpgSWnEANZp7gfHXHu0=
github.com/showwin/speedtest-go v1.7.5 h1:FQ3EdM2vnfw5BRCRzGCYe8aWu70rr21Az5ZFHiW9CdE=
github.com/showwin/speedtest-go v1.7.5/go.mod h1:uLgdWCNarXxlYsL2E5TOZpCIwpgSWnEANZp7gfHXHu0=
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
github.com/shurcooL/github_flavored_markdown v0.0.0-20181002035957-2122de532470/go.mod h1:2dOwnU2uBioM+SGy2aZoq1f/Sd1l9OkAeAUvjSyvgU0=
Expand Down

0 comments on commit 3e54eaf

Please sign in to comment.