1
1
import os
2
+ import runpy
3
+ import sys
4
+ import traceback
5
+ from io import StringIO
2
6
from time import sleep
3
7
4
8
import importlib_resources
5
9
import pkg_resources
6
10
from benchmarker import Benchmarker
7
11
from importlib_resources import path
8
12
13
+ from benchmarks .benchmark_utils import swap_attr
14
+
9
15
10
16
def benchmark_all_examples ():
11
17
for entry in [entry for entry in importlib_resources .contents ('examples' ) if
@@ -23,27 +29,30 @@ def benchmark_all_examples():
23
29
if benchmarks_output_folder :
24
30
outfile = os .path .join (benchmarks_output_folder , f"{ entry } .json" )
25
31
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." )
47
56
48
57
49
58
if __name__ == '__main__' :
0 commit comments