Skip to content

Commit

Permalink
Add more tests for import and diff views
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriPavela committed Sep 24, 2024
1 parent 5bf2437 commit 2b3fa99
Show file tree
Hide file tree
Showing 11 changed files with 19,649 additions and 39 deletions.
1 change: 0 additions & 1 deletion perun/cli_groups/import_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"--metadata",
"-md",
multiple=True,
default=None,
metavar="['KEY|VALUE|[DESCRIPTION]'] or [FILE.json]",
help="Describes a single metadata entry associated with the imported profiles as a "
"'key|value[|description]' string, or a JSON file that may contain multiple metadata entries "
Expand Down
38 changes: 24 additions & 14 deletions perun/profile/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def import_perf_from_stack(
@vcs_kit.lookup_minor_version
def import_elk_from_json(
import_entries: list[str],
metadata: tuple[str, ...] | None,
metadata: tuple[str, ...],
minor_version: str,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -234,7 +234,7 @@ def import_perf_profile(

def import_elk_profile(
resources: list[dict[str, Any]],
metadata: dict[str, Any],
metadata: dict[str, profile_helpers.ProfileMetadata],
minor_version: MinorVersion,
save_to_index: bool = False,
**kwargs: Any,
Expand Down Expand Up @@ -382,7 +382,9 @@ def get_machine_info(machine_info: str, import_dir: Path) -> dict[str, Any]:
return environment.get_machine_specification()


def extract_machine_info_from_elk_metadata(metadata: dict[str, Any]) -> dict[str, Any]:
def extract_machine_info_from_elk_metadata(
metadata: dict[str, profile_helpers.ProfileMetadata]
) -> dict[str, Any]:
"""Extracts the parts of the profile that correspond to machine info.
Note that not many is collected from the ELK formats, and it can vary greatly,
Expand All @@ -392,18 +394,28 @@ def extract_machine_info_from_elk_metadata(metadata: dict[str, Any]) -> dict[str
:return: machine info extracted from the profiles.
"""
machine_info = {
"architecture": metadata.get("machine.arch", "?"),
"system": metadata.get("machine.os", "?").capitalize(),
"release": metadata.get("extra.machine.platform", "?"),
"host": metadata.get("machine.hostname", "?"),
machine_info: dict[str, Any] = {
"architecture": metadata.get(
"machine.arch", profile_helpers.ProfileMetadata("", "?")
).value,
"system": str(
metadata.get("machine.os", profile_helpers.ProfileMetadata("", "?")).value
).capitalize(),
"release": metadata.get(
"extra.machine.platform", profile_helpers.ProfileMetadata("", "?")
).value,
"host": metadata.get("machine.hostname", profile_helpers.ProfileMetadata("", "?")).value,
"cpu": {
"physical": "?",
"total": metadata.get("machine.cpu-cores", "?"),
"total": metadata.get(
"machine.cpu-cores", profile_helpers.ProfileMetadata("", "?")
).value,
"frequency": "?",
},
"memory": {
"total_ram": metadata.get("machine.ram", "?"),
"total_ram": metadata.get(
"machine.ram", profile_helpers.ProfileMetadata("", "?")
).value,
"swap": "?",
},
"boot_info": "?",
Expand All @@ -415,7 +427,7 @@ def extract_machine_info_from_elk_metadata(metadata: dict[str, Any]) -> dict[str


def _import_metadata(
metadata: tuple[str, ...] | None, import_dir: Path
metadata: tuple[str, ...], import_dir: Path
) -> list[profile_helpers.ProfileMetadata]:
"""Parse the metadata entries from CLI and convert them to our internal representation.
Expand All @@ -425,8 +437,6 @@ def _import_metadata(
:return: a collection of parsed and converted metadata objects
"""
p_metadata: list[profile_helpers.ProfileMetadata] = []
if metadata is None:
return p_metadata
# Normalize the metadata string for parsing and/or opening the file
for metadata_str in map(str.strip, metadata):
if metadata_str.lower().endswith(".json"):
Expand Down Expand Up @@ -564,7 +574,7 @@ def _parse_perf_entry(
:return: the parsed profile, or None if the import entry is invalid.
"""
if len(entry) == 0:
if len(entry) == 0 or not entry[0]:
# Empty profile specification, warn
log.warn("Empty import profile specification. Skipping.")
return None
Expand Down
9 changes: 5 additions & 4 deletions perun/profile/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ProfileStat:
@classmethod
def from_string(
cls,
name: str = "[empty]",
name: str = "",
cmp: str = "",
unit: str = "#",
aggregate_by: str = "",
Expand All @@ -139,9 +139,10 @@ def from_string(
:return: A constructed ProfileStat object.
"""
if name == "[empty]":
if not name:
# Invalid stat specification, warn
perun_log.warn("Empty profile stat specification. Creating a dummy '[empty]' stat.")
name = "[empty]"
comparison_enum = ProfileStatComparison.str_to_comparison(cmp)
return cls(name, comparison_enum, unit, aggregate_by, description)

Expand Down Expand Up @@ -479,10 +480,10 @@ def compare_stats(
if comparison == ProfileStatComparison.HIGHER
else StatComparisonResult.TARGET_BETTER
)
elif value < other_value:
else:
# value < other_value
return (
StatComparisonResult.BASELINE_BETTER
if comparison == ProfileStatComparison.LOWER
else StatComparisonResult.TARGET_BETTER
)
return StatComparisonResult.UNEQUAL
13 changes: 7 additions & 6 deletions perun/utils/common/diff_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ def _format_exit_codes(exit_code: str | list[str] | list[int]) -> str:
"""
# Unify the exit code types
exit_codes: list[str] = []
if isinstance(exit_code, str):
exit_codes.append(exit_code)
if isinstance(exit_code, (str, int)):
exit_codes.append(str(exit_code))
else:
exit_codes = list(map(str, exit_code))
# Color exit codes that are not zero
Expand Down Expand Up @@ -386,8 +386,8 @@ def _color_stat_record_diff(
if comparison_result == pstats.StatComparisonResult.INVALID:
baseline_value, target_value = "invalid comparison", "invalid comparison"
else:
baseline_value = _format_stat_value(lhs_stat_agg.as_table()[0])
target_value = _format_stat_value(rhs_stat_agg.as_table()[0])
baseline_value = _format_stat_value(lhs_stat_agg.as_table(compare_key)[0])
target_value = _format_stat_value(rhs_stat_agg.as_table(compare_key)[0])
return _emphasize(baseline_value, baseline_color), _emphasize(target_value, target_color)


Expand Down Expand Up @@ -443,8 +443,9 @@ def from_stat(
# The standard construction
stat_agg = pstats.aggregate_stats(stat)
unit = f" [{stat.unit}]" if stat.unit else ""
name = f"{stat.name}{unit} " f"({stat_agg.normalize_aggregate_key(stat.aggregate_by)})"
value, details = stat_agg.as_table()
agg_key = stat_agg.normalize_aggregate_key(stat.aggregate_by)
name = f"{stat.name}{unit} " f"({agg_key})"
value, details = stat_agg.as_table(agg_key)
tooltip = stat_description_to_tooltip(
stat.description, stat_agg.infer_auto_comparison(stat.cmp)
)
Expand Down
Loading

0 comments on commit 2b3fa99

Please sign in to comment.