-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.go
92 lines (73 loc) · 1.79 KB
/
statistics.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
package main
import "C"
import (
"time"
)
type StatChannel struct {
secondCounter int
totalCounter int
average5Sec int
average30Sec int
}
func (sc *StatChannel) Add(count int) {
sc.secondCounter += count
sc.totalCounter += count
}
func (sc *StatChannel) Rate() int {
return sc.average5Sec
}
func (sc *StatChannel) LowPassRate() int {
return sc.average30Sec
}
func (sc *StatChannel) AddChannel(s StatChannel) *StatChannel {
sc.secondCounter += s.secondCounter
sc.totalCounter += s.totalCounter
return sc
}
func (sc *StatChannel) calc(duration time.Duration) {
sample := (sc.secondCounter * 1000) / int(duration.Milliseconds())
sc.average5Sec = sc.average5Sec*4/5 + sample/5
sc.average30Sec = sc.average30Sec*29/30 + sample/30
sc.secondCounter = 0
}
const (
CH_UPLOAD = iota
CH_DOWNLOAD
)
type Statistics struct {
channels []*StatChannel
}
func NewStatistics() *Statistics {
return &Statistics{channels: []*StatChannel{{}, {}}}
}
func MakeStatistics() Statistics {
return Statistics{channels: []*StatChannel{{}, {}}}
}
//func (s *Statistics) Add(stat Statistics) {
//for i := 0; i < len(s.channels); i++ {
// s.channels[i].AddChannel(*stat.channels[i])
//}
//}
func (s *Statistics) SecondTick(duration time.Duration) {
for _, x := range s.channels {
x.calc(duration)
}
}
func (s *Statistics) ReceiveBytes(bytes int) {
s.channels[CH_DOWNLOAD].Add(bytes)
}
func (s *Statistics) SendBytes(bytes int) {
s.channels[CH_UPLOAD].Add(bytes)
}
func (s *Statistics) DownloadRate() int {
return s.channels[CH_DOWNLOAD].Rate()
}
func (s *Statistics) UploadRate() int {
return s.channels[CH_UPLOAD].Rate()
}
func (s *Statistics) DownloadLowPassRate() int {
return s.channels[CH_DOWNLOAD].LowPassRate()
}
func (s *Statistics) UploadLowPassRate() int {
return s.channels[CH_UPLOAD].LowPassRate()
}