Skip to content

Commit

Permalink
detect mpirun from cmake tests for scheduling (#8)
Browse files Browse the repository at this point in the history
v0.0.9
  • Loading branch information
PhilipDeegan authored Feb 8, 2024
1 parent 0561560 commit aa33c4c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions phlop/testing/parallel_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def process(batches, n_cores=None, print_only=False, fail_fast=False):
batches = [batches]
if sum([len(t.tests) for t in batches]) == 0:
return # nothing to do

if print_only:
print_tests(batches)
return
Expand Down
57 changes: 49 additions & 8 deletions phlop/testing/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

_LOG_DIR = Path(os.environ.get("PHLOP_LOG_DIR", os.getcwd()))

CMD_PREFIX = ""
CMD_POSTFIX = ""


@dataclass
class TestCase:
cmd: str
env: dict = field(default_factory=lambda: {}) # dict[str, str] # eventually
working_dir: str = field(default_factory=lambda: None)
log_file_path: str = field(default_factory=lambda: None)
cores: int = field(default_factory=lambda: 1)

def __post_init__(self):
self.cmd = self.cmd.strip()
Expand Down Expand Up @@ -67,8 +71,8 @@ def __call__(self, ctest_test):

class PythonUnitTestCaseExtractor:
def __call__(self, ctest_test):
if "python3" in ctest_test.cmd: # hacky
return load_test_cases_from_cmake(ctest_test)
if "python3 " in ctest_test.cmd: # hacky
return load_py_test_cases_from_cmake(ctest_test)
return None


Expand All @@ -80,7 +84,7 @@ def __call__(self, ctest_test):


def python3_default_test_cmd(clazz, test_id):
return f"python3 -m {clazz.__module__} {clazz.__name__}.{test_id}"
return f"python3 -Oum {clazz.__module__} {clazz.__name__}.{test_id}"


def load_test_cases_in(
Expand All @@ -104,21 +108,48 @@ def load_test_cases_in(
return tests


def load_test_cases_from_cmake(ctest_test):
def load_py_test_cases_from_cmake(ctest_test):
ppath = ctest_test.env.get("PYTHONPATH", "")
bits = ctest_test.cmd.split(" ")
idx = [i for i, x in enumerate(bits) if "python3" in x][0]
prefix = " ".join(bits[:idx])
with extend_sys_path([ctest_test.working_dir] + ppath.split(env_sep())):
pyfile = ctest_test.cmd.split(" ")[-1]
pyfile = bits[-1]
return load_test_cases_in(
classes_in_file(pyfile, unittest.TestCase, fail_on_import_error=True),
env=ctest_test.env,
working_dir=ctest_test.working_dir,
log_file_path=_LOG_DIR
/ ".phlop"
/ f"{Path(ctest_test.working_dir).relative_to(_LOG_DIR)}",
test_cmd_pre=CMD_PREFIX + prefix + CMD_POSTFIX,
)


# probably return a list of TestBatch if we do some core count detection per test
def determine_cores_for_test_case(test_case):
cores = 1

try:
if "mpirun -n" in test_case.cmd:
bits = test_case.cmd.split(" ")
# print(bits)
idx = [i for i, x in enumerate(bits) if "mpirun" in x][0]
test_case.cores = int(bits[idx + 2])
except Exception as e:
print("EXXXX", e)

return test_case


def binless(test_case):
if test_case.cmd.startswith("/usr/bin/"):
test_case.cmd = test_case.cmd[9:]
return test_case


MUTATORS = [determine_cores_for_test_case, binless]


def load_cmake_tests(cmake_dir, cores=1, test_cmd_pre="", test_cmd_post=""):
cmake_tests = get_cmake_tests(cmake_dir)
tests = []
Expand All @@ -131,12 +162,22 @@ def load_cmake_tests(cmake_dir, cores=1, test_cmd_pre="", test_cmd_post=""):
)
]

test_batches = {}

def _add(test_cases):
for test_case in test_cases:
for mutator in MUTATORS:
test_case = mutator(test_case)
if test_case.cores not in test_batches:
test_batches[test_case.cores] = []
test_batches[test_case.cores].append(test_case)

test_cases = []
for test in tests:
for extractor in EXTRACTORS:
res = extractor(test)
if res:
test_cases += res
_add(res)
break

return TestBatch(test_cases, cores)
return [TestBatch(v, k) for k, v in test_batches.items()]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "phlop"
version = "0.0.8"
version = "0.0.9"

dependencies = [

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="phlop",
version="0.0.8",
version="0.0.9",
cmdclass={},
classifiers=[],
include_package_data=True,
Expand Down

0 comments on commit aa33c4c

Please sign in to comment.