|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +import multiprocessing |
| 4 | +import os |
| 5 | +import signal |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import time |
| 9 | +from contextlib import contextmanager |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +WD = Path(__file__).resolve().parent |
| 14 | +CPU = multiprocessing.cpu_count() |
| 15 | +LOOPS = ['asyncio', 'rloop', 'uvloop'] |
| 16 | +MSGS = [1024, 1024 * 10, 1024 * 100] |
| 17 | +CONCURRENCIES = sorted({1, max(CPU / 2, 1), max(CPU - 1, 1)}) |
| 18 | + |
| 19 | + |
| 20 | +@contextmanager |
| 21 | +def server(loop, streams=False, proto=False): |
| 22 | + exc_prefix = os.environ.get('BENCHMARK_EXC_PREFIX') |
| 23 | + py = 'python' |
| 24 | + if exc_prefix: |
| 25 | + py = f'{exc_prefix}/{py}' |
| 26 | + target = WD / 'server.py' |
| 27 | + proc_cmd = f'{py} {target} --loop {loop}' |
| 28 | + if streams: |
| 29 | + proc_cmd += ' --streams' |
| 30 | + if proto: |
| 31 | + proc_cmd += ' --proto' |
| 32 | + |
| 33 | + proc = subprocess.Popen(proc_cmd, shell=True, preexec_fn=os.setsid) # noqa: S602 |
| 34 | + time.sleep(2) |
| 35 | + yield proc |
| 36 | + os.killpg(os.getpgid(proc.pid), signal.SIGKILL) |
| 37 | + |
| 38 | + |
| 39 | +def client(duration, concurrency, msgsize): |
| 40 | + exc_prefix = os.environ.get('BENCHMARK_EXC_PREFIX') |
| 41 | + py = 'python' |
| 42 | + if exc_prefix: |
| 43 | + py = f'{exc_prefix}/{py}' |
| 44 | + target = WD / 'client.py' |
| 45 | + cmd_parts = [ |
| 46 | + py, |
| 47 | + str(target), |
| 48 | + f'--concurrency {concurrency}', |
| 49 | + f'--duration {duration}', |
| 50 | + f'--msize {msgsize}', |
| 51 | + '--output json', |
| 52 | + ] |
| 53 | + try: |
| 54 | + proc = subprocess.run( # noqa: S602 |
| 55 | + ' '.join(cmd_parts), |
| 56 | + shell=True, |
| 57 | + check=True, |
| 58 | + capture_output=True, |
| 59 | + ) |
| 60 | + data = json.loads(proc.stdout.decode('utf8')) |
| 61 | + return data |
| 62 | + except Exception as e: |
| 63 | + print(f'WARN: got exception {e} while loading client data') |
| 64 | + return {} |
| 65 | + |
| 66 | + |
| 67 | +def benchmark(msgs=None, concurrencies=None): |
| 68 | + concurrencies = concurrencies or CONCURRENCIES |
| 69 | + msgs = msgs or MSGS |
| 70 | + results = {} |
| 71 | + # primer |
| 72 | + client(1, 1, 1024) |
| 73 | + time.sleep(1) |
| 74 | + # warm up |
| 75 | + client(1, max(concurrencies), 1024 * 100) |
| 76 | + time.sleep(2) |
| 77 | + # bench |
| 78 | + for concurrency in concurrencies: |
| 79 | + cres = results[concurrency] = {} |
| 80 | + for msg in msgs: |
| 81 | + res = client(10, concurrency, msg) |
| 82 | + cres[msg] = res |
| 83 | + time.sleep(3) |
| 84 | + time.sleep(1) |
| 85 | + return results |
| 86 | + |
| 87 | + |
| 88 | +def raw(): |
| 89 | + results = {} |
| 90 | + for loop in LOOPS: |
| 91 | + with server(loop): |
| 92 | + results[loop] = benchmark(concurrencies=CONCURRENCIES[0]) |
| 93 | + return results |
| 94 | + |
| 95 | + |
| 96 | +def stream(): |
| 97 | + results = {} |
| 98 | + for loop in LOOPS: |
| 99 | + with server(loop, streams=True): |
| 100 | + results[loop] = benchmark(concurrencies=CONCURRENCIES[0]) |
| 101 | + return results |
| 102 | + |
| 103 | + |
| 104 | +def proto(): |
| 105 | + results = {} |
| 106 | + for loop in LOOPS: |
| 107 | + with server(loop, proto=True): |
| 108 | + results[loop] = benchmark(concurrencies=CONCURRENCIES[0]) |
| 109 | + return results |
| 110 | + |
| 111 | + |
| 112 | +def concurrency(): |
| 113 | + results = {} |
| 114 | + for loop in LOOPS: |
| 115 | + with server(loop): |
| 116 | + results[loop] = benchmark(msgs=[1024], concurrencies=CONCURRENCIES[1:]) |
| 117 | + return results |
| 118 | + |
| 119 | + |
| 120 | +def _rloop_version(): |
| 121 | + import rloop |
| 122 | + |
| 123 | + return rloop.__version__ |
| 124 | + |
| 125 | + |
| 126 | +def run(): |
| 127 | + all_benchmarks = { |
| 128 | + 'raw': raw, |
| 129 | + 'stream': stream, |
| 130 | + 'proto': proto, |
| 131 | + 'concurrency': concurrency, |
| 132 | + } |
| 133 | + inp_benchmarks = sys.argv[1:] or ['raw'] |
| 134 | + run_benchmarks = set(inp_benchmarks) & set(all_benchmarks.keys()) |
| 135 | + |
| 136 | + now = datetime.datetime.utcnow() |
| 137 | + results = {} |
| 138 | + for benchmark_key in run_benchmarks: |
| 139 | + runner = all_benchmarks[benchmark_key] |
| 140 | + results[benchmark_key] = runner() |
| 141 | + |
| 142 | + with open('results/data.json', 'w') as f: |
| 143 | + pyver = sys.version_info |
| 144 | + f.write( |
| 145 | + json.dumps( |
| 146 | + { |
| 147 | + 'cpu': CPU, |
| 148 | + 'run_at': int(now.timestamp()), |
| 149 | + 'pyver': f'{pyver.major}.{pyver.minor}', |
| 150 | + 'results': results, |
| 151 | + 'rloop': _rloop_version(), |
| 152 | + } |
| 153 | + ) |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + run() |
0 commit comments