Skip to content

Commit c857d75

Browse files
author
Daniel Watkins
committed
Restore running of benchmarks in test_snippets.py
This distinguishes them from regular snippet tests, and runs them without trace logging (which was causing major slowdowns previously).
1 parent 402ca31 commit c857d75

1 file changed

Lines changed: 30 additions & 18 deletions

File tree

tests/test_snippets.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@
1010
import logging
1111
import subprocess
1212
import contextlib
13+
import enum
1314

1415
import compile_code
1516

1617

18+
class _TestType(enum.Enum):
19+
functional = 1
20+
benchmark = 2
21+
22+
1723
logger = logging.getLogger('tests')
1824
ROOT_DIR = '..'
19-
TEST_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'tests', 'snippets'))
25+
TEST_ROOT = os.path.abspath(os.path.join(ROOT_DIR, 'tests'))
26+
TEST_DIRS = {
27+
_TestType.functional: os.path.join(TEST_ROOT, 'snippets'),
28+
_TestType.benchmark: os.path.join(TEST_ROOT, 'benchmarks'),
29+
}
2030
CPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'py_code_object'))
2131
RUSTPYTHON_RUNNER_DIR = os.path.abspath(os.path.join(ROOT_DIR))
2232

@@ -29,14 +39,14 @@ def pushd(path):
2939
os.chdir(old_dir)
3040

3141

32-
def perform_test(filename, method):
42+
def perform_test(filename, method, test_type):
3343
logger.info('Running %s via %s', filename, method)
3444
if method == 'cpython':
3545
run_via_cpython(filename)
3646
elif method == 'cpython_bytecode':
37-
run_via_cpython_bytecode(filename)
47+
run_via_cpython_bytecode(filename, test_type)
3848
elif method == 'rustpython':
39-
run_via_rustpython(filename)
49+
run_via_rustpython(filename, test_type)
4050
else:
4151
raise NotImplementedError(method)
4252

@@ -46,37 +56,39 @@ def run_via_cpython(filename):
4656
subprocess.check_call([sys.executable, filename])
4757

4858

49-
def run_via_cpython_bytecode(filename):
59+
def run_via_cpython_bytecode(filename, test_type):
5060
# Step1: Create bytecode file:
5161
bytecode_filename = filename + '.bytecode'
5262
with open(bytecode_filename, 'w') as f:
5363
compile_code.compile_to_bytecode(filename, out_file=f)
5464

5565
# Step2: run cpython bytecode:
5666
env = os.environ.copy()
57-
env['RUST_LOG'] = 'debug,cargo=error,jobserver=error'
67+
log_level = 'info' if test_type == _TestType.benchmark else 'debug'
68+
env['RUST_LOG'] = '{},cargo=error,jobserver=error'.format(log_level)
5869
env['RUST_BACKTRACE'] = '1'
5970
with pushd(CPYTHON_RUNNER_DIR):
6071
subprocess.check_call(['cargo', 'run', bytecode_filename], env=env)
6172

6273

63-
def run_via_rustpython(filename):
74+
def run_via_rustpython(filename, test_type):
6475
env = os.environ.copy()
65-
env['RUST_LOG'] = 'trace,cargo=error,jobserver=error'
76+
log_level = 'info' if test_type == _TestType.benchmark else 'trace'
77+
env['RUST_LOG'] = '{},cargo=error,jobserver=error'.format(log_level)
6678
env['RUST_BACKTRACE'] = '1'
6779
with pushd(RUSTPYTHON_RUNNER_DIR):
6880
subprocess.check_call(['cargo', 'run', '--release', filename], env=env)
6981

7082

71-
def create_test_function(cls, filename, method):
83+
def create_test_function(cls, filename, method, test_type):
7284
""" Create a test function for a single snippet """
7385
core_test_directory, snippet_filename = os.path.split(filename)
7486
test_function_name = 'test_{}_'.format(method) \
7587
+ os.path.splitext(snippet_filename)[0] \
7688
.replace('.', '_').replace('-', '_')
7789

7890
def test_function(self):
79-
perform_test(filename, method)
91+
perform_test(filename, method, test_type)
8092

8193
if hasattr(cls, test_function_name):
8294
raise ValueError('Duplicate test case {}'.format(test_function_name))
@@ -86,21 +98,21 @@ def test_function(self):
8698
def populate(method):
8799
def wrapper(cls):
88100
""" Decorator function which can populate a unittest.TestCase class """
89-
for filename in get_test_files():
90-
create_test_function(cls, filename, method)
101+
for test_type, filename in get_test_files():
102+
create_test_function(cls, filename, method, test_type)
91103
return cls
92104
return wrapper
93105

94106

95107
def get_test_files():
96108
""" Retrieve test files """
97-
for filepath in sorted(glob.iglob(os.path.join(
98-
TEST_DIR, '*.py'))):
99-
filename = os.path.split(filepath)[1]
100-
if filename.startswith('xfail_'):
101-
continue
109+
for test_type, test_dir in TEST_DIRS.items():
110+
for filepath in sorted(glob.iglob(os.path.join(test_dir, '*.py'))):
111+
filename = os.path.split(filepath)[1]
112+
if filename.startswith('xfail_'):
113+
continue
102114

103-
yield os.path.abspath(filepath)
115+
yield test_type, os.path.abspath(filepath)
104116

105117

106118
@populate('cpython')

0 commit comments

Comments
 (0)