bbperf 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bbperf/.gitignore +3 -0
- bbperf/__init__.py +0 -0
- bbperf/bbperf.py +79 -0
- bbperf/calibration.py +40 -0
- bbperf/client.py +223 -0
- bbperf/const.py +27 -0
- bbperf/control_receiver_thread.py +109 -0
- bbperf/data_receiver_thread.py +142 -0
- bbperf/data_sender_thread.py +214 -0
- bbperf/data_udp_ping_sender_thread.py +45 -0
- bbperf/exceptions.py +5 -0
- bbperf/graph.py +37 -0
- bbperf/output.py +225 -0
- bbperf/run_test.sh +41 -0
- bbperf/server.py +184 -0
- bbperf/tcp-graph.gp +49 -0
- bbperf/tcp_control_connection_class.py +196 -0
- bbperf/udp-graph.gp +58 -0
- bbperf/util.py +105 -0
- bbperf-0.0.1.dist-info/METADATA +128 -0
- bbperf-0.0.1.dist-info/RECORD +24 -0
- bbperf-0.0.1.dist-info/WHEEL +4 -0
- bbperf-0.0.1.dist-info/entry_points.txt +2 -0
- bbperf-0.0.1.dist-info/licenses/LICENSE +14 -0
bbperf/.gitignore
ADDED
bbperf/__init__.py
ADDED
|
File without changes
|
bbperf/bbperf.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
#!/usr/bin/python3 -u
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2024 Cloudflare, Inc.
|
|
4
|
+
# Licensed under the Apache 2.0 license found in the LICENSE file or at https://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
|
|
8
|
+
from . import client
|
|
9
|
+
from . import server
|
|
10
|
+
from . import util
|
|
11
|
+
from . import const
|
|
12
|
+
|
|
13
|
+
def mainline():
|
|
14
|
+
|
|
15
|
+
parser = argparse.ArgumentParser(description="bbperf: end to end performance and bufferbloat measurement tool")
|
|
16
|
+
|
|
17
|
+
parser.add_argument("-v", "--verbosity",
|
|
18
|
+
action="count",
|
|
19
|
+
default=0,
|
|
20
|
+
help="increase output verbosity")
|
|
21
|
+
|
|
22
|
+
parser.add_argument("-s", "--server",
|
|
23
|
+
action="store_true",
|
|
24
|
+
default=False,
|
|
25
|
+
help="run in server mode")
|
|
26
|
+
|
|
27
|
+
parser.add_argument("-c", "--client",
|
|
28
|
+
metavar="SERVER_IP",
|
|
29
|
+
default=None,
|
|
30
|
+
help="run in client mode")
|
|
31
|
+
|
|
32
|
+
parser.add_argument("-p", "--port",
|
|
33
|
+
metavar="SERVER_PORT",
|
|
34
|
+
type=int,
|
|
35
|
+
default=const.SERVER_PORT,
|
|
36
|
+
help="server port (default: 5301)")
|
|
37
|
+
|
|
38
|
+
parser.add_argument("-R", "--reverse",
|
|
39
|
+
action="store_true",
|
|
40
|
+
default=False,
|
|
41
|
+
help="data flow in download direction (server to client)")
|
|
42
|
+
|
|
43
|
+
parser.add_argument("-t", "--time",
|
|
44
|
+
metavar="SECONDS",
|
|
45
|
+
type=int,
|
|
46
|
+
default=const.DURATION_SEC,
|
|
47
|
+
help="duration of run in seconds")
|
|
48
|
+
|
|
49
|
+
parser.add_argument("-u", "--udp",
|
|
50
|
+
action="store_true",
|
|
51
|
+
default=False,
|
|
52
|
+
help="run in UDP mode (default: TCP mode)")
|
|
53
|
+
|
|
54
|
+
parser.add_argument("-b", "--bandwidth",
|
|
55
|
+
default=None,
|
|
56
|
+
help="n[kmgKMG] | n[kmgKMG]pps")
|
|
57
|
+
|
|
58
|
+
parser.add_argument("-g", "--graph",
|
|
59
|
+
action="store_true",
|
|
60
|
+
default=False,
|
|
61
|
+
help="generate graph (requires gnuplot)")
|
|
62
|
+
|
|
63
|
+
parser.add_argument("-k", "--keep",
|
|
64
|
+
action="store_true",
|
|
65
|
+
default=False,
|
|
66
|
+
help="keep data file")
|
|
67
|
+
|
|
68
|
+
args = parser.parse_args()
|
|
69
|
+
|
|
70
|
+
util.validate_args(args)
|
|
71
|
+
|
|
72
|
+
if args.client:
|
|
73
|
+
client.client_mainline(args)
|
|
74
|
+
else:
|
|
75
|
+
server.server_mainline(args)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
if __name__ == '__main__':
|
|
79
|
+
mainline()
|
bbperf/calibration.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Copyright (c) 2024 Cloudflare, Inc.
|
|
2
|
+
# Licensed under the Apache 2.0 license found in the LICENSE file or at https://www.apache.org/licenses/LICENSE-2.0
|
|
3
|
+
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
from . import const
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
calibration_start_time = None
|
|
10
|
+
calibration_end_time = None
|
|
11
|
+
min_rtt_sec = None
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def start():
|
|
15
|
+
global calibration_start_time
|
|
16
|
+
global calibration_end_time
|
|
17
|
+
|
|
18
|
+
calibration_start_time = time.time()
|
|
19
|
+
calibration_end_time = calibration_start_time + const.UNLOADED_DURATION_SEC
|
|
20
|
+
|
|
21
|
+
def is_calibrated():
|
|
22
|
+
if calibration_end_time is None:
|
|
23
|
+
return False
|
|
24
|
+
|
|
25
|
+
if time.time() < calibration_end_time:
|
|
26
|
+
return False
|
|
27
|
+
|
|
28
|
+
return True
|
|
29
|
+
|
|
30
|
+
def update_rtt_sec(new_sample):
|
|
31
|
+
global min_rtt_sec
|
|
32
|
+
|
|
33
|
+
if (min_rtt_sec is None) or (new_sample < min_rtt_sec):
|
|
34
|
+
min_rtt_sec = new_sample
|
|
35
|
+
|
|
36
|
+
def get_unloaded_latency_rtt_sec():
|
|
37
|
+
if min_rtt_sec is None:
|
|
38
|
+
raise Exception("ERROR: there is no min rtt")
|
|
39
|
+
|
|
40
|
+
return min_rtt_sec
|
bbperf/client.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
#!/usr/bin/python3
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2024 Cloudflare, Inc.
|
|
4
|
+
# Licensed under the Apache 2.0 license found in the LICENSE file or at https://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
|
|
6
|
+
import multiprocessing
|
|
7
|
+
import time
|
|
8
|
+
import queue
|
|
9
|
+
import socket
|
|
10
|
+
|
|
11
|
+
from . import data_sender_thread
|
|
12
|
+
from . import data_udp_ping_sender_thread
|
|
13
|
+
from . import data_receiver_thread
|
|
14
|
+
from . import control_receiver_thread
|
|
15
|
+
from . import util
|
|
16
|
+
from . import calibration
|
|
17
|
+
from . import const
|
|
18
|
+
from . import output
|
|
19
|
+
from . import graph
|
|
20
|
+
|
|
21
|
+
from .tcp_control_connection_class import TcpControlConnectionClass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def client_mainline(args):
|
|
25
|
+
if args.verbosity:
|
|
26
|
+
print("args: {}".format(args))
|
|
27
|
+
|
|
28
|
+
server_ip = args.client
|
|
29
|
+
server_port = args.port
|
|
30
|
+
|
|
31
|
+
data_conn_verification_str = "d" + const.SOFT_SECRET + " "
|
|
32
|
+
|
|
33
|
+
# create control connection
|
|
34
|
+
|
|
35
|
+
if args.verbosity:
|
|
36
|
+
print("creating control connection")
|
|
37
|
+
|
|
38
|
+
control_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
39
|
+
control_sock.connect((server_ip, server_port))
|
|
40
|
+
|
|
41
|
+
control_conn = TcpControlConnectionClass(control_sock)
|
|
42
|
+
control_conn.set_args(args)
|
|
43
|
+
|
|
44
|
+
control_conn.send_validation_string()
|
|
45
|
+
|
|
46
|
+
if args.verbosity:
|
|
47
|
+
|
|
48
|
+
print("created control connection")
|
|
49
|
+
|
|
50
|
+
if args.verbosity:
|
|
51
|
+
print("sending args to server {}".format(vars(args)))
|
|
52
|
+
|
|
53
|
+
control_conn.send_args_to_server(args)
|
|
54
|
+
|
|
55
|
+
if args.verbosity:
|
|
56
|
+
print("sent args to server")
|
|
57
|
+
|
|
58
|
+
# create data connection
|
|
59
|
+
|
|
60
|
+
if args.verbosity:
|
|
61
|
+
print("creating data connection")
|
|
62
|
+
|
|
63
|
+
if args.udp:
|
|
64
|
+
data_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
65
|
+
data_sock.settimeout(const.SOCKET_TIMEOUT_SEC)
|
|
66
|
+
|
|
67
|
+
else:
|
|
68
|
+
data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
69
|
+
data_sock.connect((server_ip, server_port))
|
|
70
|
+
data_sock.settimeout(const.SOCKET_TIMEOUT_SEC)
|
|
71
|
+
|
|
72
|
+
# send verification string
|
|
73
|
+
num_bytes_sent = data_sock.send(data_conn_verification_str.encode())
|
|
74
|
+
if num_bytes_sent != len(data_conn_verification_str):
|
|
75
|
+
raise Exception("ERROR: send failed")
|
|
76
|
+
|
|
77
|
+
server_addr = (server_ip, server_port)
|
|
78
|
+
|
|
79
|
+
if args.verbosity:
|
|
80
|
+
print("created data connection ({})".format("udp" if args.udp else "tcp"))
|
|
81
|
+
|
|
82
|
+
# run test
|
|
83
|
+
|
|
84
|
+
if args.verbosity:
|
|
85
|
+
print("test running")
|
|
86
|
+
|
|
87
|
+
calibration.start()
|
|
88
|
+
|
|
89
|
+
if not args.reverse:
|
|
90
|
+
# up direction
|
|
91
|
+
|
|
92
|
+
control_receiver_stdout_queue = multiprocessing.Queue()
|
|
93
|
+
control_receiver_results_queue = multiprocessing.Queue()
|
|
94
|
+
|
|
95
|
+
control_receiver_process = multiprocessing.Process(
|
|
96
|
+
name = "controlreceiver",
|
|
97
|
+
target = control_receiver_thread.run_recv_term_queue,
|
|
98
|
+
args = (args, control_receiver_stdout_queue, control_conn, control_receiver_results_queue),
|
|
99
|
+
daemon = True)
|
|
100
|
+
|
|
101
|
+
control_receiver_process.start()
|
|
102
|
+
|
|
103
|
+
data_sender_stdout_queue = multiprocessing.Queue()
|
|
104
|
+
|
|
105
|
+
data_sender_process = multiprocessing.Process(
|
|
106
|
+
name = "datasender",
|
|
107
|
+
target = data_sender_thread.run,
|
|
108
|
+
args = (args, data_sender_stdout_queue, data_sock, server_addr),
|
|
109
|
+
daemon = True)
|
|
110
|
+
|
|
111
|
+
# test starts here
|
|
112
|
+
data_sender_process.start()
|
|
113
|
+
|
|
114
|
+
thread_list = []
|
|
115
|
+
thread_list.append(control_receiver_process)
|
|
116
|
+
thread_list.append(data_sender_process)
|
|
117
|
+
|
|
118
|
+
queue_list = []
|
|
119
|
+
queue_list.append([control_receiver_results_queue, output.print_output])
|
|
120
|
+
queue_list.append([control_receiver_stdout_queue, print])
|
|
121
|
+
queue_list.append([data_sender_stdout_queue, print])
|
|
122
|
+
|
|
123
|
+
if args.reverse:
|
|
124
|
+
# down direction
|
|
125
|
+
|
|
126
|
+
# udp pinger
|
|
127
|
+
if args.udp:
|
|
128
|
+
data_udp_ping_sender_stdout_queue = multiprocessing.Queue()
|
|
129
|
+
|
|
130
|
+
data_udp_ping_sender_process = multiprocessing.Process(
|
|
131
|
+
name = "dataudppingsender",
|
|
132
|
+
target = data_udp_ping_sender_thread.run,
|
|
133
|
+
args = (args, data_udp_ping_sender_stdout_queue, data_sock, server_addr),
|
|
134
|
+
daemon = True)
|
|
135
|
+
|
|
136
|
+
data_udp_ping_sender_process.start()
|
|
137
|
+
|
|
138
|
+
# yield to let the first ping fly
|
|
139
|
+
time.sleep(0.005)
|
|
140
|
+
|
|
141
|
+
data_receiver_stdout_queue = multiprocessing.Queue()
|
|
142
|
+
|
|
143
|
+
data_receiver_process = multiprocessing.Process(
|
|
144
|
+
name = "datareceiver",
|
|
145
|
+
target = data_receiver_thread.run,
|
|
146
|
+
args = (args, data_receiver_stdout_queue, control_conn, data_sock, server_addr),
|
|
147
|
+
daemon = True)
|
|
148
|
+
|
|
149
|
+
data_receiver_process.start()
|
|
150
|
+
|
|
151
|
+
control_receiver_stdout_queue = multiprocessing.Queue()
|
|
152
|
+
control_receiver_results_queue = multiprocessing.Queue()
|
|
153
|
+
|
|
154
|
+
control_receiver_process = multiprocessing.Process(
|
|
155
|
+
name = "controlreceiver",
|
|
156
|
+
target = control_receiver_thread.run_recv_queue,
|
|
157
|
+
args = (args, control_receiver_stdout_queue, control_conn, control_receiver_results_queue),
|
|
158
|
+
daemon = True)
|
|
159
|
+
|
|
160
|
+
control_receiver_process.start()
|
|
161
|
+
|
|
162
|
+
# test starts here
|
|
163
|
+
|
|
164
|
+
control_conn.send_start_message_to_server()
|
|
165
|
+
|
|
166
|
+
thread_list = []
|
|
167
|
+
thread_list.append(data_receiver_process)
|
|
168
|
+
thread_list.append(control_receiver_process)
|
|
169
|
+
if args.udp:
|
|
170
|
+
thread_list.append(data_udp_ping_sender_process)
|
|
171
|
+
|
|
172
|
+
queue_list = []
|
|
173
|
+
if args.udp:
|
|
174
|
+
queue_list.append([data_udp_ping_sender_stdout_queue, print])
|
|
175
|
+
queue_list.append([data_receiver_stdout_queue, print])
|
|
176
|
+
queue_list.append([control_receiver_stdout_queue, print])
|
|
177
|
+
queue_list.append([control_receiver_results_queue, output.print_output])
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# output loop
|
|
181
|
+
|
|
182
|
+
output.init(args)
|
|
183
|
+
|
|
184
|
+
while True:
|
|
185
|
+
queue_was_processed = False
|
|
186
|
+
|
|
187
|
+
for queue_to_read, function_to_call in queue_list:
|
|
188
|
+
try:
|
|
189
|
+
s1 = queue_to_read.get_nowait()
|
|
190
|
+
queue_was_processed = True
|
|
191
|
+
function_to_call(s1)
|
|
192
|
+
except queue.Empty:
|
|
193
|
+
pass
|
|
194
|
+
|
|
195
|
+
if queue_was_processed:
|
|
196
|
+
# immediately loop again
|
|
197
|
+
continue
|
|
198
|
+
|
|
199
|
+
if util.threads_are_running(thread_list):
|
|
200
|
+
# nothing in queues, but test is still running
|
|
201
|
+
time.sleep(0.01)
|
|
202
|
+
continue
|
|
203
|
+
|
|
204
|
+
# exit program
|
|
205
|
+
break
|
|
206
|
+
|
|
207
|
+
output.term()
|
|
208
|
+
|
|
209
|
+
util.done_with_socket(data_sock)
|
|
210
|
+
control_conn.close()
|
|
211
|
+
|
|
212
|
+
graphdatafilename = output.get_graph_data_file_name()
|
|
213
|
+
rawdatafilename = output.get_raw_data_file_name()
|
|
214
|
+
|
|
215
|
+
if args.graph:
|
|
216
|
+
graph.create_graph(args, graphdatafilename)
|
|
217
|
+
print("created graph: {}".format(graphdatafilename + ".png"))
|
|
218
|
+
|
|
219
|
+
if args.keep:
|
|
220
|
+
print("keeping graph data file: {}".format(graphdatafilename))
|
|
221
|
+
print("keeping raw data file: {}".format(rawdatafilename))
|
|
222
|
+
else:
|
|
223
|
+
output.delete_data_files()
|
bbperf/const.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Copyright (c) 2024 Cloudflare, Inc.
|
|
2
|
+
# Licensed under the Apache 2.0 license found in the LICENSE file or at https://www.apache.org/licenses/LICENSE-2.0
|
|
3
|
+
|
|
4
|
+
# defaults
|
|
5
|
+
SERVER_PORT = 5301
|
|
6
|
+
DURATION_SEC = 10
|
|
7
|
+
UNLOADED_DURATION_SEC=5
|
|
8
|
+
|
|
9
|
+
# for socket recv()
|
|
10
|
+
BUFSZ = 4096
|
|
11
|
+
|
|
12
|
+
PAYLOAD_1K = b'a'*1024
|
|
13
|
+
PAYLOAD_4K = b'a'*4096
|
|
14
|
+
|
|
15
|
+
SOFT_SECRET = "ZZ32gkogidrueowiruvnmxn432874ZZ"
|
|
16
|
+
|
|
17
|
+
# sample interval time
|
|
18
|
+
SAMPLE_INTERVAL_SEC = 0.1
|
|
19
|
+
STDOUT_INTERVAL_SEC = 1
|
|
20
|
+
|
|
21
|
+
RATE_LIMITED_BATCH_SIZE_PKTS_UDP_PKTS = 20
|
|
22
|
+
RATE_LIMITED_BATCH_SIZE_PKTS_TCP_PKTS = 5
|
|
23
|
+
|
|
24
|
+
START_MSG = " start "
|
|
25
|
+
UDP_PING_MSG = "ping"
|
|
26
|
+
|
|
27
|
+
SOCKET_TIMEOUT_SEC=20
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Copyright (c) 2024 Cloudflare, Inc.
|
|
2
|
+
# Licensed under the Apache 2.0 license found in the LICENSE file or at https://www.apache.org/licenses/LICENSE-2.0
|
|
3
|
+
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
from .exceptions import PeerDisconnectedException
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# falling off the end of this method terminates the process
|
|
10
|
+
def run_recv_term_queue(args, stdout_queue, control_conn, results_queue):
|
|
11
|
+
if args.verbosity:
|
|
12
|
+
stdout_queue.put("starting control receiver process: run_recv_term_queue")
|
|
13
|
+
|
|
14
|
+
while True:
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
# blocking
|
|
18
|
+
bytes_read = control_conn.recv_a_c_block()
|
|
19
|
+
|
|
20
|
+
except ConnectionResetError:
|
|
21
|
+
if args.verbosity:
|
|
22
|
+
stdout_queue.put("connection reset error")
|
|
23
|
+
# exit process
|
|
24
|
+
break
|
|
25
|
+
|
|
26
|
+
except PeerDisconnectedException:
|
|
27
|
+
if args.verbosity:
|
|
28
|
+
stdout_queue.put("peer disconnected (control socket)")
|
|
29
|
+
# exit process
|
|
30
|
+
break
|
|
31
|
+
|
|
32
|
+
received_str = bytes_read.decode()
|
|
33
|
+
curr_time_str = str(time.time())
|
|
34
|
+
new_str = received_str + curr_time_str + " d "
|
|
35
|
+
|
|
36
|
+
results_queue.put(new_str)
|
|
37
|
+
|
|
38
|
+
control_conn.close()
|
|
39
|
+
|
|
40
|
+
if args.verbosity:
|
|
41
|
+
stdout_queue.put("exiting control receiver process: run_recv_term_queue")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# falling off the end of this method terminates the process
|
|
45
|
+
def run_recv_term_send(args, stdout_queue, control_conn):
|
|
46
|
+
if args.verbosity:
|
|
47
|
+
stdout_queue.put("starting control receiver process: run_recv_term_send")
|
|
48
|
+
|
|
49
|
+
while True:
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
# blocking
|
|
53
|
+
bytes_read = control_conn.recv_a_c_block()
|
|
54
|
+
|
|
55
|
+
except ConnectionResetError:
|
|
56
|
+
if args.verbosity:
|
|
57
|
+
stdout_queue.put("connection reset error")
|
|
58
|
+
# exit process
|
|
59
|
+
break
|
|
60
|
+
|
|
61
|
+
except PeerDisconnectedException:
|
|
62
|
+
if args.verbosity:
|
|
63
|
+
stdout_queue.put("peer disconnected (control socket)")
|
|
64
|
+
# exit process
|
|
65
|
+
break
|
|
66
|
+
|
|
67
|
+
received_str = bytes_read.decode()
|
|
68
|
+
curr_time_str = str(time.time())
|
|
69
|
+
new_str = received_str + curr_time_str + " d "
|
|
70
|
+
|
|
71
|
+
control_conn.send(new_str.encode())
|
|
72
|
+
|
|
73
|
+
control_conn.close()
|
|
74
|
+
|
|
75
|
+
if args.verbosity:
|
|
76
|
+
stdout_queue.put("exiting control receiver process: run_recv_term_send")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# falling off the end of this method terminates the process
|
|
80
|
+
def run_recv_queue(args, stdout_queue, control_conn, results_queue):
|
|
81
|
+
if args.verbosity:
|
|
82
|
+
stdout_queue.put("starting control receiver process: run_recv_queue")
|
|
83
|
+
|
|
84
|
+
while True:
|
|
85
|
+
try:
|
|
86
|
+
# blocking
|
|
87
|
+
bytes_read = control_conn.recv_a_d_block()
|
|
88
|
+
|
|
89
|
+
except ConnectionResetError:
|
|
90
|
+
if args.verbosity:
|
|
91
|
+
stdout_queue.put("connection reset error")
|
|
92
|
+
# exit process
|
|
93
|
+
break
|
|
94
|
+
|
|
95
|
+
except PeerDisconnectedException:
|
|
96
|
+
if args.verbosity:
|
|
97
|
+
stdout_queue.put("peer disconnected (control socket)")
|
|
98
|
+
# exit process
|
|
99
|
+
break
|
|
100
|
+
|
|
101
|
+
received_str = bytes_read.decode()
|
|
102
|
+
|
|
103
|
+
# passthru as is
|
|
104
|
+
results_queue.put(received_str)
|
|
105
|
+
|
|
106
|
+
control_conn.close()
|
|
107
|
+
|
|
108
|
+
if args.verbosity:
|
|
109
|
+
stdout_queue.put("exiting control receiver process: run_recv_queue")
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# Copyright (c) 2024 Cloudflare, Inc.
|
|
2
|
+
# Licensed under the Apache 2.0 license found in the LICENSE file or at https://www.apache.org/licenses/LICENSE-2.0
|
|
3
|
+
|
|
4
|
+
import time
|
|
5
|
+
import socket
|
|
6
|
+
|
|
7
|
+
from . import const
|
|
8
|
+
from . import util
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def run(args, stdout_queue, control_conn, data_sock, peer_addr):
|
|
12
|
+
|
|
13
|
+
if args.verbosity:
|
|
14
|
+
stdout_queue.put("starting data receiver process")
|
|
15
|
+
|
|
16
|
+
# do not block on the below recv calls as we want to exit the process when
|
|
17
|
+
# the "end of run" timer expires even if the flow of packets has stopped
|
|
18
|
+
data_sock.settimeout(0.05)
|
|
19
|
+
|
|
20
|
+
peer_addr_for_udp = peer_addr
|
|
21
|
+
|
|
22
|
+
end_time = None
|
|
23
|
+
|
|
24
|
+
total_recv_calls = 0
|
|
25
|
+
|
|
26
|
+
interval_start_time = time.time()
|
|
27
|
+
interval_end_time = interval_start_time + const.SAMPLE_INTERVAL_SEC
|
|
28
|
+
|
|
29
|
+
interval_pkts_received = 0
|
|
30
|
+
interval_bytes_received = 0
|
|
31
|
+
|
|
32
|
+
# do until end of test duration
|
|
33
|
+
# we will not get a connection close with udp
|
|
34
|
+
while True:
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
num_bytes_read = 0
|
|
38
|
+
if args.udp:
|
|
39
|
+
# recv with timeout
|
|
40
|
+
bytes_read, pkt_from_addr = data_sock.recvfrom(const.BUFSZ)
|
|
41
|
+
|
|
42
|
+
# validate peer address
|
|
43
|
+
# all subsequent packets should come from the same addr as the first one
|
|
44
|
+
if pkt_from_addr != peer_addr_for_udp:
|
|
45
|
+
if peer_addr_for_udp is None:
|
|
46
|
+
# first packet is received
|
|
47
|
+
peer_addr_for_udp = pkt_from_addr
|
|
48
|
+
print("peer address: {}".format(peer_addr_for_udp))
|
|
49
|
+
else:
|
|
50
|
+
raise Exception("ERROR: datagram from wrong peer address")
|
|
51
|
+
|
|
52
|
+
num_bytes_read = len(bytes_read)
|
|
53
|
+
|
|
54
|
+
else:
|
|
55
|
+
# tcp
|
|
56
|
+
# recv with timeout
|
|
57
|
+
bytes_read = data_sock.recv(const.BUFSZ)
|
|
58
|
+
|
|
59
|
+
num_bytes_read = len(bytes_read)
|
|
60
|
+
|
|
61
|
+
if num_bytes_read == 0:
|
|
62
|
+
# peer has disconnected
|
|
63
|
+
if args.verbosity:
|
|
64
|
+
stdout_queue.put("peer disconnected (data socket)")
|
|
65
|
+
# exit process
|
|
66
|
+
break
|
|
67
|
+
|
|
68
|
+
except socket.timeout:
|
|
69
|
+
pass
|
|
70
|
+
|
|
71
|
+
total_recv_calls += 1
|
|
72
|
+
|
|
73
|
+
curr_time_sec = time.time()
|
|
74
|
+
|
|
75
|
+
if end_time and (curr_time_sec > end_time):
|
|
76
|
+
if args.verbosity:
|
|
77
|
+
stdout_queue.put("data receiver thread: end of test, exiting")
|
|
78
|
+
# exit process
|
|
79
|
+
break
|
|
80
|
+
|
|
81
|
+
if num_bytes_read == 0:
|
|
82
|
+
# recv must have timed out, skip the rest of this loop
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
if not end_time:
|
|
86
|
+
# first data packet arrived, so test just started running
|
|
87
|
+
end_time = curr_time_sec + args.time
|
|
88
|
+
|
|
89
|
+
interval_pkts_received += 1 # valid for udp only
|
|
90
|
+
interval_bytes_received += num_bytes_read
|
|
91
|
+
|
|
92
|
+
if curr_time_sec > interval_end_time:
|
|
93
|
+
interval_time_sec = curr_time_sec - interval_start_time
|
|
94
|
+
|
|
95
|
+
# find the packet send time in the user payload
|
|
96
|
+
|
|
97
|
+
a_b_block = None
|
|
98
|
+
|
|
99
|
+
idx_of_a = bytes_read.find(b' a ')
|
|
100
|
+
if idx_of_a > -1:
|
|
101
|
+
idx_of_b = bytes_read.find(b' b ', idx_of_a)
|
|
102
|
+
if idx_of_b > -1:
|
|
103
|
+
a_b_block = bytes_read[ idx_of_a : idx_of_b + 3 ]
|
|
104
|
+
|
|
105
|
+
if a_b_block is None:
|
|
106
|
+
# skip sending for this packet, but stay "in" sample interval
|
|
107
|
+
continue
|
|
108
|
+
|
|
109
|
+
# sending info back to client on control connection
|
|
110
|
+
|
|
111
|
+
ba = bytearray()
|
|
112
|
+
ba.extend(a_b_block)
|
|
113
|
+
ba.extend(str(interval_time_sec).encode())
|
|
114
|
+
ba.extend(b' ')
|
|
115
|
+
ba.extend(str(interval_pkts_received).encode())
|
|
116
|
+
ba.extend(b' ')
|
|
117
|
+
ba.extend(str(interval_bytes_received).encode())
|
|
118
|
+
ba.extend(b' ')
|
|
119
|
+
ba.extend(str(total_recv_calls).encode()) # num of pkts received, valid for udp only
|
|
120
|
+
ba.extend(b' c ')
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
control_conn.send(ba)
|
|
124
|
+
|
|
125
|
+
except Exception:
|
|
126
|
+
stdout_queue.put("ERROR: send on control socket failed 2")
|
|
127
|
+
# exit process
|
|
128
|
+
break
|
|
129
|
+
|
|
130
|
+
interval_bytes_received = 0
|
|
131
|
+
interval_pkts_received = 0
|
|
132
|
+
|
|
133
|
+
interval_start_time = curr_time_sec
|
|
134
|
+
interval_end_time = interval_start_time + const.SAMPLE_INTERVAL_SEC
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# peer disconnected (or an error)
|
|
138
|
+
util.done_with_socket(data_sock)
|
|
139
|
+
control_conn.close()
|
|
140
|
+
|
|
141
|
+
if args.verbosity:
|
|
142
|
+
stdout_queue.put("exiting data receiver process")
|