Skip to content

Commit

Permalink
Fixes returning raw string from baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonb5 committed Oct 14, 2023
1 parent ccb3b4e commit b856aa1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
12 changes: 7 additions & 5 deletions CIME/baselines/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ def read_baseline_file(baseline_file):
"""
Reads value from `baseline_file`.
Strips comments and returns the raw content to be decoded.
Parameters
----------
baseline_file : str
Expand All @@ -430,7 +432,7 @@ def read_baseline_file(baseline_file):
with open(baseline_file) as fd:
lines = [x.strip() for x in fd.readlines() if not x.startswith("#")]

return lines
return "\n".join(lines)


def _perf_compare_throughput_baseline(case, baseline, tolerance):
Expand Down Expand Up @@ -459,8 +461,8 @@ def _perf_compare_throughput_baseline(case, baseline, tolerance):

try:
# default baseline is stored as single float
baseline = float(baseline[0])
except IndexError:
baseline = float(baseline)
except ValueError:
comment = "Could not compare throughput to baseline, as basline had no value."

return None, comment
Expand Down Expand Up @@ -516,8 +518,8 @@ def _perf_compare_memory_baseline(case, baseline, tolerance):

try:
# default baseline is stored as single float
baseline = float(baseline[0])
except IndexError:
baseline = float(baseline)
except ValueError:
baseline = 0.0

try:
Expand Down
27 changes: 13 additions & 14 deletions CIME/tests/test_unit_baselines_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ def test_get_cpl_mem_usage(self, isfile):

def test_read_baseline_file_multi_line(self):
with mock.patch(
"builtins.open", mock.mock_open(read_data="1000.0\n2000.0\n")
"builtins.open",
mock.mock_open(read_data="#comment about data\n1000.0\n2000.0\n"),
) as mock_file:
baseline = performance.read_baseline_file("/tmp/cpl-mem.log")

mock_file.assert_called_with("/tmp/cpl-mem.log")
assert baseline == ["1000.0", "2000.0"]
assert baseline == "1000.0\n2000.0"

def test_read_baseline_file_content(self):
with mock.patch(
Expand All @@ -168,14 +169,14 @@ def test_read_baseline_file_content(self):
baseline = performance.read_baseline_file("/tmp/cpl-mem.log")

mock_file.assert_called_with("/tmp/cpl-mem.log")
assert baseline == ["1000.0"]
assert baseline == "1000.0"

def test_read_baseline_file(self):
with mock.patch("builtins.open", mock.mock_open(read_data="")) as mock_file:
baseline = performance.read_baseline_file("/tmp/cpl-mem.log")

mock_file.assert_called_with("/tmp/cpl-mem.log")
assert baseline == []
assert baseline == ""

def test_write_baseline_file(self):
with mock.patch("builtins.open", mock.mock_open()) as mock_file:
Expand Down Expand Up @@ -316,7 +317,7 @@ def test_perf_compare_throughput_baseline_no_baseline_file(
def test_perf_compare_throughput_baseline_no_baseline(
self, get_latest_cpl_logs, read_baseline_file, _perf_get_throughput
):
read_baseline_file.return_value = []
read_baseline_file.return_value = ""

_perf_get_throughput.return_value = 504

Expand Down Expand Up @@ -348,9 +349,7 @@ def test_perf_compare_throughput_baseline_no_baseline(
def test_perf_compare_throughput_baseline_no_tolerance(
self, get_latest_cpl_logs, read_baseline_file, _perf_get_throughput
):
read_baseline_file.return_value = [
"500",
]
read_baseline_file.return_value = "500"

_perf_get_throughput.return_value = 504

Expand Down Expand Up @@ -382,7 +381,7 @@ def test_perf_compare_throughput_baseline_no_tolerance(
def test_perf_compare_throughput_baseline_above_threshold(
self, get_latest_cpl_logs, read_baseline_file, _perf_get_throughput
):
read_baseline_file.return_value = ["1000"]
read_baseline_file.return_value = "1000"

_perf_get_throughput.return_value = 504

Expand Down Expand Up @@ -413,7 +412,7 @@ def test_perf_compare_throughput_baseline_above_threshold(
def test_perf_compare_throughput_baseline(
self, get_latest_cpl_logs, read_baseline_file, _perf_get_throughput
):
read_baseline_file.return_value = ["500"]
read_baseline_file.return_value = "500"

_perf_get_throughput.return_value = 504

Expand Down Expand Up @@ -445,7 +444,7 @@ def test_perf_compare_throughput_baseline(
def test_perf_compare_memory_baseline_no_baseline(
self, get_latest_cpl_logs, read_baseline_file, get_cpl_mem_usage
):
read_baseline_file.return_value = []
read_baseline_file.return_value = ""

get_cpl_mem_usage.return_value = [
(1, 1000.0),
Expand Down Expand Up @@ -538,7 +537,7 @@ def test_perf_compare_memory_baseline_no_baseline_file(
def test_perf_compare_memory_baseline_no_tolerance(
self, get_latest_cpl_logs, read_baseline_file, get_cpl_mem_usage
):
read_baseline_file.return_value = ["1000.0"]
read_baseline_file.return_value = "1000.0"

get_cpl_mem_usage.return_value = [
(1, 1000.0),
Expand Down Expand Up @@ -573,7 +572,7 @@ def test_perf_compare_memory_baseline_no_tolerance(
def test_perf_compare_memory_baseline_above_threshold(
self, get_latest_cpl_logs, read_baseline_file, get_cpl_mem_usage
):
read_baseline_file.return_value = ["1000.0"]
read_baseline_file.return_value = "1000.0"

get_cpl_mem_usage.return_value = [
(1, 2000.0),
Expand Down Expand Up @@ -608,7 +607,7 @@ def test_perf_compare_memory_baseline_above_threshold(
def test_perf_compare_memory_baseline(
self, get_latest_cpl_logs, read_baseline_file, get_cpl_mem_usage
):
read_baseline_file.return_value = ["1000.0"]
read_baseline_file.return_value = "1000.0"

get_cpl_mem_usage.return_value = [
(1, 1000.0),
Expand Down

0 comments on commit b856aa1

Please sign in to comment.