-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathmain.go
44 lines (36 loc) · 1.22 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
package main
import (
"fmt"
"github.com/showwin/speedtest-go/speedtest"
"log"
)
func main() {
// _, _ = speedtest.FetchUserInfo()
// Get a list of servers near a specified location
// user.SetLocationByCity("Tokyo")
// user.SetLocation("Osaka", 34.6952, 135.5006)
// Select a network card as the data interface.
// speedtest.WithUserConfig(&speedtest.UserConfig{Source: "192.168.1.101"})(speedtestClient)
// Search server using serverID.
// eg: fetch server with ID 28910.
// speedtest.ErrEmptyServers will be returned if the server cannot be found.
// server, err := speedtest.FetchServerByID("28910")
serverList, _ := speedtest.FetchServers()
targets, _ := serverList.FindServer([]int{})
for _, s := range targets {
// Please make sure your host can access this test server,
// otherwise you will get an error.
// It is recommended to replace a server at this time
checkError(s.PingTest(nil))
checkError(s.DownloadTest())
checkError(s.UploadTest())
// Note: The unit of s.DLSpeed, s.ULSpeed is bytes per second, this is a float64.
fmt.Printf("Latency: %s, Download: %s, Upload: %s\n", s.Latency, s.DLSpeed, s.ULSpeed)
s.Context.Reset()
}
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}