|
| 1 | +""" |
| 2 | +Django-cms test |
| 3 | +Sets up a djangocms installation, and hits '/' a number of times. |
| 4 | +'/' is not super interesting, but it still exercises a little bit of |
| 5 | +functionality; looking at cms/templates/cms/welcome.html, it seems |
| 6 | +to do a decent amount of template logic, as well as do some basic |
| 7 | +user auth. |
| 8 | +We could probably improve the flow though, perhaps by logging in |
| 9 | +and browsing around. |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +import requests |
| 14 | +import socket |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | +import tempfile |
| 18 | +import time |
| 19 | +import json |
| 20 | + |
| 21 | +def setup(): |
| 22 | + """ |
| 23 | + Set up a djangocms installation. |
| 24 | + Runs the initial bootstrapping without the db migration, |
| 25 | + so that we can turn off sqlite synchronous and avoid fs time. |
| 26 | + Rough testing shows that setting synchronous=OFF is basically |
| 27 | + the same performance as running on /dev/shm |
| 28 | + """ |
| 29 | + |
| 30 | + subprocess.check_call([exe.replace("python3", "djangocms"), "testsite", "--verbose", "--no-sync"]) |
| 31 | + |
| 32 | + with open("testsite/testsite/settings.py", "a") as f: |
| 33 | + f.write(""" |
| 34 | +from django.db.backends.signals import connection_created |
| 35 | +def set_no_sychronous(sender, connection, **kwargs): |
| 36 | + if connection.vendor == 'sqlite': |
| 37 | + cursor = connection.cursor() |
| 38 | + cursor.execute('PRAGMA synchronous = OFF;') |
| 39 | +
|
| 40 | +connection_created.connect(set_no_sychronous) |
| 41 | +""") |
| 42 | + start = time.time() |
| 43 | + subprocess.check_call([exe, "manage.py", "migrate"], cwd="testsite") |
| 44 | + elapsed = time.time() - start |
| 45 | + print("%.2fs to initialize db" % (elapsed,)) |
| 46 | + |
| 47 | +def waitUntilUp(addr, timeout=10.0): |
| 48 | + start = time.time() |
| 49 | + while True: |
| 50 | + try: |
| 51 | + with socket.create_connection(addr) as sock: |
| 52 | + return |
| 53 | + except ConnectionRefusedError: |
| 54 | + if time.time() > start + timeout: |
| 55 | + raise Exception("Timeout reached when trying to connect") |
| 56 | + time.sleep(0.001) |
| 57 | + |
| 58 | +def runbenchmark(n=800, out_file=None): |
| 59 | + p = subprocess.Popen([exe, "manage.py", "runserver", "--noreload"], cwd="testsite", stdout=open("/dev/null", "w"), stderr=subprocess.STDOUT) |
| 60 | + try: |
| 61 | + waitUntilUp(("127.0.0.1", 8000)) |
| 62 | + |
| 63 | + start = time.time() |
| 64 | + times = [] |
| 65 | + for i in range(n): |
| 66 | + times.append(time.time()) |
| 67 | + if i % 100 == 0: |
| 68 | + print(i, time.time() - start) |
| 69 | + requests.get("http://localhost:8000/").text |
| 70 | + times.append(time.time()) |
| 71 | + elapsed = time.time() - start |
| 72 | + print("%.2fs (%.3freq/s)" % (elapsed, n / elapsed)) |
| 73 | + |
| 74 | + exitcode = p.poll() |
| 75 | + assert exitcode is None, exitcode |
| 76 | + |
| 77 | + if out_file: |
| 78 | + json.dump(times, open(out_file, 'w')) |
| 79 | + |
| 80 | + finally: |
| 81 | + p.terminate() |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + exe = sys.executable |
| 85 | + # Hack: make sure this file gets run as "python3" so that perf will collate across different processes |
| 86 | + if not exe.endswith('3'): |
| 87 | + os.execv(exe + '3', [exe + '3'] + sys.argv) |
| 88 | + |
| 89 | + os.environ["PATH"] = os.path.dirname(exe) + ":" + os.environ["PATH"] |
| 90 | + |
| 91 | + """ |
| 92 | + Usage: |
| 93 | + python djangocms.py |
| 94 | + python djangocms.py --setup DIR |
| 95 | + python djangocms.py --serve DIR |
| 96 | +
|
| 97 | + The first form creates a temporary directory, sets up djangocms in it, |
| 98 | + serves out of it, and removes the directory. |
| 99 | + The second form sets up a djangocms installation in the given directory. |
| 100 | + The third form runs a benchmark out of an already-set-up directory |
| 101 | + The second and third forms are useful if you want to benchmark the |
| 102 | + initial migration phase separately from the second serving phase. |
| 103 | + """ |
| 104 | + if "--setup" in sys.argv: |
| 105 | + assert len(sys.argv) > 2 |
| 106 | + dir = sys.argv[-1] |
| 107 | + os.makedirs(dir, exist_ok=True) |
| 108 | + os.chdir(dir) |
| 109 | + setup() |
| 110 | + elif "--serve" in sys.argv: |
| 111 | + assert len(sys.argv) > 2 |
| 112 | + os.chdir(sys.argv[-1]) |
| 113 | + runbenchmark() |
| 114 | + else: |
| 115 | + n = 800 |
| 116 | + if len(sys.argv) > 1: |
| 117 | + n = int(sys.argv[1]) |
| 118 | + out_file = None |
| 119 | + if len(sys.argv) > 2: |
| 120 | + out_file = os.path.abspath(sys.argv[2]) |
| 121 | + |
| 122 | + # It might be interesting to put the temporary directory in /dev/shm, |
| 123 | + # which makes the initial db migration about 20% faster. |
| 124 | + with tempfile.TemporaryDirectory(prefix="djangocms_test_") as d: |
| 125 | + os.chdir(d) |
| 126 | + |
| 127 | + setup() |
| 128 | + runbenchmark(n, out_file) |
0 commit comments