Debian Codegraph @

benchmarks/benchmark.py

raw symbols SCIP
1 import os
2 import sys
3 import re
4 import subprocess
5 import traceback
6 import statistics
7
8 python = "python3"
9 progname = os.path.join(os.path.dirname(__file__), "julia1_nopil.py")
10 number_of_runs = 1 # We take the average of this many runs.
11
12 # Output timing string from the benchmark.
13 result_regexp = re.compile(
14 "calculate_z_serial_purepython took ([0-9]*\.[0-9]+) seconds"
15 )
16
17 # Characteristics of the tools.
18
19 line_level = {}
20 cpu_profiler = {}
21 separate_profiler = {}
22 memory_profiler = {}
23 unmodified_code = {}
24 timing = {}
25
26 line_level["baseline"] = None
27 line_level["cProfile"] = False
28 line_level["Profile"] = False
29 line_level["line_profiler"] = True
30 line_level["pyinstrument"] = False
31 line_level["yappi_cputime"] = False
32 line_level["yappi_wallclock"] = False
33 line_level["pprofile_deterministic"] = True
34 line_level["pprofile_statistical"] = True
35 line_level["py_spy"] = True
36 line_level["memory_profiler"] = True
37 line_level["scalene_cpu"] = True
38 line_level["scalene_cpu_memory"] = True
39
40 cpu_profiler["baseline"] = None
41 cpu_profiler["cProfile"] = True
42 cpu_profiler["Profile"] = True
43 cpu_profiler["pyinstrument"] = True
44 cpu_profiler["line_profiler"] = True
45 cpu_profiler["yappi_cputime"] = True
46 cpu_profiler["yappi_wallclock"] = True
47 cpu_profiler["pprofile_deterministic"] = True
48 cpu_profiler["pprofile_statistical"] = True
49 cpu_profiler["py_spy"] = True
50 cpu_profiler["memory_profiler"] = False
51 cpu_profiler["scalene_cpu"] = True
52 cpu_profiler["scalene_cpu_memory"] = True
53
54 separate_profiler["baseline"] = None
55 separate_profiler["cProfile"] = False
56 separate_profiler["Profile"] = False
57 separate_profiler["pyinstrument"] = False
58 separate_profiler["line_profiler"] = False
59 separate_profiler["yappi_cputime"] = False
60 separate_profiler["yappi_wallclock"] = False
61 separate_profiler["pprofile_deterministic"] = False
62 separate_profiler["pprofile_statistical"] = False
63 separate_profiler["py_spy"] = False
64 separate_profiler["memory_profiler"] = False
65 separate_profiler["scalene_cpu"] = True
66 separate_profiler["scalene_cpu_memory"] = True
67
68 memory_profiler["baseline"] = None
69 memory_profiler["cProfile"] = False
70 memory_profiler["Profile"] = False
71 memory_profiler["pyinstrument"] = False
72 memory_profiler["line_profiler"] = False
73 memory_profiler["yappi_cputime"] = False
74 memory_profiler["yappi_wallclock"] = False
75 memory_profiler["pprofile_deterministic"] = False
76 memory_profiler["pprofile_statistical"] = False
77 memory_profiler["py_spy"] = False
78 memory_profiler["memory_profiler"] = True
79 memory_profiler["scalene_cpu"] = False
80 memory_profiler["scalene_cpu_memory"] = True
81
82 unmodified_code["baseline"] = None
83 unmodified_code["cProfile"] = True
84 unmodified_code["Profile"] = True
85 unmodified_code["pyinstrument"] = True
86 unmodified_code["line_profiler"] = False
87 unmodified_code["yappi_cputime"] = True
88 unmodified_code["yappi_wallclock"] = True
89 unmodified_code["pprofile_deterministic"] = True
90 unmodified_code["pprofile_statistical"] = True
91 unmodified_code["py_spy"] = True
92 unmodified_code["memory_profiler"] = False
93 unmodified_code["scalene_cpu"] = True
94 unmodified_code["scalene_cpu_memory"] = True
95
96 # how the profilers measure time
97 # - wall clock only
98 # - virtual (process) time only
99 # - either one
100 WallClock = 1
101 VirtualTime = 2
102 Either = 3
103
104 timing["baseline"] = None
105 timing["cProfile"] = WallClock
106 timing["Profile"] = VirtualTime
107 timing["pyinstrument"] = WallClock
108 timing["line_profiler"] = WallClock
109 timing["yappi_cputime"] = Either
110 timing["yappi_wallclock"] = Either
111 timing["pprofile_deterministic"] = WallClock
112 timing["pprofile_statistical"] = WallClock
113 timing["py_spy"] = Either
114 timing["memory_profiler"] = None
115 timing["scalene_cpu"] = Either
116 timing["scalene_cpu_memory"] = Either
117
118
119 # Command lines for the various tools.
120
121 baseline = f"{python} {progname}"
122 cprofile = f"{python} -m cProfile {progname}"
123 profile = f"{python} -m profile {progname}"
124 pyinstrument = f"pyinstrument {progname}"
125 line_profiler = f"{python} -m kernprof -l -v {progname}"
126 pprofile_deterministic = f"pprofile {progname}"
127 pprofile_statistical = f"pprofile --statistic 0.001 {progname}" # Same as Scalene
128 yappi_cputime = f"yappi {progname}"
129 yappi_wallclock = f"yappi -c wall {progname}"
130 py_spy = f"py-spy record -f raw -o foo.txt -- python3.7 {progname}"
131 scalene_cpu = f"{python} -m scalene {progname}"
132 scalene_cpu_memory = (
133 f"{python} -m scalene {progname}" # see below for environment variables
134 )
135
136 benchmarks = [
137 (baseline, "baseline", "_original program_"),
138 (cprofile, "cProfile", "`cProfile`"),
139 (profile, "Profile", "`Profile`"),
140 (pyinstrument, "pyinstrument", "`pyinstrument`"),
141 (line_profiler, "line_profiler", "`line_profiler`"),
142 (pprofile_deterministic, "pprofile_deterministic", "`pprofile` _(deterministic)_"),
143 (pprofile_statistical, "pprofile_statistical", "`pprofile` _(statistical)_"),
144 (yappi_cputime, "yappi_cputime", "`yappi` _(CPU)_"),
145 (yappi_wallclock, "yappi_wallclock", "`yappi` _(wallclock)_"),
146 (scalene_cpu, "scalene_cpu", "`scalene` _(CPU only)_"),
147 (scalene_cpu_memory, "scalene_cpu_memory", "`scalene` _(CPU + memory)_"),
148 ]
149
150 # benchmarks = [(baseline, "baseline", "_original program_"), (pprofile_deterministic, "`pprofile` _(deterministic)_")]
151 # benchmarks = [(baseline, "baseline", "_original program_"), (pprofile_statistical, "pprofile_statistical", "`pprofile` _(statistical)_")]
152 benchmarks = [
153 (baseline, "baseline", "_original program_"),
154 (py_spy, "py_spy", "`py-spy`"),
155 (scalene_cpu, "scalene_cpu", "`scalene` _(CPU only)_"),
156 (scalene_cpu_memory, "scalene_cpu_memory", "`scalene` _(CPU + memory)_"),
157 ]
158
159 average_time = {}
160 check = ":heavy_check_mark:"
161
162 print(
163 "| | Time | Slowdown | Line-level? | CPU? | Python vs. C? | Memory? | Unmodified code? |"
164 )
165 print("| :--- | ---: | ---: | :---: | :---: | :---: | :---: | :---: |")
166
167 for bench in benchmarks:
168 print(bench)
169 times = []
170 for i in range(0, number_of_runs):
171 my_env = os.environ.copy()
172 if bench[1] == "scalene_cpu_memory":
173 my_env["PYTHONMALLOC"] = "malloc"
174 if sys.platform == "darwin":
175 my_env["DYLD_INSERT_LIBRARIES"] = "./libscalene.dylib"
176 if sys.platform == "linux":
177 my_env["LD_PRELOAD"] = "./libscalene.so"
178 result = subprocess.run(
179 bench[0].split(),
180 env=my_env,
181 stderr=subprocess.STDOUT,
182 stdout=subprocess.PIPE,
183 )
184 output = result.stdout.decode("utf-8")
185 print(output)
186 match = result_regexp.search(output)
187 if match is not None:
188 times.append(round(100 * float(match.group(1))) / 100.0)
189 else:
190 print("failed run")
191 average_time[bench[1]] = statistics.mean(times) # sum_time / (number_of_runs * 1.0)
192 print(str(average_time[bench[1]]))
193 if bench[1] == "baseline":
194 print(f"| {bench[2]} | {average_time[bench[1]]}s | 1.0x | | | | | |")
195 print("| | | | | |")
196 else:
197 try:
198 if bench[1].find("scalene") >= 0:
199 if bench[1].find("scalene_cpu") >= 0:
200 print("| | | | | |")
201 print(
202 f"| {bench[2]} | {average_time[bench[1]]}s | **{round(100 * average_time[bench[1]] / average_time['baseline']) / 100}x** | {check if line_level[bench[1]] else 'function-level'} | {check if cpu_profiler[bench[1]] else ''} | {check if separate_profiler[bench[1]] else ''} | {check if memory_profiler[bench[1]] else ''} | {check if unmodified_code[bench[1]] else 'needs `@profile` decorators'} |"
203 )
204 else:
205 print(
206 f"| {bench[2]} | {average_time[bench[1]]}s | {round(100 * average_time[bench[1]] / average_time['baseline']) / 100}x | {check if line_level[bench[1]] else 'function-level'} | {check if cpu_profiler[bench[1]] else ''} | {check if separate_profiler[bench[1]] else ''} | {check if memory_profiler[bench[1]] else ''} | {check if unmodified_code[bench[1]] else 'needs `@profile` decorators'} |"
207 )
208 except Exception as err:
209 traceback.print_exc()
210 print("err = " + str(err))
211 print("WOOPS")
212 # print(bench[1] + " = " + str(sum_time / 5.0))