实时网速算法已经完成,三网延迟和丢包算法存在问题,研究中

pull/92/head
CHN-STUDENT 2020-12-11 12:31:08 +08:00
parent 6744bee992
commit a42c6c62a2
5 changed files with 264 additions and 63 deletions

View File

@ -2,12 +2,6 @@
懒得改项目名了,需要的话自己改。
### 编译环境
CentOS7: go version go1.15.5 linux/amd64
Windows10: go version go1.15.5 windows/amd64
### 使用方法
可选:更改源码中的连接信息
```

View File

@ -69,25 +69,6 @@ type ClientInfo struct {
HddUsed uint64`json:"hdd_used"`
}
type NetSpeed struct {
netrx float64
nettx float64
clock float64
diff float64
avgrx uint64
avgtx uint64
}
func NewNetSpeed() NetSpeed{
return NetSpeed{
netrx: 0.0,
nettx: 0.0,
clock: 0.0,
diff: 0.0,
avgrx: 0,
avgtx: 0,
}
}
func NewDefaultClientInfo() ClientInfo {
return ClientInfo {
@ -145,40 +126,6 @@ func trafficCount() {
clientInfo.NetworkOut = bytesSent
}
//func getNetworkSpeed() {
// for {
// var bytesSent uint64 = 0
// var bytesRecv uint64 = 0
// netInfo, err := nnet.IOCounters(true)
// if err != nil {
// fmt.Println("Get network speed error:",err)
// }
// for _, v := range netInfo {
// if strings.Index(v.Name,"lo") > -1 ||
// strings.Index(v.Name,"tun") > -1 ||
// strings.Index(v.Name,"docker") > -1 ||
// strings.Index(v.Name,"veth") > -1 ||
// strings.Index(v.Name,"br-") > -1 ||
// strings.Index(v.Name,"vmbr") > -1 ||
// strings.Index(v.Name,"vnet") > -1 ||
// strings.Index(v.Name,"kube") > -1 {
// continue
// }
// bytesSent += v.BytesSent
// bytesRecv += v.BytesRecv
// }
// timeUnix:= float64(time.Now().Unix())
// netSpeed.diff = timeUnix - netSpeed.clock
// netSpeed.clock = timeUnix
// netSpeed.netrx = float64(bytesRecv - netSpeed.avgrx)/netSpeed.diff
// netSpeed.nettx = float64(bytesSent - netSpeed.avgtx)/netSpeed.diff
// clientInfo.NetworkTx = uint64(netSpeed.nettx)
// clientInfo.NetworkRx = uint64(netSpeed.netrx)
// time.Sleep(time.Duration(INTERVAL)*time.Second)
// }
//}
func spaceCount() {
// golang 没有类似于在 python 的 dict 或 tuple 的 in 查找关键字,自己写多重判断实现
diskList, _ := disk.Partitions(false)
@ -283,7 +230,7 @@ func bytes2str(b []byte) string {
}
var clientInfo ClientInfo
var netSpeed NetSpeed
func main() {
for _, args := range os.Args {
@ -352,7 +299,14 @@ func main() {
}
fmt.Println(checkIP)
clientInfo = NewDefaultClientInfo()
netSpeed = NewNetSpeed()
netSpeed := NewNetSpeed()
pingValueCU := NewPingValue()
pingValueCT := NewPingValue()
pingValueCM := NewPingValue()
pingValueCU.Get()
pingValueCT.Get()
pingValueCM.Get()
netSpeed.Run()
for {
clientInfo.MemoryTotal = ram.Info().Total / 1024 // 需要转单位
clientInfo.MemoryUsed = ram.Info().Usage / 1024 // 需要转单位
@ -368,8 +322,12 @@ func main() {
trafficCount()
spaceCount()
getNetworkStatus()
//getNetworkSpeed()
//TODO:三网延迟,三网丢包,网络链接速度
netSpeed.Get()
clientInfo.Ping10086, clientInfo.Time10086 = pingValueCM.Get()
clientInfo.Ping189, clientInfo.Time189 = pingValueCT.Get()
clientInfo.Ping10010, clientInfo.Time10010 = pingValueCU.Get()
//TODO:三网延迟和丢包率算法存在问题
//fmt.Println(clientInfo.Time10086)
//结构体转json字符串
data, err := jsoniter.MarshalToString(&clientInfo)
//fmt.Println(data)

View File

@ -0,0 +1,88 @@
package main
import (
"fmt"
nnet "github.com/shirou/gopsutil/net"
"strings"
"sync"
"time"
)
type NetSpeed struct {
netrx float64
nettx float64
clock float64
diff float64
avgrx uint64
avgtx uint64
stop chan struct{}
mtx sync.Mutex
}
func NewNetSpeed() *NetSpeed{
return &NetSpeed{
netrx: 0.0,
nettx: 0.0,
clock: 0.0,
diff: 0.0,
avgrx: 0,
avgtx: 0,
stop: make (chan struct{}),
}
}
func (netSpeed *NetSpeed) Run() {
go func() {
t1 := time.Duration(INTERVAL) * time.Second
t := time.NewTicker(t1)
for {
select {
case <- netSpeed.stop:
t.Stop()
return
case <-t.C:
netSpeed.mtx.Lock()
var bytesSent uint64 = 0
var bytesRecv uint64 = 0
netInfo, err := nnet.IOCounters(true)
if err != nil {
fmt.Println("Get network speed error:",err)
}
for _, v := range netInfo {
if strings.Index(v.Name,"lo") > -1 ||
strings.Index(v.Name,"tun") > -1 ||
strings.Index(v.Name,"docker") > -1 ||
strings.Index(v.Name,"veth") > -1 ||
strings.Index(v.Name,"br-") > -1 ||
strings.Index(v.Name,"vmbr") > -1 ||
strings.Index(v.Name,"vnet") > -1 ||
strings.Index(v.Name,"kube") > -1 {
continue
}
bytesSent += v.BytesSent
bytesRecv += v.BytesRecv
}
timeUnix:= float64(time.Now().Unix())
netSpeed.diff = timeUnix - netSpeed.clock
netSpeed.clock = timeUnix
netSpeed.netrx = float64(bytesRecv - netSpeed.avgrx)/netSpeed.diff
netSpeed.nettx = float64(bytesSent - netSpeed.avgtx)/netSpeed.diff
netSpeed.avgtx = bytesSent
netSpeed.avgrx = bytesRecv
netSpeed.mtx.Unlock()
}
}
}()
}
func (netSpeed *NetSpeed) Stop() {
close(netSpeed.stop)
}
func (netSpeed *NetSpeed) Get() {
netSpeed.mtx.Lock()
defer netSpeed.mtx.Unlock()
clientInfo.NetworkTx = uint64(netSpeed.nettx)
clientInfo.NetworkRx = uint64(netSpeed.netrx)
}

View File

@ -0,0 +1,161 @@
package main
import (
"fmt"
"net"
"sync"
"time"
)
type PingValue struct {
ping float64
lostRate float64
stop chan struct{}
mtx sync.Mutex
}
func NewPingValue() *PingValue {
return &PingValue{
ping: 0.0,
lostRate: 0.0,
stop: make (chan struct{}),
}
}
func (pingValue *PingValue) RunCU() {
go func() {
t1 := time.Duration(INTERVAL) * time.Second
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
select {
case <- pingValue.stop:
t.Stop()
return
case <-t.C:
t := time.Now()
conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
defer conn.Close()
if err != nil {
fmt.Println("Error try to connect China unicom :", err)
conn.Close()
lostPacket += 1
} else {
conn.Close()
}
diffTime := time.Since(t)
//TODO:三网延迟和丢包率算法存在问题
//fmt.Println(diffTime)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket)
}
pingValue.ping = float64(diffTime * time.Second)
resetTime := uint64(time.Since(startTime) * time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
startTime = time.Now()
}
pingValue.mtx.Unlock()
}
}
}()
}
func (pingValue *PingValue) RunCT() {
go func() {
t1 := time.Duration(INTERVAL) * time.Second
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
select {
case <- pingValue.stop:
t.Stop()
return
case <-t.C:
t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
defer conn.Close()
if err != nil {
fmt.Println("Error try to connect China Telecom :", err)
conn.Close()
lostPacket += 1
} else {
conn.Close()
}
diffTime := time.Since(t)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket)
}
pingValue.ping = float64(diffTime * time.Second)
resetTime := uint64(time.Since(startTime) * time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
startTime = time.Now()
}
pingValue.mtx.Unlock()
}
}
}()
}
func (pingValue *PingValue) RunCM() {
go func() {
t1 := time.Duration(INTERVAL) * time.Second
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
select {
case <- pingValue.stop:
t.Stop()
return
case <-t.C:
t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
defer conn.Close()
if err != nil {
fmt.Println("Error try to connect China mobile :", err)
conn.Close()
lostPacket += 1
} else {
conn.Close()
}
diffTime := time.Since(t)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket)
}
pingValue.ping = float64(diffTime * time.Second)
resetTime := uint64(time.Since(startTime) * time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
startTime = time.Now()
}
pingValue.mtx.Unlock()
}
}
}()
}
func (pingValue *PingValue) Stop() {
close(pingValue.stop)
}
func (pingValue *PingValue) Get() (float64,float64) {
pingValue.mtx.Lock()
defer pingValue.mtx.Unlock()
return pingValue.lostRate,pingValue.ping
}