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
Show file tree
Hide file tree
Changes from all commits
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
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
Loading