-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiperf_gui.go
executable file
·265 lines (223 loc) · 5.72 KB
/
iperf_gui.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
)
type IperfResult struct {
Index int64
Rate float64
Jitter float64
Total int64
Lost int64
LossRate float64
}
type IperfResults struct {
IRs []IperfResult
Mu sync.Mutex
}
var Results IperfResults
var version bool
var isIperf2 bool
var isUdp bool
var port int
func init() {
flag.BoolVar(&version, "v", false, "Version")
flag.BoolVar(&isIperf2, "2", false, "Use Iperf2")
flag.BoolVar(&isUdp, "u", false, "Use UDP with Iperf2")
flag.IntVar(&port, "p", 4096, "Http server listening port")
flag.Parse()
if version {
fmt.Println(AppVersion)
os.Exit(0)
}
}
func RateHandler(w http.ResponseWriter, r *http.Request) {
var result [][]string
for _, res := range Results.IRs {
result = append(result, []string{strconv.FormatInt(res.Index, 10), strconv.FormatFloat(res.Rate, 'E', -1, 64)})
}
resJson, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(resJson)
}
func JitterHandler(w http.ResponseWriter, r *http.Request) {
var result [][]string
for _, res := range Results.IRs {
result = append(result, []string{strconv.FormatInt(res.Index, 10), strconv.FormatFloat(res.Jitter, 'E', -1, 64)})
}
resJson, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(resJson)
}
func LossRateHandler(w http.ResponseWriter, r *http.Request) {
var result [][]string
for _, res := range Results.IRs {
result = append(result, []string{strconv.FormatInt(res.Index, 10), strconv.FormatFloat(res.LossRate, 'E', -1, 64)})
}
resJson, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(resJson)
}
func TcpSrvResultHandler(m string) (r IperfResult, err error) {
reg, _ := regexp.Compile(`.*sec\s+\S+\s+KBytes\s+(\S+)\s+Kbits.*`)
if reg.MatchString(m) {
var rate float64
rate, err = strconv.ParseFloat(reg.FindStringSubmatch(m)[1], 64)
if err != nil {
fmt.Printf("Failed to parse %s to float64\n", reg.FindStringSubmatch(m)[1])
return
}
r.Rate = rate
} else {
err = errors.New("Failed to match: " + m)
fmt.Println(err.Error())
}
return
}
func UdpSrvResultHandler(m string) (r IperfResult, err error) {
reg, _ := regexp.Compile(`.*sec\s+\S+\s+KBytes\s+(\S+)\s+Kbits/sec\s+(\S+)\s+ms\s+(\S+)/\s*(\S+)\s+\((\S+)%\).*`)
if reg.MatchString(m) {
var rate float64
rate, err = strconv.ParseFloat(reg.FindStringSubmatch(m)[1], 64)
if err != nil {
fmt.Printf("Failed to parse %s to float64\n", reg.FindStringSubmatch(m)[1])
return
}
r.Rate = rate
var jitter float64
jitter, err = strconv.ParseFloat(reg.FindStringSubmatch(m)[2], 64)
if err != nil {
fmt.Printf("Failed to parse %s to float64\n", reg.FindStringSubmatch(m)[2])
return
}
r.Jitter = jitter
var lost int64
lost, err = strconv.ParseInt(reg.FindStringSubmatch(m)[3], 10, 64)
if err != nil {
fmt.Printf("Failed to parse %s to int64\n", reg.FindStringSubmatch(m)[3])
return
}
r.Lost = lost
var total int64
total, err = strconv.ParseInt(reg.FindStringSubmatch(m)[4], 10, 64)
if err != nil {
fmt.Printf("Failed to parse %s to int64\n", reg.FindStringSubmatch(m)[4])
return
}
r.Total = total
var lossRate float64
lossRate, err = strconv.ParseFloat(reg.FindStringSubmatch(m)[5], 64)
if err != nil {
fmt.Printf("Failed to parse %s to float64\n", reg.FindStringSubmatch(m)[5])
return
}
r.LossRate = lossRate
} else {
err = errors.New("Failed to match: " + m)
fmt.Println(err.Error())
}
return
}
func main() {
go func() {
fmt.Printf("Start HTTP Server on port %d\n", port)
http.HandleFunc("/rate", RateHandler)
http.HandleFunc("/jitter", JitterHandler)
http.HandleFunc("/lossrate", LossRateHandler)
http.Handle("/", http.StripPrefix("/", http.FileServer(assetFS())))
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil {
fmt.Println("Failed to start http server with error: " + err.Error())
os.Exit(0)
}
}()
var cmd *exec.Cmd
var err error
if isIperf2 {
iperf2, err := exec.LookPath("iperf")
if err != nil {
fmt.Println("The iperf2 is not installed")
os.Exit(0)
}
var args string
if isUdp {
args = "-s -u -i 1 -f k"
} else {
args = "-s -i 1 -f k"
}
cmd = exec.Command(iperf2, strings.Split(args, " ")...)
} else {
iperf3, err := exec.LookPath("iperf3")
if err != nil {
fmt.Println("The iperf3 is not installed")
os.Exit(0)
}
args := "--forceflush -s -i 1 -f k"
cmd = exec.Command(iperf3, strings.Split(args, " ")...)
}
defer func() {
_ = cmd.Process.Kill()
}()
stdout, _ := cmd.StdoutPipe()
scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanLines)
err = cmd.Start()
if err != nil {
fmt.Println(err)
_ = cmd.Process.Kill()
os.Exit(0)
}
mode := "tcp"
index := 0
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
if strings.Contains(m, "Interval") {
if strings.Contains(m, "Jitter") {
mode = "udp"
} else {
mode = "tcp"
}
}
if strings.Contains(m, " sec ") && !strings.Contains(m, "receiver") {
var r IperfResult
if mode == "tcp" {
r, err = TcpSrvResultHandler(m)
} else if mode == "udp" {
r, err = UdpSrvResultHandler(m)
}
if err != nil {
fmt.Println(err)
continue
}
r.Index = int64(index)
index++
Results.Mu.Lock()
Results.IRs = append(Results.IRs, r)
Results.Mu.Unlock()
}
}
_ = cmd.Wait()
}