-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
56 lines (47 loc) · 1.15 KB
/
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
47
48
49
50
51
52
53
54
55
56
package quacfka
import (
"fmt"
"strings"
"time"
)
func formatBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%s %cB", formatLargeNumber(float64(bytes)/float64(div)), "KMGTPE"[exp])
}
func formatDuration(d time.Duration) string {
return d.Round(time.Millisecond).String()
}
func formatLargeNumber(n float64) string {
parts := strings.Split(fmt.Sprintf("%.2f", n), ".")
intPart := parts[0]
decPart := parts[1]
var result []rune
for i, c := range reverse(intPart) {
if i > 0 && i%3 == 0 {
result = append(result, '_')
}
result = append(result, c)
}
return fmt.Sprintf("%s.%s", string(reverse(string(result))), decPart)
}
func formatThroughput(t float64) string {
return formatLargeNumber(t)
}
func formatThroughputBytes(t float64) string {
return fmt.Sprintf("%s/second", formatBytes(int64(t)))
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}