Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loss calc method #206

Merged
merged 3 commits into from
May 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(api): calc packet loss with mixed PLoss
r3inbowari committed May 9, 2024
commit 95a4fe5b837ea9b71148e2a2b389f59d617ba7ad
4 changes: 1 addition & 3 deletions example/packet_loss/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 8 additions & 7 deletions speedtest/loss.go
Original file line number Diff line number Diff line change
@@ -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()
}
})
@@ -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 {