summaryrefslogtreecommitdiff
path: root/tests/performance_test.py
blob: a7790ca76afd2e3ad05b7243972f47d264c0d7b7 (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
57
58
#!/usr/bin/env python
from webob.response import Response


def make_middleware(app):
    from repoze.profile.profiler import AccumulatingProfileMiddleware

    return AccumulatingProfileMiddleware(
        app,
        log_filename="/tmp/profile.log",
        discard_first_request=True,
        flush_at_shutdown=True,
        path="/__profile__",
    )


def simple_app(environ, start_response):
    resp = Response("Hello world!")
    return resp(environ, start_response)


if __name__ == "__main__":
    import os
    import signal
    import sys

    if sys.argv[1:]:
        arg = sys.argv[1]
    else:
        arg = None
    if arg in ["open", "run"]:
        import subprocess
        import time
        import webbrowser

        os.environ["SHOW_OUTPUT"] = "0"
        proc = subprocess.Popen([sys.executable, __file__])
        time.sleep(1)
        subprocess.call(["ab", "-n", "1000", "http://localhost:8080/"])
        if arg == "open":
            webbrowser.open("http://localhost:8080/__profile__")
        print("Hit ^C to end")
        try:
            while 1:
                input()
        finally:
            os.kill(proc.pid, signal.SIGKILL)
    else:
        from paste.httpserver import serve

        if os.environ.get("SHOW_OUTPUT") != "0":
            print("Note you can also use:)")
            print(f"  {sys.executable} {__file__} open")
            print('to run ab and open a browser (or "run" to just run ab)')
            print("Now do:")
            print("ab -n 1000 http://localhost:8080/")
            print("wget -O - http://localhost:8080/__profile__")
        serve(make_middleware(simple_app))