speccpu: add benchdir related utils

This commit is contained in:
2025-04-23 13:36:19 +08:00
parent ce177285d5
commit 075ad5d8c8
2 changed files with 110 additions and 1 deletions

View File

@@ -117,3 +117,49 @@ class SPEC:
*(["--outputdir", str(output_root)] if output_root else []),
*benchmarks,
]
def check_bench_dir(dir: Path) -> bool:
"""
Check if a directory has the expected SPEC benchmark structure.
Parameters
----------
dir : Path
The directory to check
Returns
-------
bool
True if the directory contains 'build', 'exe', and 'run' subdirectories,
False otherwise
"""
files = os.listdir(dir)
return "build" in files and "exe" in files and "run" in files
def get_benchspec_dir(spec_output: Path) -> Path:
"""
Get the directory containing CPU benchmarks in the SPEC CPU output.
This function returns the path to the CPU benchmarks directory within the SPEC CPU
output directory structure.
Parameters
----------
spec_output : Path
The path to the SPEC CPU output directory.
Returns
-------
Path
The path to the CPU benchmarks directory.
Examples
--------
>>> from pathlib import Path
>>> spec_dir = Path("/path/to/spec/output")
>>> get_benchspec_dir(spec_dir)
PosixPath('/path/to/spec/output/benchspec/CPU')
"""
return spec_output / "benchspec" / "CPU"

View File

@@ -3,7 +3,7 @@ from unittest import mock
import pytest
from speccpu import SPEC, find_build
from speccpu import SPEC, check_bench_dir, find_build
class TestFindBuild:
@@ -79,3 +79,66 @@ class TestSPEC:
cmd = spec.mkcmd_runcpu("myconfig", ["benchmark1"], output_root=output_dir)
assert "--outputdir" in cmd
assert str(output_dir) in cmd
class TestCheckBenchDir:
def test_valid_bench_dir(self, tmpdir):
"""Test that check_bench_dir returns True for a valid benchmark directory."""
bench_dir = Path(tmpdir)
# Create the required subdirectories
(bench_dir / "build").mkdir()
(bench_dir / "exe").mkdir()
(bench_dir / "run").mkdir()
# Add some extra files/directories to make sure they don't affect the result
(bench_dir / "other_dir").mkdir()
(bench_dir / "file.txt").write_text("test")
assert check_bench_dir(bench_dir) is True
def test_invalid_bench_dir_missing_build(self, tmpdir):
"""Test that check_bench_dir returns False when 'build' directory is missing."""
bench_dir = Path(tmpdir)
# Create only exe and run directories
(bench_dir / "exe").mkdir()
(bench_dir / "run").mkdir()
assert check_bench_dir(bench_dir) is False
def test_invalid_bench_dir_missing_exe(self, tmpdir):
"""Test that check_bench_dir returns False when 'exe' directory is missing."""
bench_dir = Path(tmpdir)
# Create only build and run directories
(bench_dir / "build").mkdir()
(bench_dir / "run").mkdir()
assert check_bench_dir(bench_dir) is False
def test_invalid_bench_dir_missing_run(self, tmpdir):
"""Test that check_bench_dir returns False when 'run' directory is missing."""
bench_dir = Path(tmpdir)
# Create only build and exe directories
(bench_dir / "build").mkdir()
(bench_dir / "exe").mkdir()
assert check_bench_dir(bench_dir) is False
def test_empty_dir(self, tmpdir):
"""Test that check_bench_dir returns False for an empty directory."""
bench_dir = Path(tmpdir)
assert check_bench_dir(bench_dir) is False
def test_invalid_dir_case_sensitive(self, tmpdir):
"""Test that check_bench_dir is case-sensitive."""
bench_dir = Path(tmpdir)
# Create directories with different case
(bench_dir / "Build").mkdir()
(bench_dir / "EXE").mkdir()
(bench_dir / "RUN").mkdir()
assert check_bench_dir(bench_dir) is False