Skip to content

Commit 94d0d8b

Browse files
committed
Return latest value in read_baseline_file
Also add black formatting and update unit tests.
1 parent bb7022e commit 94d0d8b

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

CIME/baselines/performance.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,11 @@ def read_baseline_file(baseline_file):
428428
Value stored in baseline file without comments.
429429
"""
430430
with open(baseline_file) as fd:
431-
lines = [x.strip().split(" ")[-1] for x in fd.readlines() if not x.startswith("#")]
431+
lines = [
432+
x.strip().split(" ")[-1] for x in fd.readlines() if not x.startswith("#")
433+
]
432434

433-
return "\n".join(lines)
435+
return lines[-1]
434436

435437

436438
def _perf_compare_throughput_baseline(case, baseline, tolerance):
@@ -474,7 +476,7 @@ def _perf_compare_throughput_baseline(case, baseline, tolerance):
474476
below_tolerance = diff < tolerance
475477

476478
info = "Throughput changed by {:.2f}%: baseline={:.3f} sypd, tolerance={:d}%, current={:.3f} sypd".format(
477-
diff * 100, baseline, int(tolerance * 100), current
479+
diff * 100, baseline, int(tolerance * 100), current
478480
)
479481
if below_tolerance:
480482
comment = "TPUTCOMP: " + info
@@ -531,8 +533,8 @@ def _perf_compare_memory_baseline(case, baseline, tolerance):
531533
if diff is not None:
532534
below_tolerance = diff < tolerance
533535

534-
info = "Memory usage highwater changed by {:.2f}%: baseline={:.3f} sypd, tolerance={:d}%, current={:.3f} sypd".format(
535-
diff * 100, baseline, int(tolerance * 100), current
536+
info = "Memory usage highwater changed by {:.2f}%: baseline={:.3f} MB, tolerance={:d}%, current={:.3f} MB".format(
537+
diff * 100, baseline, int(tolerance * 100), current
536538
)
537539
if below_tolerance:
538540
comment = "MEMCOMP: " + info

CIME/tests/test_unit_baselines_performance.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,18 @@ def test_get_cpl_mem_usage(self, isfile):
155155
def test_read_baseline_file_multi_line(self):
156156
with mock.patch(
157157
"builtins.open",
158-
mock.mock_open(read_data="#comment about data\n1000.0\n2000.0\n"),
158+
mock.mock_open(
159+
read_data="sha:1df0 date:2023 1000.0\nsha:3b05 date:2023 2000.0"
160+
),
159161
) as mock_file:
160162
baseline = performance.read_baseline_file("/tmp/cpl-mem.log")
161163

162164
mock_file.assert_called_with("/tmp/cpl-mem.log")
163-
assert baseline == "1000.0\n2000.0"
165+
assert baseline == "2000.0"
164166

165167
def test_read_baseline_file_content(self):
166168
with mock.patch(
167-
"builtins.open", mock.mock_open(read_data="1000.0")
169+
"builtins.open", mock.mock_open(read_data="sha:1df0 date:2023 1000.0")
168170
) as mock_file:
169171
baseline = performance.read_baseline_file("/tmp/cpl-mem.log")
170172

@@ -176,14 +178,12 @@ def test_read_baseline_file(self):
176178
baseline = performance.read_baseline_file("/tmp/cpl-mem.log")
177179

178180
mock_file.assert_called_with("/tmp/cpl-mem.log")
179-
assert baseline == ""
180181

181182
def test_write_baseline_file(self):
182183
with mock.patch("builtins.open", mock.mock_open()) as mock_file:
183184
performance.write_baseline_file("/tmp/cpl-tput.log", "1000")
184185

185-
mock_file.assert_called_with("/tmp/cpl-tput.log", "w")
186-
mock_file.return_value.write.assert_called_with("1000")
186+
mock_file.assert_called_with("/tmp/cpl-tput.log", "a")
187187

188188
@mock.patch("CIME.baselines.performance.get_cpl_throughput")
189189
@mock.patch("CIME.baselines.performance.get_latest_cpl_logs")
@@ -368,7 +368,7 @@ def test_perf_compare_throughput_baseline_no_tolerance(
368368
assert below_tolerance
369369
assert (
370370
comment
371-
== "TPUTCOMP: Computation time changed by -0.80% relative to baseline"
371+
== "TPUTCOMP: Throughput changed by -0.80%: baseline=500.000 sypd, tolerance=10%, current=504.000 sypd"
372372
)
373373

374374
@mock.patch("CIME.baselines.performance._perf_get_throughput")
@@ -399,7 +399,8 @@ def test_perf_compare_throughput_baseline_above_threshold(
399399

400400
assert not below_tolerance
401401
assert (
402-
comment == "Error: TPUTCOMP: Computation time increase > 5% from baseline"
402+
comment
403+
== "Error: TPUTCOMP: Throughput changed by 49.60%: baseline=1000.000 sypd, tolerance=5%, current=504.000 sypd"
403404
)
404405

405406
@mock.patch("CIME.baselines.performance._perf_get_throughput")
@@ -431,7 +432,7 @@ def test_perf_compare_throughput_baseline(
431432
assert below_tolerance
432433
assert (
433434
comment
434-
== "TPUTCOMP: Computation time changed by -0.80% relative to baseline"
435+
== "TPUTCOMP: Throughput changed by -0.80%: baseline=500.000 sypd, tolerance=5%, current=504.000 sypd"
435436
)
436437

437438
@mock.patch("CIME.baselines.performance.get_cpl_mem_usage")
@@ -466,7 +467,7 @@ def test_perf_compare_memory_baseline_no_baseline(
466467
assert below_tolerance
467468
assert (
468469
comment
469-
== "MEMCOMP: Memory usage highwater has changed by 0.00% relative to baseline"
470+
== "MEMCOMP: Memory usage highwater changed by 0.00%: baseline=0.000 MB, tolerance=5%, current=1003.000 MB"
470471
)
471472

472473
@mock.patch("CIME.baselines.performance.get_cpl_mem_usage")
@@ -557,7 +558,7 @@ def test_perf_compare_memory_baseline_no_tolerance(
557558
assert below_tolerance
558559
assert (
559560
comment
560-
== "MEMCOMP: Memory usage highwater has changed by 0.30% relative to baseline"
561+
== "MEMCOMP: Memory usage highwater changed by 0.30%: baseline=1000.000 MB, tolerance=10%, current=1003.000 MB"
561562
)
562563

563564
@mock.patch("CIME.baselines.performance.get_cpl_mem_usage")
@@ -592,7 +593,7 @@ def test_perf_compare_memory_baseline_above_threshold(
592593
assert not below_tolerance
593594
assert (
594595
comment
595-
== "Error: Memory usage increase >5% from baseline's 1000.000000 to 2003.000000"
596+
== "Error: MEMCOMP: Memory usage highwater changed by 100.30%: baseline=1000.000 MB, tolerance=5%, current=2003.000 MB"
596597
)
597598

598599
@mock.patch("CIME.baselines.performance.get_cpl_mem_usage")
@@ -627,7 +628,7 @@ def test_perf_compare_memory_baseline(
627628
assert below_tolerance
628629
assert (
629630
comment
630-
== "MEMCOMP: Memory usage highwater has changed by 0.30% relative to baseline"
631+
== "MEMCOMP: Memory usage highwater changed by 0.30%: baseline=1000.000 MB, tolerance=5%, current=1003.000 MB"
631632
)
632633

633634
def test_get_latest_cpl_logs_found_multiple(self):

0 commit comments

Comments
 (0)