forked from microsoft/ethr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
264 lines (233 loc) · 4.77 KB
/
utils.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
//-----------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license.
// See LICENSE.txt file in the project root for full license information.
//-----------------------------------------------------------------------------
package main
import (
"net"
"strconv"
"strings"
"syscall"
"time"
"unicode"
"unicode/utf8"
)
//
// TODO: Use a better way to define ports. The core logic is:
// Find a base port, such as 9999, and the Bandwidth is: base - 0,
// Cps is base - 1, Pps is base - 2 and Latency is base - 3
//
const (
hostAddr = ""
ctrlPort = "8888"
tcpBandwidthPort = "9999"
tcpCpsPort = "9998"
tcpPpsPort = "9997"
tcpLatencyPort = "9996"
udpBandwidthPort = "9999"
udpCpsPort = "9998"
udpPpsPort = "9997"
udpLatencyPort = "9996"
httpBandwidthPort = "9899"
httpCpsPort = "9898"
httpPpsPort = "9897"
httpLatencyPort = "9896"
httpsBandwidthPort = "9799"
httpsCpsPort = "9798"
httpsPpsPort = "9797"
httpsLatencyPort = "9796"
protoTCP = "tcp"
protoUDP = "udp"
)
var gDone = false
const (
// UNO represents 1 unit.
UNO = 1
// KILO represents k.
KILO = 1000
// MEGA represents m.
MEGA = 1000 * 1000
// GIGA represents g.
GIGA = 1000 * 1000 * 1000
// TERA represents t.
TERA = 1000 * 1000 * 1000 * 1000
)
func numberToUnit(num uint64) string {
unit := ""
value := float64(num)
switch {
case num >= TERA:
unit = "T"
value = value / TERA
case num >= GIGA:
unit = "G"
value = value / GIGA
case num >= MEGA:
unit = "M"
value = value / MEGA
case num >= KILO:
unit = "K"
value = value / KILO
}
result := strconv.FormatFloat(value, 'f', 2, 64)
result = strings.TrimSuffix(result, ".00")
return result + unit
}
func unitToNumber(s string) uint64 {
s = strings.TrimSpace(s)
s = strings.ToUpper(s)
i := strings.IndexFunc(s, unicode.IsLetter)
if i == -1 {
bytes, err := strconv.ParseFloat(s, 64)
if err != nil || bytes <= 0 {
return 0
}
return uint64(bytes)
}
bytesString, multiple := s[:i], s[i:]
bytes, err := strconv.ParseFloat(bytesString, 64)
if err != nil || bytes <= 0 {
return 0
}
switch multiple {
case "T", "TB", "TIB":
return uint64(bytes * TERA)
case "G", "GB", "GIB":
return uint64(bytes * GIGA)
case "M", "MB", "MIB":
return uint64(bytes * MEGA)
case "K", "KB", "KIB":
return uint64(bytes * KILO)
case "B":
return uint64(bytes)
default:
return 0
}
}
func durationToString(d time.Duration) string {
if d < 0 {
return d.String()
}
ud := uint64(d)
val := float64(ud)
unit := ""
if ud < uint64(60*time.Second) {
switch {
case ud < uint64(time.Microsecond):
unit = "ns"
case ud < uint64(time.Millisecond):
val = val / 1000
unit = "us"
case ud < uint64(time.Second):
val = val / (1000 * 1000)
unit = "ms"
default:
val = val / (1000 * 1000 * 1000)
unit = "s"
}
result := strconv.FormatFloat(val, 'f', 2, 64)
return result + unit
}
return d.String()
}
func bytesToRate(bytes uint64) string {
bits := bytes * 8
result := numberToUnit(bits)
return result
}
func cpsToString(cps uint64) string {
result := numberToUnit(cps)
return result
}
func ppsToString(pps uint64) string {
result := numberToUnit(pps)
return result
}
func testToString(testType EthrTestType) string {
switch testType {
case Bandwidth:
return "Bandwidth"
case Cps:
return "Connections/s"
case Pps:
return "Packets/s"
case Latency:
return "Latency"
default:
return "Invalid"
}
}
func protoToString(proto EthrProtocol) string {
switch proto {
case TCP:
return "TCP"
case UDP:
return "UDP"
case HTTP:
return "HTTP"
case HTTPS:
return "HTTPS"
case ICMP:
return "ICMP"
}
return ""
}
func ethrUnused(vals ...interface{}) {
for _, val := range vals {
_ = val
}
}
func splitString(longString string, maxLen int) []string {
splits := []string{}
var l, r int
for l, r = 0, maxLen; r < len(longString); l, r = r, r+maxLen {
for !utf8.RuneStart(longString[r]) {
r--
}
splits = append(splits, longString[l:r])
}
splits = append(splits, longString[l:])
return splits
}
func max(x, y uint64) uint64 {
if x < y {
return y
}
return x
}
func truncateString(str string, num int) string {
s := str
l := len(str)
if l > num {
if num > 3 {
s = "..." + str[l-num+3:l]
} else {
s = str[l-num : l]
}
}
return s
}
func roundUpToZero(n int64) int64 {
y := n >> 63
return (n ^ y) - y
}
func getFd(conn net.Conn) uintptr {
var fd uintptr
var rc syscall.RawConn
var err error
switch ct := conn.(type) {
case *net.TCPConn:
rc, err = ct.SyscallConn()
if err != nil {
return 0
}
default:
return 0
}
fn := func(s uintptr) {
fd = s
}
rc.Control(fn)
return fd
}