-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.go
64 lines (57 loc) · 1.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"github.com/showwin/speedtest-go/speedtest"
"github.com/showwin/speedtest-go/speedtest/transport"
"log"
"sync"
"time"
)
// Note: The current packet loss analyzer does not support udp over http.
// This means we cannot get packet loss through a proxy.
func main() {
// 0. Fetching servers
serverList, err := speedtest.FetchServers()
checkError(err)
// 1. Retrieve available servers
targets := serverList.Available()
// 2. Create a packet loss analyzer, use default options
analyzer := speedtest.NewPacketLossAnalyzer(&speedtest.PacketLossAnalyzerOptions{
PacketSendingInterval: time.Millisecond * 100,
})
wg := &sync.WaitGroup{}
// 3. Perform packet loss analysis on all available servers
for _, server := range *targets {
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) {
go func(server *speedtest.Server, analyzer *speedtest.PacketLossAnalyzer) {
//defer cancel()
defer wg.Done()
// Note: Please call ctx.cancel at the appropriate time to release resources if you use analyzer.RunWithContext
// we using analyzer.Run() here.
err = analyzer.Run(server.Host, func(packetLoss *transport.PLoss) {
fmt.Println(packetLoss, server.Host, server.Name)
})
//err = analyzer.RunWithContext(ctx, server.Host, func(packetLoss *transport.PLoss) {
// fmt.Println(packetLoss, server.Host, server.Name)
//})
if err != nil {
fmt.Println(err)
}
//}(server, analyzer, ctx, cancel)
}(server, analyzer)
}
wg.Wait()
// use mixed PacketLoss
mixed, err := analyzer.RunMulti(serverList.Hosts())
checkError(err)
fmt.Printf("Mixed packets lossed: %.2f%%\n", mixed.LossPercent())
fmt.Printf("Mixed packets lossed: %.2f\n", mixed.Loss())
fmt.Printf("Mixed packets lossed: %s\n", mixed)
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}