Skip to content

Commit 5f6860a

Browse files
committed
Added pytest for tdp_finder
1 parent 5b94105 commit 5f6860a

2 files changed

Lines changed: 65 additions & 5 deletions

File tree

execution_process_metrics_collector/collector.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ def execution_metrics_collector(
255255
sleep_secs: "float" = 1,
256256
timestamp_format: "str" = "%Y-%m-%d %H:%M:%S",
257257
match_docker: "bool" = False,
258-
) -> "None":
258+
) -> "pathlib.Path":
259259
pop = subprocess.Popen(cmdline)
260-
process_metrics_collector(
260+
metrics_path = process_metrics_collector(
261261
pop.pid,
262262
reldatadir,
263263
sleep_secs=sleep_secs,
@@ -267,14 +267,16 @@ def execution_metrics_collector(
267267
# Just for completeness
268268
pop.wait()
269269

270+
return metrics_path
271+
270272

271273
def process_metrics_collector(
272274
pid: "int",
273275
reldatadir: "pathlib.Path",
274276
sleep_secs: "float" = 1,
275277
timestamp_format: "str" = "%Y-%m-%d %H:%M:%S",
276278
match_docker: "bool" = False,
277-
) -> "None":
279+
) -> "pathlib.Path":
278280
# If the process does not exist, it will raise a psutil.NoSuchProcess exception
279281
try:
280282
p = psutil.Process(pid)
@@ -669,6 +671,8 @@ def process_metrics_collector(
669671

670672
time.sleep(sleep_secs)
671673

674+
return dir_name
675+
672676

673677
def main() -> "None":
674678
if len(sys.argv) >= 3:

tests/core/test_collector.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import pytest
2222
import inspect
2323
import pathlib
24+
import shutil
25+
import subprocess
2426

2527
from execution_process_metrics_collector.collector import execution_metrics_collector
28+
from execution_process_metrics_collector.tdp_finder import tdp_finder
2629

2730
from typing import (
2831
TYPE_CHECKING,
@@ -33,6 +36,9 @@
3336
Optional,
3437
Sequence,
3538
)
39+
from typing_extensions import (
40+
Final,
41+
)
3642

3743

3844
COLLECTOR_TESTBED = pytest.mark.parametrize(
@@ -43,14 +49,15 @@
4349
)
4450

4551

52+
@pytest.mark.filterwarnings("ignore:.*:pytest.PytestReturnNotNoneWarning")
4653
@COLLECTOR_TESTBED
4754
def test_collector(
4855
tmpdir: "str",
4956
command_line: "Sequence[str]",
5057
should_fail: "Optional[Sequence[str]]",
51-
) -> "None":
58+
) -> "pathlib.Path":
5259
try:
53-
execution_metrics_collector(
60+
metrics_path = execution_metrics_collector(
5461
command_line,
5562
pathlib.Path(tmpdir),
5663
match_docker=True,
@@ -73,3 +80,52 @@ def test_collector(
7380
raise AssertionError(
7481
f"Method {current_frame.f_code.co_name} should have failed with command line {command_line}"
7582
) # type: ignore[union-attr]
83+
84+
return metrics_path
85+
86+
87+
CPU_SPEC_DATASET_REPO: "Final[str]" = "https://github.com/JosuaCarl/cpu-spec-dataset"
88+
89+
90+
@COLLECTOR_TESTBED
91+
def test_tdp_finder(
92+
tmpdir: "str",
93+
command_line: "Sequence[str]",
94+
should_fail: "Optional[Sequence[str]]",
95+
) -> "None":
96+
metrics_path = test_collector(tmpdir, command_line, should_fail)
97+
98+
spec_path = pathlib.Path(tmpdir) / "cpu_spec_dataset"
99+
if not spec_path.exists():
100+
git_path = shutil.which("git")
101+
assert git_path is not None, "git not found"
102+
subprocess.run(
103+
[git_path, "clone", CPU_SPEC_DATASET_REPO, spec_path.as_posix()], check=True
104+
)
105+
106+
assert spec_path.is_dir()
107+
108+
processors_file = spec_path / "dataset" / "intel-cpus.csv"
109+
110+
assert processors_file.exists()
111+
112+
try:
113+
tdp_finder(metrics_path, processors_file)
114+
except BaseException:
115+
current_frame = inspect.currentframe()
116+
if (
117+
should_fail is None
118+
or current_frame is None
119+
or current_frame.f_code.co_name not in should_fail
120+
): # type: ignore[union-attr]
121+
raise
122+
else:
123+
current_frame = inspect.currentframe()
124+
if (
125+
should_fail is not None
126+
and current_frame is not None
127+
and current_frame.f_code.co_name in should_fail
128+
): # type: ignore[union-attr]
129+
raise AssertionError(
130+
f"Method {current_frame.f_code.co_name} should have failed with command line {command_line}"
131+
) # type: ignore[union-attr]

0 commit comments

Comments
 (0)