This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
forked from microsoft/ethr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethr.go
207 lines (189 loc) · 5.46 KB
/
ethr.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
//-----------------------------------------------------------------------------
// 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 (
"flag"
"fmt"
"os"
"runtime"
"strings"
"time"
)
const defaultLogFileName = "./ethrs.log for server, ./ethrc.log for client"
func main() {
isServer := flag.Bool("s", false, "Run as server")
clientServerIP := flag.String("c", "",
"Run as client and connect to server specified by String")
testTypePtr := flag.String("t", "b",
"Test to run (\"b\", \"c\", \"p\" or \"l\")\n"+
"b: Bandwidth\n"+
"c: Connections/s or Requests/s\n"+
"p: Packets/s\n"+
"l: Latency, Loss & Jitter")
thCount := flag.Int("n", 1,
"Number of Threads\n"+
"0: Equal to number of CPUs")
bufLenStr := flag.String("l", "16KB",
"Length of buffer to use (format: <num>[KB | MB | GB])\n"+
"Only valid for Bandwidth tests. Max 1GB.")
protocol := flag.String("p", "tcp",
"Protocol (\"tcp\", \"udp\", \"http\", \"https\", or \"icmp\")")
outputFile := flag.String("o", defaultLogFileName,
"Name of the file for logging output.\n")
debug := flag.Bool("debug", false, "Log debug output. Only valid if \"-o\" is specified.")
noOutput := flag.Bool("no", false, "Disable logging output to file.")
durationStr := flag.String("d", "10s",
"Duration for the test (format: <num>[s | m | h] \n"+
"0: Run forever")
showUI := flag.Bool("ui", false, "Show output in text UI. Valid for server only.")
rttCount := flag.Int("i", 1000,
"Number of round trip iterations for calculating latency.")
portStr := flag.String("ports", "",
"Ports to use for server and client\n"+
"Format: \"key1=value1, key2=value2\"\n"+
"Example: \"control=8888, tcp=9999, http=8099\"\n"+
"For protocols, only base port is specified, so tcp=9999 means:\n"+
"9999 - Bandwidth, 9998 - CPS, 9997 - PPS, 9996 - Latency tests\n"+
"Default: control=8888, tcp=9999, udp=9999, http=9899, https=9799")
ethrUnused(portStr)
ethrUnused(noOutput)
flag.Parse()
//
// TODO: Handle the case if there are incorrect arguments
// fmt.Println("Number of incorrect arguments: " + strconv.Itoa(flag.NArg()))
//
if (*isServer && *clientServerIP != "") ||
(!*isServer && *clientServerIP == "") {
fmt.Println("Please specify either server mode (-s) or client mode (-c).")
flag.PrintDefaults()
os.Exit(1)
}
bufLen := unitToNumber(*bufLenStr)
if bufLen == 0 {
fmt.Println("Invalid length specified: " + *bufLenStr)
flag.PrintDefaults()
os.Exit(1)
}
if *rttCount <= 0 {
fmt.Println("Invalid RTT count for latency test:", *rttCount)
flag.PrintDefaults()
os.Exit(1)
}
var testType EthrTestType
switch *testTypePtr {
case "b":
testType = Bandwidth
case "c":
testType = Cps
case "p":
testType = Pps
case "l":
testType = Latency
default:
fmt.Printf("Invalid value \"%s\" specified for parameter \"-t\".\n"+
"Valid parameters and values are:\n", *testTypePtr)
flag.PrintDefaults()
os.Exit(1)
}
p := strings.ToUpper(*protocol)
proto := TCP
switch p {
case "TCP":
proto = TCP
case "UDP":
proto = UDP
case "HTTP":
proto = HTTP
case "HTTPS":
proto = HTTPS
case "ICMP":
proto = ICMP
default:
fmt.Printf("Invalid value \"%s\" specified for parameter \"-p\".\n"+
"Valid parameters and values are:\n", *protocol)
flag.PrintDefaults()
os.Exit(1)
}
duration, err := time.ParseDuration(*durationStr)
if err != nil {
fmt.Printf("Invalid value \"%s\" specified for parameter \"-d\".\n",
*durationStr)
flag.PrintDefaults()
os.Exit(1)
}
if *thCount <= 0 {
*thCount = runtime.NumCPU()
}
//
// For Pkt/s, we always override the buffer size to be just 1 byte.
// TODO: Evaluate in future, if we need to support > 1 byte packets for
// Pkt/s testing.
//
if testType == Pps {
bufLen = 1
}
testParam := EthrTestParam{EthrTestID{EthrProtocol(proto), testType},
uint32(*thCount),
uint32(bufLen),
uint32(*rttCount)}
if !validateTestParam(testParam) {
os.Exit(1)
}
generatePortNumbers(*portStr)
logFileName := *outputFile
if *isServer {
if !*noOutput {
if logFileName == defaultLogFileName {
logFileName = "ethrs.log"
}
logInit(logFileName, *debug)
}
runServer(testParam, *showUI)
} else {
if !*noOutput {
if logFileName == defaultLogFileName {
logFileName = "ethrc.log"
}
logInit(logFileName, *debug)
}
runClient(testParam, *clientServerIP, duration)
}
}
func emitUnsupportedTest(testParam EthrTestParam) {
fmt.Printf("Error: \"%s\" test for \"%s\" is not supported.\n",
testToString(testParam.TestID.Type), protoToString(testParam.TestID.Protocol))
}
func validateTestParam(testParam EthrTestParam) bool {
testType := testParam.TestID.Type
protocol := testParam.TestID.Protocol
switch protocol {
case TCP:
if testType != Bandwidth && testType != Cps && testType != Latency {
emitUnsupportedTest(testParam)
return false
}
case UDP:
if testType != Bandwidth && testType != Pps {
emitUnsupportedTest(testParam)
return false
}
if testType == Bandwidth {
if testParam.BufferSize > (64 * 1024) {
fmt.Printf("Error: Maximum supported buffer size for UDP is 64K\n")
return false
}
}
case HTTP:
if testType != Bandwidth {
emitUnsupportedTest(testParam)
return false
}
default:
emitUnsupportedTest(testParam)
return false
}
return true
}