Skip to content

Commit

Permalink
fix: packet loss calc method (#206)
Browse files Browse the repository at this point in the history
* add(api): add Hosts for servers

* fix(api): calc packet loss with mixed PLoss

* chore(cli): lint error
  • Loading branch information
r3inbowari authored May 9, 2024
1 parent b033183 commit 202fb08
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 1 addition & 3 deletions example/packet_loss/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down
15 changes: 8 additions & 7 deletions speedtest/loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,17 @@ 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 {
wg.Add(1)
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()
}
})
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions speedtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 202fb08

Please sign in to comment.