forked from orestonce/m3u8d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed.go
66 lines (53 loc) · 1.38 KB
/
speed.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
package m3u8d
import (
"strconv"
"time"
)
func (this *downloadEnv) speedSetBegin() {
this.speedBytesLocker.Lock()
defer this.speedBytesLocker.Unlock()
this.speedBeginTime = time.Now()
}
func (this *downloadEnv) speedAddBytes(a int) {
this.speedBytesLocker.Lock()
defer this.speedBytesLocker.Unlock()
now := time.Now()
this.speedBytesMap[now] += int64(a)
}
func (this *downloadEnv) speedClearBytes() {
this.speedBytesLocker.Lock()
defer this.speedBytesLocker.Unlock()
this.speedBytesMap = map[time.Time]int64{}
}
func (this *downloadEnv) speedRecent5sGetAndUpdate() string {
this.speedBytesLocker.Lock()
defer this.speedBytesLocker.Unlock()
now := time.Now()
if this.GetIsCancel() || this.speedBeginTime.IsZero() || now.Sub(this.speedBeginTime) < time.Second { // 1s以内, 暂时不计算速度
return ""
}
const secondCount = 5
expireTime := now.Add(-secondCount * time.Second)
var total int64
for ct, v := range this.speedBytesMap {
if ct.Before(expireTime) {
delete(this.speedBytesMap, ct)
continue
}
total += v
}
realSecond := now.Sub(this.speedBeginTime).Seconds()
if realSecond > secondCount {
realSecond = secondCount
}
v := float64(total) / realSecond
if v < 1024 {
return strconv.Itoa(int(v)) + " B/s"
}
v = v / 1024
if v < 1024 {
return strconv.Itoa(int(v)) + " KB/s"
}
v = v / 1024
return strconv.FormatFloat(v, 'f', 2, 64) + " MB/s"
}