From 202fb08ae11204eea3658c6e75fe0826b617aa2f Mon Sep 17 00:00:00 2001 From: r3inbowari Date: Thu, 9 May 2024 16:04:53 +0800 Subject: [PATCH] fix: packet loss calc method (#206) * add(api): add Hosts for servers * fix(api): calc packet loss with mixed PLoss * chore(cli): lint error --- example/packet_loss/main.go | 4 +--- speedtest.go | 2 +- speedtest/loss.go | 15 ++++++++------- speedtest/server.go | 9 +++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/example/packet_loss/main.go b/example/packet_loss/main.go index c6fae4a..c07ed92 100644 --- a/example/packet_loss/main.go +++ b/example/packet_loss/main.go @@ -26,9 +26,7 @@ func main() { wg := &sync.WaitGroup{} // 3. Perform packet loss analysis on all available servers - var hosts []string for _, server := range *targets { - hosts = append(hosts, server.Host) wg.Add(1) //ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) //go func(server *speedtest.Server, analyzer *speedtest.PacketLossAnalyzer, ctx context.Context, cancel context.CancelFunc) { @@ -52,7 +50,7 @@ func main() { wg.Wait() // use mixed PacketLoss - mixed, err := analyzer.RunMulti(hosts) + mixed, err := analyzer.RunMulti(serverList.Hosts()) checkError(err) fmt.Printf("Mixed packets lossed: %.2f\n", mixed) } diff --git a/speedtest.go b/speedtest.go index 78e7790..635b571 100644 --- a/speedtest.go +++ b/speedtest.go @@ -136,7 +136,7 @@ func main() { }) // 3.0 create a packet loss analyzer, use default options - var analyzer = speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{ + analyzer := speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{ SourceInterface: *source, }) diff --git a/speedtest/loss.go b/speedtest/loss.go index a7cc3d1..dfc4762 100644 --- a/speedtest/loss.go +++ b/speedtest/loss.go @@ -68,7 +68,7 @@ func (pla *PacketLossAnalyzer) RunMulti(hosts []string) (float64, error) { } func (pla *PacketLossAnalyzer) RunMultiWithContext(ctx context.Context, hosts []string) (float64, error) { - results := make(map[string]float64) + results := make(map[string]*transport.PLoss) mutex := &sync.Mutex{} wg := &sync.WaitGroup{} for _, host := range hosts { @@ -76,10 +76,9 @@ func (pla *PacketLossAnalyzer) RunMultiWithContext(ctx context.Context, hosts [] go func(h string) { defer wg.Done() _ = pla.RunWithContext(ctx, h, func(packetLoss *transport.PLoss) { - loss := packetLoss.Loss() - if loss != -1 { + if packetLoss.Sent != 0 { mutex.Lock() - results[h] = loss + results[h] = packetLoss mutex.Unlock() } }) @@ -89,11 +88,13 @@ func (pla *PacketLossAnalyzer) RunMultiWithContext(ctx context.Context, hosts [] if len(results) == 0 { return -1, transport.ErrUnsupported } - packetLossAvg := 0.0 + var pLoss transport.PLoss for _, hostPacketLoss := range results { - packetLossAvg += hostPacketLoss + pLoss.Sent += hostPacketLoss.Sent + pLoss.Dup += hostPacketLoss.Dup + pLoss.Max += hostPacketLoss.Max } - return packetLossAvg / float64(len(results)), nil + return pLoss.Loss(), nil } func (pla *PacketLossAnalyzer) Run(host string, callback func(packetLoss *transport.PLoss)) error { diff --git a/speedtest/server.go b/speedtest/server.go index 2997bcd..19a3c50 100644 --- a/speedtest/server.go +++ b/speedtest/server.go @@ -133,6 +133,15 @@ func (servers Servers) Swap(i, j int) { servers[i], servers[j] = servers[j], servers[i] } +// Hosts return hosts of servers +func (servers Servers) Hosts() []string { + var retServer []string + for _, server := range servers { + retServer = append(retServer, server.Host) + } + return retServer +} + // Less compares the distance. For sorting servers. func (b ByDistance) Less(i, j int) bool { return b.Servers[i].Distance < b.Servers[j].Distance