-
Notifications
You must be signed in to change notification settings - Fork 16
/
performance.go
138 lines (120 loc) · 2.96 KB
/
performance.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ioperformance
import (
"github.com/annchain/OG/arefactor/common/goroutine"
"github.com/annchain/OG/common"
"github.com/sirupsen/logrus"
"sync"
"time"
)
type IoData struct {
send common.IOSize
recv common.IOSize
Send string `json:"send"`
Recv string `json:"recv"`
SendNum int `json:"send_num"`
RecvNum int `json:"recv_num"`
Time time.Time `json:"time"`
}
type IoDataInfo struct {
DataSize []IoData `json:"data_size"`
AvgSend string `json:"avg_send"`
TotalSendNum int `json:"total_send_num"`
AvgRecv string `json:"avg_recv"`
TotalRecvNum int `json:"total_receiv_num"`
}
type iOPerformance struct {
quit chan bool
dataSize []IoData
send int
sendNum int
recv int
recvNum int
mu sync.RWMutex
}
var performance *iOPerformance
func Init() *iOPerformance {
performance = new(iOPerformance)
performance.quit = make(chan bool)
return performance
}
func (i *iOPerformance) Start() {
goroutine.New(i.run)
}
func (i *iOPerformance) Stop() {
close(i.quit)
}
func (i *iOPerformance) Name() string {
return "iOPerformance"
}
func GetNetPerformance() *IoDataInfo {
i := performance
var info IoDataInfo
var dataSize []IoData
i.mu.RLock()
dataSize = i.dataSize
i.mu.RUnlock()
if len(dataSize) == 0 {
return &info
}
totalSend := common.IOSize(0)
totalReceiv := common.IOSize(0)
for i, d := range dataSize {
totalSend += d.send
totalReceiv += d.recv
info.TotalRecvNum += d.RecvNum
info.TotalSendNum += d.SendNum
dataSize[i].Send = d.send.String()
dataSize[i].Recv = d.recv.String()
info.DataSize = append(info.DataSize, dataSize[len(dataSize)-i-1])
}
avgSend := totalSend / common.IOSize(len(dataSize))
avgRecv := totalReceiv / common.IOSize(len(dataSize))
info.AvgSend = avgSend.String()
info.AvgRecv = avgRecv.String()
return &info
}
func AddSendSize(size int) {
performance.AddSendSize(size)
}
func AddRecvSize(size int) {
performance.AddRecvSize(size)
}
func (i *iOPerformance) AddSendSize(size int) {
i.mu.Lock()
defer i.mu.Unlock()
i.send += size
i.sendNum++
}
func (i *iOPerformance) AddRecvSize(size int) {
i.mu.Lock()
defer i.mu.Unlock()
i.recv += size
i.recvNum++
}
func (s *iOPerformance) run() {
var i int
for {
select {
case <-time.After(time.Second):
s.mu.Lock()
if i == 60 {
ioData := IoData{send: common.IOSize(s.send), recv: common.IOSize(s.recv), SendNum: s.sendNum, RecvNum: s.recvNum}
ioData.Time = time.Now()
s.dataSize = s.dataSize[1:]
s.dataSize = append(s.dataSize, ioData)
s.send, s.recv, s.sendNum, s.recvNum = 0, 0, 0, 0
} else {
ioData := IoData{send: common.IOSize(s.send), recv: common.IOSize(s.recv), SendNum: s.sendNum, RecvNum: s.recvNum}
ioData.Time = time.Now()
s.dataSize = append(s.dataSize, ioData)
s.send, s.recv, s.sendNum, s.recvNum = 0, 0, 0, 0
i++
}
s.mu.Unlock()
logrus.WithField("data ", s.dataSize[0]).Debug("performance")
//
case <-s.quit:
return
}
}
}