-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
54 lines (50 loc) · 1.17 KB
/
utils.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
package main
import (
"fmt"
"net"
"net/http"
"os/exec"
"time"
)
// NetWorkStatus 检测网络系统调用速度最快
func NetWorkStatus() bool {
cmd := exec.Command("ping", "www.baidu.com", "-c", "4", "-W", "5")
t1 := time.Now()
err := cmd.Run()
fmt.Println("waist time :", time.Now().Sub(t1))
if err != nil {
fmt.Println(err.Error())
return false
} else {
fmt.Println("Net Status , OK")
}
return true
}
// CheckServer 检测网络 TCP 实现速度稍慢
func CheckServer() {
timeout := time.Duration(5 * time.Second)
t1 := time.Now()
_, err := net.DialTimeout("tcp", "www.baidu.com:443", timeout)
fmt.Println("waist time :", time.Now().Sub(t1))
if err != nil {
fmt.Println("Site unreachable, error: ", err)
return
}
fmt.Println("tcp server is ok")
}
// CheckServerHttp 检测网络 HTTP 实现速度最慢
func CheckServerHttp() {
timeout := time.Duration(5 * time.Second)
client := http.Client{
Timeout: timeout,
}
t1 := time.Now()
resp, err := client.Get("https://www.baidu.com")
fmt.Println("waist time :", time.Now().Sub(t1))
if err != nil {
fmt.Println("client.Get, error: ", err)
return
}
defer resp.Body.Close()
fmt.Println(resp.Status)
}