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/server.py ADDED
@@ -0,0 +1,184 @@
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 time
7
+ import socket
8
+ import multiprocessing
9
+ import queue
10
+
11
+ from . import data_sender_thread
12
+ from . import data_receiver_thread
13
+ from . import control_receiver_thread
14
+ from . import util
15
+ from . import const
16
+ from . import calibration
17
+
18
+ from .tcp_control_connection_class import TcpControlConnectionClass
19
+
20
+
21
+ def server_mainline(args):
22
+ server_port = args.port
23
+
24
+ listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25
+
26
+ listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
27
+
28
+ listen_sock.bind(('0.0.0.0', server_port))
29
+
30
+ listen_sock.listen(32) # listen backlog
31
+ listen_sock.setblocking(True)
32
+
33
+ server_port = listen_sock.getsockname()[1]
34
+
35
+ while True:
36
+ print("server listening on port ", server_port)
37
+
38
+ data_conn_verification_str = "d" + const.SOFT_SECRET + " "
39
+
40
+ # accept control connection
41
+
42
+ # blocking
43
+ control_sock, _ = listen_sock.accept()
44
+
45
+ control_conn = TcpControlConnectionClass(control_sock)
46
+ control_conn.set_args(args)
47
+
48
+ print("client connected (control socket)")
49
+
50
+ control_conn.recv_and_check_validation_string()
51
+
52
+ print("waiting for args from client")
53
+
54
+ # blocking
55
+ client_args = control_conn.receive_args_from_client()
56
+
57
+ if client_args.verbosity:
58
+ print("received args from client: {}".format(vars(client_args)))
59
+ else:
60
+ print("received args from client")
61
+
62
+ control_conn.set_args(client_args)
63
+
64
+ # accept data connection
65
+
66
+ if client_args.udp:
67
+
68
+ if client_args.verbosity:
69
+ print("creating data connection (udp)")
70
+
71
+ data_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
72
+ data_sock.bind(("0.0.0.0", server_port))
73
+
74
+ else:
75
+
76
+ if client_args.verbosity:
77
+ print("creating data connection (tcp)")
78
+
79
+ # blocking
80
+ data_sock, _ = listen_sock.accept()
81
+
82
+ data_sock.settimeout(const.SOCKET_TIMEOUT_SEC)
83
+
84
+ # perform verification of data connection
85
+ if not client_args.udp:
86
+ # blocking
87
+ total_num_bytes_to_read = len(data_conn_verification_str)
88
+ payload_bytes = util.recv_exact_num_bytes_tcp(data_sock, total_num_bytes_to_read)
89
+ payload_str = payload_bytes.decode()
90
+ if payload_str != data_conn_verification_str:
91
+ raise Exception("ERROR: client connection invalid, ident: {} payload {}".format(data_conn_verification_str[0], payload_str))
92
+
93
+ print("created data connection ({})".format("udp" if client_args.udp else "tcp"))
94
+
95
+ # run test
96
+
97
+ print("test running")
98
+
99
+ calibration.start()
100
+
101
+ if not client_args.reverse:
102
+ # direction up
103
+
104
+ data_receiver_stdout_queue = multiprocessing.Queue()
105
+
106
+ client_addr = None
107
+
108
+ data_receiver_process = multiprocessing.Process(
109
+ name = "datareceiver",
110
+ target = data_receiver_thread.run,
111
+ args = (client_args, data_receiver_stdout_queue, control_conn, data_sock, client_addr),
112
+ daemon = True)
113
+
114
+ data_receiver_process.start()
115
+
116
+ thread_list = []
117
+ thread_list.append(data_receiver_process)
118
+
119
+ queue_list = []
120
+ queue_list.append([data_receiver_stdout_queue, print])
121
+
122
+ if client_args.reverse:
123
+ # direction down
124
+
125
+ control_receiver_stdout_queue = multiprocessing.Queue()
126
+
127
+ control_receiver_process = multiprocessing.Process(
128
+ name = "controlreceiver",
129
+ target = control_receiver_thread.run_recv_term_send,
130
+ args = (client_args, control_receiver_stdout_queue, control_conn),
131
+ daemon = True)
132
+
133
+ data_sender_stdout_queue = multiprocessing.Queue()
134
+
135
+ data_sender_process = multiprocessing.Process(
136
+ name = "datasender",
137
+ target = data_sender_thread.run,
138
+ args = (client_args, data_sender_stdout_queue, data_sock, None),
139
+ daemon = True)
140
+
141
+ # wait for start message
142
+ control_conn.wait_for_start_message()
143
+
144
+ control_receiver_process.start()
145
+
146
+ data_sender_process.start()
147
+
148
+ thread_list = []
149
+ thread_list.append(control_receiver_process)
150
+ thread_list.append(data_sender_process)
151
+
152
+ queue_list = []
153
+ queue_list.append([control_receiver_stdout_queue, print])
154
+ queue_list.append([data_sender_stdout_queue, print])
155
+
156
+ # both up and down
157
+
158
+ while True:
159
+ queue_was_processed = False
160
+
161
+ for queue_to_read, function_to_call in queue_list:
162
+ try:
163
+ s1 = queue_to_read.get_nowait()
164
+ queue_was_processed = True
165
+ function_to_call(s1)
166
+ except queue.Empty:
167
+ pass
168
+
169
+ if queue_was_processed:
170
+ # immediately loop again
171
+ continue
172
+
173
+ if util.threads_are_running(thread_list):
174
+ # nothing in queues, but test is still running
175
+ time.sleep(0.01)
176
+ continue
177
+
178
+ # nothing in queues, and test has ended
179
+ break
180
+
181
+ util.done_with_socket(data_sock)
182
+ control_conn.close()
183
+
184
+ print("client ended")
bbperf/tcp-graph.gp ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/gnuplot
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
+ #datafile1 = "/tmp/bbperf-tcp-data-j9xh25q3"
7
+
8
+ pngfile1 = datafile1.".png"
9
+
10
+ set grid
11
+
12
+ set key right top
13
+ set key box opaque
14
+
15
+ set style data lines
16
+
17
+ # noenhanced to avoid need to escape underscores in labels
18
+ set terminal pngcairo size 1200,1000 noenhanced
19
+ set output pngfile1
20
+
21
+ # generate stats for column 2
22
+ # nooutput - do not sent to "screen"
23
+ # name - prefix
24
+ stats datafile1 using 1 nooutput name "XRANGE"
25
+
26
+ set multiplot title "bbperf TCP ".datafile1 layout 3,1
27
+
28
+ set lmargin 12
29
+
30
+ # dt 1 (solid), dt 2 (dotted), dt 4 (dot dash)
31
+ # lc 1 (purple), lc 4 (orange), lc 6 (blue), lc 7 (red), lc 8 (black)
32
+
33
+ set ylabel "Mbps"
34
+
35
+ plot datafile1 using ($1-XRANGE_min):6 title "receiver throughput (L7)" lw 2 lc 6, \
36
+ "" using ($1-XRANGE_min):4 title "sender throughput (L7)" lw 2 lc 1
37
+
38
+ set ylabel "ms"
39
+
40
+ plot datafile1 using ($1-XRANGE_min):7 title "unloaded RTT (L7)" lw 2 lc 1, \
41
+ "" using ($1-XRANGE_min):8 title "RTT (L7)" lw 2 lc 6
42
+
43
+ set ylabel "bytes"
44
+
45
+ plot datafile1 using ($1-XRANGE_min):9 title "BDP" lw 2 lc 1, \
46
+ "" using ($1-XRANGE_min):10 title "buffered data" lw 2 lc 6
47
+
48
+ unset multiplot
49
+
@@ -0,0 +1,196 @@
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 json
5
+ import argparse
6
+
7
+ from . import const
8
+ from . import util
9
+ from .exceptions import PeerDisconnectedException
10
+
11
+
12
+ class TcpControlConnectionClass:
13
+
14
+ # class variables
15
+
16
+ validation_str = "c" + const.SOFT_SECRET + " "
17
+
18
+
19
+ def __init__(self, control_sock):
20
+ self.control_sock = control_sock
21
+ self.args = None
22
+
23
+ self.read_buffer = bytearray()
24
+
25
+ control_sock.settimeout(const.SOCKET_TIMEOUT_SEC)
26
+
27
+
28
+ def set_args(self, args):
29
+ self.args = args
30
+
31
+
32
+ def send(self, payload_bytes):
33
+
34
+ num_bytes_sent = self.control_sock.send(payload_bytes)
35
+
36
+ num_payload_bytes = len(payload_bytes)
37
+
38
+ if num_bytes_sent != num_payload_bytes:
39
+ raise Exception("ERROR: send failed: wrong number of bytes sent: expected {}, actual {}".format(
40
+ num_payload_bytes,
41
+ num_bytes_sent
42
+ ))
43
+
44
+ if self.args and self.args.verbosity == 3:
45
+ print("control connection: send: {}".format(payload_bytes.decode()))
46
+
47
+
48
+ def send_validation_string(self):
49
+ self.send(self.validation_str.encode())
50
+
51
+
52
+ def send_args_to_server(self, args):
53
+ args_json = json.dumps(vars(args))
54
+ self.send(args_json.encode())
55
+
56
+
57
+ def send_start_message_to_server(self):
58
+ if self.args.verbosity:
59
+ print("sending start message")
60
+
61
+ self.send(const.START_MSG.encode())
62
+
63
+
64
+ def recv(self, num_bytes_to_read):
65
+
66
+ # blocking
67
+ recv_bytes = self.control_sock.recv(num_bytes_to_read)
68
+
69
+ if len(recv_bytes) == 0:
70
+ raise PeerDisconnectedException()
71
+
72
+ if self.args and self.args.verbosity == 3:
73
+ print("control connection: recv: {}".format("*" + recv_bytes.decode() + "*"))
74
+
75
+ return recv_bytes
76
+
77
+
78
+ def recv_into_buffer_until_minimum_size(self, minimum_buffer_size):
79
+
80
+ while True:
81
+ if len(self.read_buffer) >= minimum_buffer_size:
82
+ break
83
+
84
+ num_bytes_remaining = minimum_buffer_size - len(self.read_buffer)
85
+
86
+ # blocking
87
+ recv_bytes = self.recv(num_bytes_remaining)
88
+
89
+ self.read_buffer.extend(recv_bytes)
90
+
91
+
92
+ def recv_into_buffer_until_substr_found(self, substr_bytes):
93
+
94
+ while True:
95
+
96
+ substr_idx = self.read_buffer.find(substr_bytes)
97
+ if substr_idx > -1:
98
+ # found
99
+ break
100
+
101
+ # blocking
102
+ recv_bytes = self.recv(const.BUFSZ)
103
+
104
+ self.read_buffer.extend(recv_bytes)
105
+
106
+ return substr_idx
107
+
108
+
109
+ def recv_and_check_validation_string(self):
110
+ len_str = len(self.validation_str)
111
+
112
+ self.recv_into_buffer_until_minimum_size(len_str)
113
+
114
+ received_bytes = self.read_buffer[ 0 : len_str ]
115
+ self.read_buffer = self.read_buffer[ len_str : ]
116
+
117
+ received_str = received_bytes.decode()
118
+
119
+ if received_str != self.validation_str:
120
+ raise Exception("ERROR: client connection invalid, ident: {} payload {}".format(self.validation_str[0], received_str))
121
+
122
+
123
+ def receive_args_from_client(self):
124
+ # starts with "{" and ends with "}"
125
+
126
+ # blocking
127
+ substr_idx = self.recv_into_buffer_until_substr_found(b'}')
128
+
129
+ received_bytes = self.read_buffer[ 0 : substr_idx + 1 ]
130
+ self.read_buffer = self.read_buffer[ substr_idx + 1 : ]
131
+
132
+ received_str = received_bytes.decode()
133
+
134
+ args_d = json.loads(received_str)
135
+
136
+ # recreate args as if it came directly from argparse
137
+ args = argparse.Namespace(**args_d)
138
+
139
+ return args
140
+
141
+
142
+ def wait_for_start_message(self):
143
+ if self.args.verbosity:
144
+ print("waiting for start message")
145
+
146
+ len_str = len(const.START_MSG)
147
+
148
+ # blocking
149
+ self.recv_into_buffer_until_minimum_size(len_str)
150
+
151
+ received_bytes = self.read_buffer[ 0 : len_str ]
152
+ self.read_buffer = self.read_buffer[ len_str : ]
153
+
154
+ received_str = received_bytes.decode()
155
+
156
+ if received_str != const.START_MSG:
157
+ raise Exception("ERROR: failed to receive start message")
158
+
159
+ if self.args.verbosity:
160
+ print("received start message")
161
+
162
+
163
+ def recv_a_c_block(self):
164
+ start_bytes = b' a '
165
+ end_bytes = b' c '
166
+
167
+ # blocking
168
+ substr_idx = self.recv_into_buffer_until_substr_found(end_bytes)
169
+
170
+ received_bytes = self.read_buffer[ 0 : substr_idx + 3 ]
171
+ self.read_buffer = self.read_buffer[ substr_idx + 3 : ]
172
+
173
+ if not (received_bytes.startswith(start_bytes) and received_bytes.endswith(end_bytes)):
174
+ raise Exception("recv_a_c_block failed")
175
+
176
+ return received_bytes
177
+
178
+
179
+ def recv_a_d_block(self):
180
+ start_bytes = b' a '
181
+ end_bytes = b' d '
182
+
183
+ # blocking
184
+ substr_idx = self.recv_into_buffer_until_substr_found(end_bytes)
185
+
186
+ received_bytes = self.read_buffer[ 0 : substr_idx + 3 ]
187
+ self.read_buffer = self.read_buffer[ substr_idx + 3 : ]
188
+
189
+ if not (received_bytes.startswith(start_bytes) and received_bytes.endswith(end_bytes)):
190
+ raise Exception("recv_a_d_block failed")
191
+
192
+ return received_bytes
193
+
194
+
195
+ def close(self):
196
+ util.done_with_socket(self.control_sock)
bbperf/udp-graph.gp ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/gnuplot
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
+ #datafile1 = "/tmp/bbperf-udp-data-aa97m9xl"
7
+
8
+ pngfile1 = datafile1.".png"
9
+
10
+ set grid
11
+
12
+ set key right top
13
+ set key box opaque
14
+
15
+ set style data lines
16
+
17
+ # noenhanced to avoid need to escape underscores in labels
18
+ set terminal pngcairo size 1200,1300 noenhanced
19
+ set output pngfile1
20
+
21
+ # generate stats for column 2
22
+ # nooutput - do not sent to "screen"
23
+ # name - prefix
24
+ stats datafile1 using 1 nooutput name "XRANGE"
25
+
26
+ set multiplot title "bbperf UDP ".datafile1 layout 5,1
27
+
28
+ set lmargin 12
29
+
30
+ # dt 1 (solid), dt 2 (dotted), dt 4 (dot dash)
31
+ # lc 1 (purple), lc 4 (orange), lc 6 (blue), lc 7 (red), lc 8 (black)
32
+
33
+ set ylabel "pps"
34
+
35
+ plot datafile1 using ($1-XRANGE_min):5 title "receiver pps" lw 2 lc 6, \
36
+ "" using ($1-XRANGE_min):3 title "sender pps" lw 2 lc 1 dt 2
37
+
38
+ set ylabel "Mbps"
39
+
40
+ plot datafile1 using ($1-XRANGE_min):6 title "receiver throughput" lw 2 lc 6, \
41
+ "" using ($1-XRANGE_min):4 title "sender throughput" lw 2 lc 1 dt 2
42
+
43
+ set ylabel "ms"
44
+
45
+ plot datafile1 using ($1-XRANGE_min):7 title "unloaded RTT" lw 2 lc 1, \
46
+ "" using ($1-XRANGE_min):8 title "RTT" lw 2 lc 6
47
+
48
+ set ylabel "bytes"
49
+
50
+ plot datafile1 using ($1-XRANGE_min):9 title "BDP" lw 2 lc 1, \
51
+ "" using ($1-XRANGE_min):10 title "buffered data" lw 2 lc 6
52
+
53
+ set ylabel "percent"
54
+
55
+ plot datafile1 using ($1-XRANGE_min):13 title "pkt loss %" lw 2 lc 6
56
+
57
+ unset multiplot
58
+
bbperf/util.py ADDED
@@ -0,0 +1,105 @@
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 socket
5
+ import ipaddress
6
+
7
+ from .exceptions import PeerDisconnectedException
8
+
9
+
10
+ def validate_args(args):
11
+ if args.server and args.client:
12
+ raise Exception("ERROR: cannot be both client and server")
13
+
14
+ if args.client:
15
+ # this is server IP address
16
+ # allow any exception here to propogate up
17
+ ipaddress.ip_address(args.client)
18
+
19
+ if args.port > 65535:
20
+ raise Exception("ERROR: invalid server port {}".format(args.port))
21
+
22
+ if args.time <= 0:
23
+ raise Exception("ERROR: argument time must be positive")
24
+
25
+ if args.time < 10:
26
+ raise Exception("ERROR: time must be at least 10 seconds")
27
+
28
+ if args.udp and not args.bandwidth:
29
+ raise Exception("ERROR: bandwidth option must be specified for UDP mode")
30
+
31
+
32
+ def convert_bandwidth_str_to_int(arg_str):
33
+ # input string:
34
+ # n[kmgKMG] | n[kmgKMG]pps
35
+
36
+ idx = arg_str.find("pps")
37
+ if idx > -1:
38
+ # found, so pps
39
+ is_pps = True
40
+ human_num_str = arg_str[:idx]
41
+ else:
42
+ # bps
43
+ is_pps = False
44
+ human_num_str = arg_str
45
+
46
+ last_char = human_num_str[-1:]
47
+
48
+ if last_char in [ "k", "K" ]:
49
+ ret_val = int(human_num_str[0:-1]) * (10 ** 3)
50
+
51
+ elif last_char in [ "m", "M" ]:
52
+ ret_val = int(human_num_str[0:-1]) * (10 ** 6)
53
+
54
+ elif last_char in [ "g", "G" ]:
55
+ ret_val = int(human_num_str[0:-1]) * (10 ** 9)
56
+
57
+ else:
58
+ ret_val = int(human_num_str)
59
+
60
+ return is_pps, ret_val
61
+
62
+
63
+ def done_with_socket(mysock):
64
+ try:
65
+ mysock.shutdown(socket.SHUT_RDWR)
66
+ except:
67
+ pass
68
+
69
+ try:
70
+ mysock.close()
71
+ except:
72
+ pass
73
+
74
+
75
+ def threads_are_running(thread_list):
76
+ any_running = False
77
+
78
+ for t in thread_list:
79
+ if t.is_alive():
80
+ any_running = True
81
+
82
+ return any_running
83
+
84
+
85
+ def recv_exact_num_bytes_tcp(client_sock, total_num_bytes_to_read):
86
+ payload_bytes = bytearray()
87
+ num_bytes_read = 0
88
+
89
+ while num_bytes_read < total_num_bytes_to_read:
90
+
91
+ num_bytes_remaining = total_num_bytes_to_read - num_bytes_read
92
+
93
+ # blocking
94
+ recv_bytes = client_sock.recv(num_bytes_remaining)
95
+
96
+ num_bytes_received = len(recv_bytes)
97
+
98
+ if num_bytes_received == 0:
99
+ raise PeerDisconnectedException()
100
+
101
+ num_bytes_read += num_bytes_received
102
+
103
+ payload_bytes.extend(recv_bytes)
104
+
105
+ return payload_bytes