-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-pacing3.py
99 lines (84 loc) · 2.4 KB
/
test-pacing3.py
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
import io
import os
import select
import socket
import time
import utils
import ctypes
import signal
import atexit
utils.new_ns()
port = 1
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.bind(('127.0.0.1', port))
s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
s.listen(16)
tcpdump = utils.tcpdump_start(port)
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
send_bufsize = c.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
print("[s] send_bufsize=%d" % (send_bufsize,))
recv_bufsize = c.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
print("[s] recv_bufsize=%d" % (recv_bufsize,))
c.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024)
c.connect(('127.0.0.1', port))
if True:
RECV_BYTES_PER_SEC = 20000
x, _ = s.accept()
child_pid = os.fork()
x.set_inheritable(True)
if child_pid == 0:
c.close()
recv_bytes = 0
# greater than poll_period
time.sleep(20)
t0 = time.time()
while True:
t1 = time.time()
bytes_per_sec = recv_bytes / (t1-t0)
if bytes_per_sec < RECV_BYTES_PER_SEC:
buf = x.recv(128)
recv_bytes += len(buf)
else:
time.sleep(0.1)
os.exit(0)
def kill_child():
os.kill(child_pid, signal.SIGINT)
os.waitpid(child_pid, 0)
atexit.register(kill_child)
else:
x, _ = s.accept()
x.shutdown(socket.SHUT_WR)
TCP_NOTSENT_LOWAT = 25
poll_period = 15
send_cnt = 0
while True:
c.setblocking(False)
while True:
try:
c.send(b"A" * 16384 * 4)
send_cnt += 1
print("[s] send_cnt=%d" % (send_cnt,))
except io.BlockingIOError:
break
c.setblocking(True)
_, _, notsent1 = utils.socket_info(c)
print("[s] notsent before=%d" % (notsent1,))
notsent1_t = time.time()
poll = select.poll()
poll.register(c, select.POLLOUT)
events = poll.poll(poll_period * 1000) # in ms
_, _, notsent2 = utils.socket_info(c)
print("[s] notsent after=%d" % (notsent2,))
notsent2_t = time.time()
pace = (notsent1 - notsent2) / (notsent2_t - notsent1_t)
if pace > 90:
print("[s] pace=%f bytes/sec. OK!" % (pace,))
else:
print("[s] pace=%f bytes/sec. connection too slow!" % (pace,))
break
utils.check_buffer(c)
c.close()
while True:
utils.ss(port)
time.sleep(5)
os.kill(child_pid, signal.SIGINT)