2015-11-11 21:29:53 +00:00
|
|
|
# Copied with minimal modifications from curio
|
|
|
|
# https://github.com/dabeaz/curio
|
|
|
|
|
|
|
|
|
2015-11-26 19:37:17 +00:00
|
|
|
import argparse
|
2018-05-25 21:34:25 +00:00
|
|
|
import concurrent.futures
|
|
|
|
import socket
|
2018-05-25 21:48:18 +00:00
|
|
|
import ssl
|
2015-11-11 21:29:53 +00:00
|
|
|
import time
|
2016-05-24 15:52:45 +00:00
|
|
|
|
2015-11-11 21:29:53 +00:00
|
|
|
|
2015-11-26 19:37:17 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--msize', default=1000, type=int,
|
|
|
|
help='message size in bytes')
|
2016-05-24 17:48:40 +00:00
|
|
|
parser.add_argument('--mpr', default=1, type=int,
|
|
|
|
help='messages per request')
|
2015-11-26 19:37:17 +00:00
|
|
|
parser.add_argument('--num', default=200000, type=int,
|
|
|
|
help='number of messages')
|
|
|
|
parser.add_argument('--times', default=1, type=int,
|
|
|
|
help='number of times to run the test')
|
|
|
|
parser.add_argument('--workers', default=3, type=int,
|
|
|
|
help='number of workers')
|
2016-02-03 23:33:39 +00:00
|
|
|
parser.add_argument('--addr', default='127.0.0.1:25000', type=str,
|
2017-02-11 15:21:54 +00:00
|
|
|
help='address:port of echoserver')
|
2018-05-25 21:48:18 +00:00
|
|
|
parser.add_argument('--ssl', default=False, action='store_true')
|
2015-11-26 19:37:17 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-05-25 21:48:18 +00:00
|
|
|
client_context = None
|
|
|
|
if args.ssl:
|
|
|
|
print('with SSL')
|
2019-10-21 20:02:24 +00:00
|
|
|
if hasattr(ssl, 'PROTOCOL_TLS'):
|
|
|
|
client_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
|
|
|
|
else:
|
|
|
|
client_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
|
2018-05-25 21:48:18 +00:00
|
|
|
if hasattr(client_context, 'check_hostname'):
|
|
|
|
client_context.check_hostname = False
|
|
|
|
client_context.verify_mode = ssl.CERT_NONE
|
|
|
|
|
2016-04-01 19:35:56 +00:00
|
|
|
unix = False
|
|
|
|
if args.addr.startswith('file:'):
|
|
|
|
unix = True
|
|
|
|
addr = args.addr[5:]
|
|
|
|
else:
|
|
|
|
addr = args.addr.split(':')
|
|
|
|
addr[1] = int(addr[1])
|
|
|
|
addr = tuple(addr)
|
2016-02-03 23:33:39 +00:00
|
|
|
print('will connect to: {}'.format(addr))
|
2015-11-26 19:37:17 +00:00
|
|
|
|
|
|
|
MSGSIZE = args.msize
|
2016-05-24 17:48:40 +00:00
|
|
|
REQSIZE = MSGSIZE * args.mpr
|
2015-11-26 19:37:17 +00:00
|
|
|
|
2019-02-15 22:03:58 +00:00
|
|
|
msg = b'x' * (MSGSIZE - 1) + b'\n'
|
2016-05-24 17:48:40 +00:00
|
|
|
if args.mpr:
|
|
|
|
msg *= args.mpr
|
2015-11-26 19:37:17 +00:00
|
|
|
|
|
|
|
def run_test(n):
|
|
|
|
print('Sending', NMESSAGES, 'messages')
|
2016-05-24 17:48:40 +00:00
|
|
|
if args.mpr:
|
|
|
|
n //= args.mpr
|
|
|
|
|
2016-04-01 19:35:56 +00:00
|
|
|
if unix:
|
2018-05-25 21:34:25 +00:00
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
2016-04-01 19:35:56 +00:00
|
|
|
else:
|
2018-05-25 21:34:25 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2016-05-24 15:52:45 +00:00
|
|
|
|
|
|
|
try:
|
2018-05-25 21:34:25 +00:00
|
|
|
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
2016-05-24 15:52:45 +00:00
|
|
|
except (OSError, NameError):
|
|
|
|
pass
|
|
|
|
|
2018-05-25 21:48:18 +00:00
|
|
|
if client_context:
|
|
|
|
sock = client_context.wrap_socket(sock)
|
|
|
|
|
2016-02-03 23:33:39 +00:00
|
|
|
sock.connect(addr)
|
2018-05-25 21:48:18 +00:00
|
|
|
|
2015-11-26 19:37:17 +00:00
|
|
|
while n > 0:
|
|
|
|
sock.sendall(msg)
|
|
|
|
nrecv = 0
|
2016-05-24 17:48:40 +00:00
|
|
|
while nrecv < REQSIZE:
|
|
|
|
resp = sock.recv(REQSIZE)
|
2015-11-26 19:37:17 +00:00
|
|
|
if not resp:
|
|
|
|
raise SystemExit()
|
|
|
|
nrecv += len(resp)
|
|
|
|
n -= 1
|
|
|
|
|
|
|
|
TIMES = args.times
|
|
|
|
N = args.workers
|
|
|
|
NMESSAGES = args.num
|
|
|
|
start = time.time()
|
|
|
|
for _ in range(TIMES):
|
2018-05-25 21:34:25 +00:00
|
|
|
with concurrent.futures.ProcessPoolExecutor(max_workers=N) as e:
|
2015-11-26 19:37:17 +00:00
|
|
|
for _ in range(N):
|
|
|
|
e.submit(run_test, NMESSAGES)
|
|
|
|
end = time.time()
|
2019-02-15 22:03:58 +00:00
|
|
|
duration = end - start
|
2018-05-25 21:34:25 +00:00
|
|
|
print(NMESSAGES * N * TIMES, 'in', duration)
|
|
|
|
print(NMESSAGES * N * TIMES / duration, 'requests/sec')
|