Skip to content

Commit 2d7f1e7

Browse files
committed
Try debugging all examples.
1 parent 4be8fd7 commit 2d7f1e7

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

benchmarks/benchmark_all_examples.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import os
2+
import runpy
3+
import sys
4+
import traceback
5+
from io import StringIO
26
from time import sleep
37

48
import importlib_resources
59
import pkg_resources
610
from benchmarker import Benchmarker
711
from importlib_resources import path
812

13+
from benchmarks.benchmark_utils import swap_attr
14+
915

1016
def benchmark_all_examples():
1117
for entry in [entry for entry in importlib_resources.contents('examples') if
@@ -23,27 +29,30 @@ def benchmark_all_examples():
2329
if benchmarks_output_folder:
2430
outfile = os.path.join(benchmarks_output_folder, f"{entry}.json")
2531
print(f"outfile={outfile}")
26-
27-
with path('examples', entry) as file_in:
28-
29-
with Benchmarker(cycle=20, extra=1, outfile=outfile) as bench:
30-
@bench(entry)
31-
def _(_):
32-
# prevent Benchmarker from doing "ZeroDivisionError: float division by zero:
33-
# ratio = base_time / real_time"
34-
sleep(0.001)
35-
# In order to pipe input into examples that have input(),
36-
# we use the test package, which is meant for internal use by Python only internal and
37-
# Any use of this package outside of Python’s standard library is discouraged as code (..)
38-
# can change or be removed without notice between releases of Python.
39-
# https://docs.python.org/3/library/test.html
40-
with open(outfile, "w") as o:
41-
o.write("abcd")
42-
# # TODO remove input() from examples?
43-
# with swap_attr(sys, "stdin", StringIO("q")):
44-
# runpy.run_path(file_in, run_name="__main__", init_globals={
45-
# 'argv': ['--sleep-for-animation=False', '--do_rendering=False']
46-
# })
32+
try:
33+
with path('examples', entry) as file_in:
34+
35+
with Benchmarker(cycle=20, extra=1, outfile=outfile) as bench:
36+
@bench(entry)
37+
def _(_):
38+
# prevent Benchmarker from doing "ZeroDivisionError: float division by zero:
39+
# ratio = base_time / real_time"
40+
sleep(0.001)
41+
# In order to pipe input into examples that have input(),
42+
# we use the test package, which is meant for internal use by Python only internal and
43+
# Any use of this package outside of Python’s standard library is discouraged as code (..)
44+
# can change or be removed without notice between releases of Python.
45+
# https://docs.python.org/3/library/test.html
46+
# TODO remove input() from examples?
47+
with swap_attr(sys, "stdin", StringIO("q")):
48+
runpy.run_path(file_in, run_name="__main__", init_globals={
49+
'argv': ['--sleep-for-animation=False', '--do_rendering=False']
50+
})
51+
52+
except:
53+
print("runpy failed:")
54+
traceback.print_exc()
55+
print("runpy done.")
4756

4857

4958
if __name__ == '__main__':

benchmarks/profile_all_examples.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def my_func():
3636
except:
3737
print("cProfile failed:")
3838
traceback.print_exc()
39-
39+
print("cProfile done.")
4040

4141
def profile_all_examples():
4242
for entry in [entry for entry in importlib_resources.contents('examples') if

0 commit comments

Comments
 (0)