Skip to content

Commit 202fb08

Browse files
authored
fix: packet loss calc method (#206)
* add(api): add Hosts for servers * fix(api): calc packet loss with mixed PLoss * chore(cli): lint error
1 parent b033183 commit 202fb08

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

example/packet_loss/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ func main() {
2626

2727
wg := &sync.WaitGroup{}
2828
// 3. Perform packet loss analysis on all available servers
29-
var hosts []string
3029
for _, server := range *targets {
31-
hosts = append(hosts, server.Host)
3230
wg.Add(1)
3331
//ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
3432
//go func(server *speedtest.Server, analyzer *speedtest.PacketLossAnalyzer, ctx context.Context, cancel context.CancelFunc) {
@@ -52,7 +50,7 @@ func main() {
5250
wg.Wait()
5351

5452
// use mixed PacketLoss
55-
mixed, err := analyzer.RunMulti(hosts)
53+
mixed, err := analyzer.RunMulti(serverList.Hosts())
5654
checkError(err)
5755
fmt.Printf("Mixed packets lossed: %.2f\n", mixed)
5856
}

speedtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func main() {
136136
})
137137

138138
// 3.0 create a packet loss analyzer, use default options
139-
var analyzer = speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{
139+
analyzer := speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{
140140
SourceInterface: *source,
141141
})
142142

speedtest/loss.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,17 @@ func (pla *PacketLossAnalyzer) RunMulti(hosts []string) (float64, error) {
6868
}
6969

7070
func (pla *PacketLossAnalyzer) RunMultiWithContext(ctx context.Context, hosts []string) (float64, error) {
71-
results := make(map[string]float64)
71+
results := make(map[string]*transport.PLoss)
7272
mutex := &sync.Mutex{}
7373
wg := &sync.WaitGroup{}
7474
for _, host := range hosts {
7575
wg.Add(1)
7676
go func(h string) {
7777
defer wg.Done()
7878
_ = pla.RunWithContext(ctx, h, func(packetLoss *transport.PLoss) {
79-
loss := packetLoss.Loss()
80-
if loss != -1 {
79+
if packetLoss.Sent != 0 {
8180
mutex.Lock()
82-
results[h] = loss
81+
results[h] = packetLoss
8382
mutex.Unlock()
8483
}
8584
})
@@ -89,11 +88,13 @@ func (pla *PacketLossAnalyzer) RunMultiWithContext(ctx context.Context, hosts []
8988
if len(results) == 0 {
9089
return -1, transport.ErrUnsupported
9190
}
92-
packetLossAvg := 0.0
91+
var pLoss transport.PLoss
9392
for _, hostPacketLoss := range results {
94-
packetLossAvg += hostPacketLoss
93+
pLoss.Sent += hostPacketLoss.Sent
94+
pLoss.Dup += hostPacketLoss.Dup
95+
pLoss.Max += hostPacketLoss.Max
9596
}
96-
return packetLossAvg / float64(len(results)), nil
97+
return pLoss.Loss(), nil
9798
}
9899

99100
func (pla *PacketLossAnalyzer) Run(host string, callback func(packetLoss *transport.PLoss)) error {

speedtest/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ func (servers Servers) Swap(i, j int) {
133133
servers[i], servers[j] = servers[j], servers[i]
134134
}
135135

136+
// Hosts return hosts of servers
137+
func (servers Servers) Hosts() []string {
138+
var retServer []string
139+
for _, server := range servers {
140+
retServer = append(retServer, server.Host)
141+
}
142+
return retServer
143+
}
144+
136145
// Less compares the distance. For sorting servers.
137146
func (b ByDistance) Less(i, j int) bool {
138147
return b.Servers[i].Distance < b.Servers[j].Distance

0 commit comments

Comments
 (0)