-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathunit.go
110 lines (95 loc) · 2.45 KB
/
unit.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
package speedtest
import (
"strconv"
)
type UnitType int
// IEC and SI
const (
UnitTypeDecimalBits = UnitType(iota) // auto scaled
UnitTypeDecimalBytes // auto scaled
UnitTypeBinaryBits // auto scaled
UnitTypeBinaryBytes // auto scaled
UnitTypeDefaultMbps // fixed
)
var (
DecimalBitsUnits = []string{"bps", "Kbps", "Mbps", "Gbps"}
DecimalBytesUnits = []string{"B/s", "KB/s", "MB/s", "GB/s"}
BinaryBitsUnits = []string{"Kibps", "KiMbps", "KiGbps"}
BinaryBytesUnits = []string{"KiB/s", "MiB/s", "GiB/s"}
)
var unitMaps = map[UnitType][]string{
UnitTypeDecimalBits: DecimalBitsUnits,
UnitTypeDecimalBytes: DecimalBytesUnits,
UnitTypeBinaryBits: BinaryBitsUnits,
UnitTypeBinaryBytes: BinaryBytesUnits,
}
const (
B = 1.0
KB = 1000 * B
MB = 1000 * KB
GB = 1000 * MB
IB = 1
KiB = 1024 * IB
MiB = 1024 * KiB
GiB = 1024 * MiB
)
type ByteRate float64
var globalByteRateUnit UnitType
func (r ByteRate) String() string {
if r == 0 {
return "0.00 Mbps"
}
if r == -1 {
return "N/A"
}
if globalByteRateUnit != UnitTypeDefaultMbps {
return r.Byte(globalByteRateUnit)
}
return strconv.FormatFloat(float64(r/125000.0), 'f', 2, 64) + " Mbps"
}
// SetUnit Set global output units
func SetUnit(unit UnitType) {
globalByteRateUnit = unit
}
func (r ByteRate) Mbps() float64 {
return float64(r) / 125000.0
}
func (r ByteRate) Gbps() float64 {
return float64(r) / 125000000.0
}
// Byte Specifies the format output byte rate
func (r ByteRate) Byte(formatType UnitType) string {
if r == 0 {
return "0.00 Mbps"
}
if r == -1 {
return "N/A"
}
return format(float64(r), formatType)
}
func format(byteRate float64, i UnitType) string {
val := byteRate
if i%2 == 0 {
val *= 8
}
if i < UnitTypeBinaryBits {
switch {
case byteRate >= GB:
return strconv.FormatFloat(val/GB, 'f', 2, 64) + " " + unitMaps[i][3]
case byteRate >= MB:
return strconv.FormatFloat(val/MB, 'f', 2, 64) + " " + unitMaps[i][2]
case byteRate >= KB:
return strconv.FormatFloat(val/KB, 'f', 2, 64) + " " + unitMaps[i][1]
default:
return strconv.FormatFloat(val/B, 'f', 2, 64) + " " + unitMaps[i][0]
}
}
switch {
case byteRate >= GiB:
return strconv.FormatFloat(val/GiB, 'f', 2, 64) + " " + unitMaps[i][2]
case byteRate >= MiB:
return strconv.FormatFloat(val/MiB, 'f', 2, 64) + " " + unitMaps[i][1]
default:
return strconv.FormatFloat(val/KiB, 'f', 2, 64) + " " + unitMaps[i][0]
}
}