Debian Codegraph @

metrics/multi_overhead.py

raw symbols SCIP
1 import cProfile
2 import profile
3 import re
4 import sys
5 import time
6 from timeit import Timer
7
8 import django.conf
9 import django.template.loader
10
11 import pyinstrument
12
13 django.conf.settings.configure(
14 INSTALLED_APPS=(),
15 TEMPLATES=[
16 {
17 "BACKEND": "django.template.backends.django.DjangoTemplates",
18 "DIRS": [
19 "./examples/django_example/django_example/templates",
20 ],
21 }
22 ],
23 )
24 django.setup()
25
26
27 def test_func_re():
28 re.compile(
29 r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
30 )
31
32
33 def test_func_template():
34 django.template.loader.render_to_string("template.html")
35
36
37 # heat caches
38 test_func_template()
39
40
41 def time_base(function, repeats):
42 timer = Timer(stmt=function)
43 return timer.repeat(number=repeats)
44
45
46 def time_profile(function, repeats):
47 timer = Timer(stmt=function)
48 p = profile.Profile()
49 return p.runcall(lambda: timer.repeat(number=repeats))
50
51
52 def time_cProfile(function, repeats):
53 timer = Timer(stmt=function)
54 p = cProfile.Profile()
55 return p.runcall(lambda: timer.repeat(number=repeats))
56
57
58 def time_pyinstrument(function, repeats):
59 timer = Timer(stmt=function)
60 p = pyinstrument.Profiler()
61 p.start()
62 result = timer.repeat(number=repeats)
63 p.stop()
64 return result
65
66
67 profilers = (
68 ("Base", time_base),
69 # ('profile', time_profile),
70 ("cProfile", time_cProfile),
71 ("pyinstrument", time_pyinstrument),
72 )
73
74 tests = (
75 ("re.compile", test_func_re, 120000),
76 ("django template render", test_func_template, 400),
77 )
78
79
80 def timings_for_test(test_func, repeats):
81 results = []
82 for profiler_tuple in profilers:
83 time = profiler_tuple[1](test_func, repeats)
84 results += (profiler_tuple[0], min(time))
85
86 return results
87
88
89 # print header
90 for column in [""] + [test[0] for test in tests]:
91 sys.stdout.write(f"{column:>24}")
92
93 sys.stdout.write("\n")
94
95 for profiler_tuple in profilers:
96 sys.stdout.write(f"{profiler_tuple[0]:>24}")
97 sys.stdout.flush()
98 for test_tuple in tests:
99 time = min(profiler_tuple[1](test_tuple[1], test_tuple[2])) * 10
100 sys.stdout.write(f"{time:>24.2f}")
101 sys.stdout.flush()
102 sys.stdout.write("\n")