-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
46 lines (41 loc) · 865 Bytes
/
util.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
package perf
import (
"fmt"
"strconv"
"strings"
"time"
)
var kmgMap = map[string]uint64{"K": 1024, "M": 1024 * 1024, "G": 1024 * 1024 * 1024}
func bandwidth(b uint64, d time.Duration) uint64 {
return uint64(float64(b) / d.Seconds())
}
func formatBytes(b uint64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
func ParseBytes(input string) uint64 {
if input == "" {
return 0
}
var kmg uint64 = 1
for s, v := range kmgMap {
if strings.ToUpper(input[len(input)-1:]) == s {
input = input[:len(input)-1]
kmg = v
break
}
}
num, err := strconv.ParseUint(input, 10, 64)
if err != nil {
panic("invalid kmg number")
}
return num * kmg
}