forked from ddo/fast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast.go
133 lines (112 loc) · 2.51 KB
/
fast.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/ddo/go-fast"
"github.com/ddo/go-spin"
)
func main() {
var kb, mb, gb, silent bool
flag.BoolVar(&kb, "k", false, "Format output in Kbps")
flag.BoolVar(&mb, "m", false, "Format output in Mbps")
flag.BoolVar(&gb, "g", false, "Format output in Gbps")
flag.BoolVar(&silent, "silent", false, "Surpress all output except for the final result")
flag.Parse()
if kb && (mb || gb) || (mb && kb) {
fmt.Println("You may have at most one formating switch. Choose either -k, -m, or -g")
os.Exit(-1)
}
status := ""
spinner := spin.New("")
// output
ticker := time.NewTicker(100 * time.Millisecond)
if !silent {
go func() {
for range ticker.C {
fmt.Printf("%c[2K %s %s\r", 27, spinner.Spin(), status)
}
}()
}
// output
fastCom := fast.New()
// init
err := fastCom.Init()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
status = "connecting"
// get urls
urls, err := fastCom.GetUrls()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
status = "loading"
// measure
KbpsChan := make(chan float64)
go func() {
var value, units string
for Kbps := range KbpsChan {
value, units = format(Kbps, kb, mb, gb)
// don't print the units of measurement if explicitly asked for
if kb || mb || gb {
status = fmt.Sprintf("%s", value)
} else {
status = fmt.Sprintf("%s %s", value, units)
}
}
if silent {
fmt.Printf("%s\n", status)
} else {
fmt.Printf("\r%c[2K -> %s\n", 27, status)
}
}()
err = fastCom.Measure(urls, KbpsChan)
ticker.Stop()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
func formatGbps(Kbps float64) (string, string, float64) {
f := "%.2f"
unit := "Gbps"
value := Kbps / 1000000
return f, unit, value
}
func formatMbps(Kbps float64) (string, string, float64) {
f := "%.2f"
unit := "Mbps"
value := Kbps / 1000
return f, unit, value
}
func formatKbps(Kbps float64) (string, string, float64) {
f := "%.f"
unit := "Kbps"
value := Kbps
return f, unit, value
}
func format(Kbps float64, kb bool, mb bool, gb bool) (string, string) {
var value float64
var unit string
var f string
if kb {
f, unit, value = formatKbps(Kbps)
} else if mb {
f, unit, value = formatMbps(Kbps)
} else if gb {
f, unit, value = formatGbps(Kbps)
} else if Kbps > 1000000 { // Gbps
f, unit, value = formatGbps(Kbps)
} else if Kbps > 1000 { // Mbps
f, unit, value = formatMbps(Kbps)
} else {
f, unit, value = formatKbps(Kbps)
}
strValue := fmt.Sprintf(f, value)
return strValue, unit
}