diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2018-04-07 07:48:58 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@corte.si> | 2018-04-16 09:19:14 +1200 |
commit | b663a224a3655aab95a39eab2450f1f0e98997f2 (patch) | |
tree | 81e3f5445c0af49fbfe09a6bcbd3c89dc3e7904e /test/bench/benchmark.py | |
parent | 5f74adc2df6d2a9452a9e3a6923fe05ba579e9e6 (diff) | |
download | mitmproxy-b663a224a3655aab95a39eab2450f1f0e98997f2.tar.gz mitmproxy-b663a224a3655aab95a39eab2450f1f0e98997f2.tar.bz2 mitmproxy-b663a224a3655aab95a39eab2450f1f0e98997f2.zip |
Improve benchmarking
- The benchmark addon now manages setting up and tearing down the backend and
traffic processes itself.
- Use wrk instead of hey. I get more consistent results with this tool, and hey
shows a strange tail-latency bump that seems artificial.
- Make termination behaviour simpler. The bencmark revealed a bug where .done
events were not called if the proxy was shut down by an addon.
Diffstat (limited to 'test/bench/benchmark.py')
-rw-r--r-- | test/bench/benchmark.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/test/bench/benchmark.py b/test/bench/benchmark.py new file mode 100644 index 00000000..8d208088 --- /dev/null +++ b/test/bench/benchmark.py @@ -0,0 +1,51 @@ +import asyncio +import cProfile +from mitmproxy import ctx + + +class Benchmark: + """ + A simple profiler addon. + """ + def __init__(self): + self.pr = cProfile.Profile() + self.started = False + + async def procs(self): + ctx.log.error("starting benchmark") + backend = await asyncio.create_subprocess_exec("devd", "-q", "-p", "10001", ".") + traf = await asyncio.create_subprocess_exec( + "wrk", + "-c50", + "-d5s", + "http://localhost:%s/benchmark.py" % ctx.master.server.address[1], + stdout=asyncio.subprocess.PIPE + ) + stdout, _ = await traf.communicate() + open(ctx.options.benchmark_save_path + ".bench", mode="wb").write(stdout) + ctx.log.error(stdout.decode("ascii")) + backend.kill() + ctx.master.shutdown() + + def load(self, loader): + loader.add_option( + "benchmark_save_path", + str, + "/tmp/profile", + "Destination for the .prof and and .bench result files" + ) + ctx.options.update( + mode="reverse:http://devd.io:10001", + ) + self.pr.enable() + + def running(self): + if not self.started: + self.started = True + asyncio.get_event_loop().create_task(self.procs()) + + def done(self): + self.pr.dump_stats(ctx.options.benchmark_save_path + ".prof") + + +addons = [Benchmark()]
\ No newline at end of file |