Skip to content

Commit

Permalink
fix(api): calc packet loss with mixed PLoss
Browse files Browse the repository at this point in the history
  • Loading branch information
r3inbowari committed May 9, 2024
1 parent f70be76 commit 95a4fe5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 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
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

0 comments on commit 95a4fe5

Please sign in to comment.