From 7a2df64b590df32599e12fd3585e2a75d62e4a06 Mon Sep 17 00:00:00 2001 From: vedithal-amd Date: Thu, 4 Dec 2025 10:12:38 -0500 Subject: [PATCH] [rocprofiler-compute] Enable running tests from installation only for TheRock setup (#2067) * Enable running tests from installation only * Use cmake option -DTEST_FROM_INSTALL=ON to enable running tests from installation folder only * It is not possible to run tests from build folder in this case * This option prevents changing working directory to source folder * Fix SourceFileLoader to import rocprof-compute main module correctly * Install sample executables in the test folder * fix num_xcds_cli_output test * Fix tests * Skip autogen. config. test and add a TODO task for re-design of this test * Add flexible import of source code in test_gpu_specs.py * Update cmake to install tests/workloads folder when INSTALL_TESTS=ON * Fix sys.argv[0] for tests * fix live attach detach test --- projects/rocprofiler-compute/CMakeLists.txt | 101 ++++++++++-------- .../rocprofiler-compute/tests/conftest.py | 13 ++- .../tests/test_TCP_counters.py | 5 - .../tests/test_autogen_config.py | 7 ++ .../tests/test_gpu_specs.py | 30 +++--- .../tests/test_profile_general.py | 2 +- 6 files changed, 92 insertions(+), 66 deletions(-) diff --git a/projects/rocprofiler-compute/CMakeLists.txt b/projects/rocprofiler-compute/CMakeLists.txt index 4e0877c407..f06c00cfda 100644 --- a/projects/rocprofiler-compute/CMakeLists.txt +++ b/projects/rocprofiler-compute/CMakeLists.txt @@ -233,6 +233,21 @@ set(PYTEST_NUMPROCS_ANALYSIS ) message(STATUS "Pytest CPU threadcount: ${PYTEST_NUMPROCS_ANALYSIS}") +# --------------------------- +# add tests +# --------------------------- + +option( + TEST_FROM_INSTALL + "Test from install directory rather than from source directory" + OFF +) +if(TEST_FROM_INSTALL) + set(WORKING_DIR_OPTION "") +else() + set(WORKING_DIR_OPTION WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) +endif() + # --------------------------- # profile mode tests # --------------------------- @@ -242,8 +257,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m kernel_execution --junitxml=tests/test_profile_kernel_execution.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -251,48 +265,42 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m dispatch --junitxml=tests/test_profile_dispatch.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( NAME test_profile_mem COMMAND ${Python3_EXECUTABLE} -m pytest -m mem --junitxml=tests/test_profile_mem.xml - ${COV_OPTION} ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ${COV_OPTION} tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( NAME test_profile_join COMMAND ${Python3_EXECUTABLE} -m pytest -m join --junitxml=tests/test_profile_join.xml - ${COV_OPTION} ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ${COV_OPTION} tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( NAME test_profile_sort COMMAND ${Python3_EXECUTABLE} -m pytest -m sort --junitxml=tests/test_profile_sort.xml - ${COV_OPTION} ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ${COV_OPTION} tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( NAME test_profile_misc COMMAND ${Python3_EXECUTABLE} -m pytest -m misc --junitxml=tests/test_profile_misc.xml - ${COV_OPTION} ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ${COV_OPTION} tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( NAME test_profile_path COMMAND ${Python3_EXECUTABLE} -m pytest -m path --junitxml=tests/test_profile_path.xml - ${COV_OPTION} ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ${COV_OPTION} tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -300,8 +308,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m roofline_1 --junitxml=tests/test_profile_roofline_1.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -309,8 +316,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m roofline_2 --junitxml=tests/test_profile_roofline_2.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -318,8 +324,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m section --junitxml=tests/test_profile_section.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -327,8 +332,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -s -m pc_sampling --junitxml=tests/test_profile_pc_sampling.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -336,8 +340,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m sets_func --junitxml=tests/test_profile_sets_func.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -345,8 +348,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -s -m live_attach_detach --junitxml=tests/test_profile_live_attach_detach.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) add_test( @@ -354,8 +356,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -s -m iteration_multiplexing --junitxml=tests/test_profile_iteration_multiplexing.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_profile_general.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_profile_general.py ${WORKING_DIR_OPTION} ) set_tests_properties( @@ -385,8 +386,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -n ${PYTEST_NUMPROCS_ANALYSIS} --verbose --junitxml=tests/test_analyze_commands.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_analyze_commands.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_analyze_commands.py ${WORKING_DIR_OPTION} ) # --------------------------- @@ -398,8 +398,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -n ${PYTEST_NUMPROCS} --junitxml=tests/test_analyze_workloads.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_analyze_workloads.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_analyze_workloads.py ${WORKING_DIR_OPTION} ) # --------------------------- @@ -411,8 +410,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m L1_cache --junitxml=tests/test_L1_cache_counters.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_TCP_counters.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_TCP_counters.py ${WORKING_DIR_OPTION} ) # --------------------------- @@ -424,8 +422,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m num_xcds_spec_class --junitxml=tests/test_num_xcds_spec_class.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_gpu_specs.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_gpu_specs.py ${WORKING_DIR_OPTION} ) add_test( @@ -433,8 +430,7 @@ add_test( COMMAND ${Python3_EXECUTABLE} -m pytest -m num_xcds_cli_output --junitxml=tests/test_num_xcds_cli_output.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_gpu_specs.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_gpu_specs.py ${WORKING_DIR_OPTION} ) # --------------------------- @@ -445,8 +441,7 @@ add_test( NAME test_utils COMMAND ${Python3_EXECUTABLE} -m pytest --junitxml=tests/test_utils.xml ${COV_OPTION} - ${PROJECT_SOURCE_DIR}/tests/test_utils.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + tests/test_utils.py ${WORKING_DIR_OPTION} ) # ----------------------------------- @@ -457,8 +452,7 @@ add_test( NAME test_autogen_config COMMAND ${Python3_EXECUTABLE} -m pytest --junitxml=tests/test_autogen_config.xml - ${COV_OPTION} ${PROJECT_SOURCE_DIR}/tests/test_autogen_config.py - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ${COV_OPTION} tests/test_autogen_config.py ${WORKING_DIR_OPTION} ) # ----------------------------------- @@ -491,8 +485,9 @@ if(${ENABLE_COVERAGE}) add_test( NAME generate_coverage_report - COMMAND ${Python3_EXECUTABLE} -m coverage xml -o ${CMAKE_BINARY_DIR}/coverage.xml - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND + ${Python3_EXECUTABLE} -m coverage xml -o ${CMAKE_BINARY_DIR}/coverage.xml + ${WORKING_DIR_OPTION} ) set_tests_properties( @@ -688,6 +683,24 @@ if(INSTALL_TESTS) PATTERN "*.py" PATTERN "__pycache__" EXCLUDE ) + install( + DIRECTORY tests/workloads + DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/${PROJECT_NAME}/tests + COMPONENT tests + PATTERN "__pycache__" EXCLUDE + ) + install( + PROGRAMS + tests/vcopy + tests/vmem + tests/vrandom_access + tests/vsequential_access + tests/occupancy + tests/hip_dynamic_shared + tests/mat_mul_max + DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/${PROJECT_NAME}/tests + COMPONENT tests + ) install( FILES requirements-test.txt DESTINATION ${CMAKE_INSTALL_LIBEXECDIR}/${PROJECT_NAME} diff --git a/projects/rocprofiler-compute/tests/conftest.py b/projects/rocprofiler-compute/tests/conftest.py index 0c231ce221..72e3d327fe 100644 --- a/projects/rocprofiler-compute/tests/conftest.py +++ b/projects/rocprofiler-compute/tests/conftest.py @@ -37,9 +37,14 @@ SRC = os.path.join(ROOT, "src") if SRC not in sys.path: sys.path.insert(0, SRC) -rocprof_compute = SourceFileLoader( - "rocprof-compute", "src/rocprof-compute" -).load_module() +try: + rocprof_compute = SourceFileLoader( + "rocprof-compute", "src/rocprof-compute" + ).load_module() +except Exception: + rocprof_compute = SourceFileLoader( + "rocprof-compute", "rocprof-compute" + ).load_module() def pytest_addoption(parser): @@ -116,7 +121,7 @@ def binary_handler_profile_rocprof_compute(request): return process.returncode else: baseline_opts = [ - "install/bin/rocprof-compute", + "rocprof-compute", "profile", "-n", app_name, diff --git a/projects/rocprofiler-compute/tests/test_TCP_counters.py b/projects/rocprofiler-compute/tests/test_TCP_counters.py index 90445e10b6..5d35b40a32 100644 --- a/projects/rocprofiler-compute/tests/test_TCP_counters.py +++ b/projects/rocprofiler-compute/tests/test_TCP_counters.py @@ -26,16 +26,11 @@ import csv import re import subprocess -from importlib.machinery import SourceFileLoader from pathlib import Path import pytest import test_utils -rocprof_compute = SourceFileLoader( - "rocprof-compute", "src/rocprof-compute" -).load_module() - config = {} config["vseq"] = ["./tests/vsequential_access"] config["vrand"] = ["./tests/vrandom_access"] diff --git a/projects/rocprofiler-compute/tests/test_autogen_config.py b/projects/rocprofiler-compute/tests/test_autogen_config.py index 9ef3d3325f..b30d83e1f1 100644 --- a/projects/rocprofiler-compute/tests/test_autogen_config.py +++ b/projects/rocprofiler-compute/tests/test_autogen_config.py @@ -26,9 +26,16 @@ import hashlib from pathlib import Path +import pytest import yaml +@pytest.mark.skip( + reason=( + "TODO: Skip this test until we use " + "tools/config_management/.config.hashes.json for testing" + ) +) def test_modification_time(): # Ensure hash map consistency hash_path = Path("tools/autogen_hash.yaml") diff --git a/projects/rocprofiler-compute/tests/test_gpu_specs.py b/projects/rocprofiler-compute/tests/test_gpu_specs.py index b77bd01367..ebe6dbca82 100644 --- a/projects/rocprofiler-compute/tests/test_gpu_specs.py +++ b/projects/rocprofiler-compute/tests/test_gpu_specs.py @@ -26,16 +26,14 @@ import re import subprocess import tempfile -from importlib.machinery import SourceFileLoader from unittest.mock import patch import pytest -from src.utils.specs import generate_machine_specs - -rocprof_compute = SourceFileLoader( - "rocprof-compute", "src/rocprof-compute" -).load_module() +try: + from src.utils.specs import generate_machine_specs +except Exception: + from utils.specs import generate_machine_specs # NOTE: Only testing gfx942 for now. GFX942_CHIP_IDS_TO_NUM_XCDS = { @@ -162,12 +160,20 @@ def test_num_xcds_cli_output(): num_xcds = get_num_xcds() # 2. Run rocprof-compute -s and grab rocprof-compute num_xcd - proc = subprocess.run( - ["src/rocprof-compute", "-s"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - ) + try: + proc = subprocess.run( + ["src/rocprof-compute", "-s"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + except Exception: + proc = subprocess.run( + ["rocprof-compute", "-s"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) assert proc.returncode == 0, ( f"Non-zero exit ({proc.returncode}), stderr:\n{proc.stderr}" ) diff --git a/projects/rocprofiler-compute/tests/test_profile_general.py b/projects/rocprofiler-compute/tests/test_profile_general.py index 5d908b0e3f..c2ad5dcc2c 100644 --- a/projects/rocprofiler-compute/tests/test_profile_general.py +++ b/projects/rocprofiler-compute/tests/test_profile_general.py @@ -72,7 +72,7 @@ config["METRIC_LOGGING"] = False num_kernels = 3 num_devices = 1 -attach_detach_interval_msec_no_delay = 10000 +attach_detach_interval_msec_no_delay = 1000 attach_detach_interval_msec_with_delay = 60000 DEFAULT_ABS_DIFF = 15 DEFAULT_REL_DIFF = 50