|
| 1 | +import os |
1 | 2 | import runpy
|
2 | 3 | import sys
|
3 | 4 | from io import StringIO
|
|
10 | 11 |
|
11 | 12 | from benchmarks.benchmark_utils import swap_attr
|
12 | 13 |
|
13 |
| -for entry in [entry for entry in importlib_resources.contents('examples') if |
14 |
| - not pkg_resources.resource_isdir('examples', entry) |
15 |
| - and entry.endswith(".py") |
16 |
| - and '__init__' not in entry |
17 |
| - and 'DELETE' not in entry |
18 |
| - ]: |
19 |
| - print("*****************************************************************") |
20 |
| - print("Benchmarking {}".format(entry)) |
21 |
| - print("*****************************************************************") |
| 14 | +if __name__ == '__main__': |
22 | 15 |
|
23 |
| - with path('examples', entry) as file_in: |
24 |
| - with Benchmarker(cycle=20, extra=1) as bench: |
25 |
| - @bench(entry) |
26 |
| - def _(_): |
27 |
| - # prevent Benchmarker from doing "ZeroDivisionError: float division by zero: |
28 |
| - # ratio = base_time / real_time" |
29 |
| - sleep(0.001) |
30 |
| - # In order to pipe input into examples that have input(), |
31 |
| - # we use the test package, which is meant for internal use by Python only internal and |
32 |
| - # Any use of this package outside of Python’s standard library is discouraged as code (..) |
33 |
| - # can change or be removed without notice between releases of Python. |
34 |
| - # https://docs.python.org/3/library/test.html |
35 |
| - # TODO remove input() from examples? |
36 |
| - with swap_attr(sys, "stdin", StringIO("q")): |
37 |
| - runpy.run_path(file_in, run_name="__main__", init_globals={ |
38 |
| - 'argv': ['--sleep-for-animation=False', '--do_rendering=False'] |
39 |
| - }) |
| 16 | + for entry in [entry for entry in importlib_resources.contents('examples') if |
| 17 | + not pkg_resources.resource_isdir('examples', entry) |
| 18 | + and entry.endswith(".py") |
| 19 | + and '__init__' not in entry |
| 20 | + and 'DELETE' not in entry |
| 21 | + ]: |
| 22 | + print("*****************************************************************") |
| 23 | + print("Benchmarking {}".format(entry)) |
| 24 | + print("*****************************************************************") |
| 25 | + |
| 26 | + benchmarks_output_folder = os.environ.get('BENCHMARKS_OUTPUT_FOLDER', None) |
| 27 | + outfile = None |
| 28 | + if benchmarks_output_folder: |
| 29 | + outfile = os.path.join(benchmarks_output_folder, f"{entry}.json") |
| 30 | + |
| 31 | + with path('examples', entry) as file_in: |
| 32 | + with Benchmarker(cycle=20, extra=1, outfile=outfile) as bench: |
| 33 | + @bench(entry) |
| 34 | + def _(_): |
| 35 | + # prevent Benchmarker from doing "ZeroDivisionError: float division by zero: |
| 36 | + # ratio = base_time / real_time" |
| 37 | + sleep(0.001) |
| 38 | + # In order to pipe input into examples that have input(), |
| 39 | + # we use the test package, which is meant for internal use by Python only internal and |
| 40 | + # Any use of this package outside of Python’s standard library is discouraged as code (..) |
| 41 | + # can change or be removed without notice between releases of Python. |
| 42 | + # https://docs.python.org/3/library/test.html |
| 43 | + # TODO remove input() from examples? |
| 44 | + with swap_attr(sys, "stdin", StringIO("q")): |
| 45 | + runpy.run_path(file_in, run_name="__main__", init_globals={ |
| 46 | + 'argv': ['--sleep-for-animation=False', '--do_rendering=False'] |
| 47 | + }) |
0 commit comments