aboutsummaryrefslogtreecommitdiffstats
path: root/test/helper_tools/benchtool.py
blob: b9078d0e4d15cf11492a86d93eb5a61b04a92b6f (plain)
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
# Profile mitmdump with apachebench and
# yappi (https://code.google.com/p/yappi/)
#
# Requirements:
# - Apache Bench "ab" binary
# - pip install click yappi

from mitmproxy.main import mitmdump
from os import system
from threading import Thread
import time

import yappi
import click


class ApacheBenchThread(Thread):

    def __init__(self, concurrency):
        self.concurrency = concurrency
        super().__init__()

    def run(self):
        time.sleep(2)
        system(
            "ab -n 1024 -c {} -X 127.0.0.1:8080 http://example.com/".format(self.concurrency))


@click.command()
@click.option('--profiler', default="none", type=click.Choice(['none', 'yappi']))
@click.option('--clock-type', default="cpu", type=click.Choice(['wall', 'cpu']))
@click.option('--concurrency', default=1, type=click.INT)
def main(profiler, clock_type, concurrency):

    outfile = "callgrind.mitmdump-{}-c{}".format(clock_type, concurrency)
    a = ApacheBenchThread(concurrency)
    a.start()

    if profiler == "yappi":
        yappi.set_clock_type(clock_type)
        yappi.start(addons=True)

    print("Start mitmdump...")
    mitmdump(["-k", "-q", "-S", "1024example"])
    print("mitmdump stopped.")

    print("Save profile information...")
    if profiler == "yappi":
        yappi.stop()
        stats = yappi.get_func_stats()
        stats.save(outfile, type='callgrind')
    print("Done.")


if __name__ == '__main__':
    main()