Skip to content

Commit 31eb6b4

Browse files
committed
feat: ensure CPU and Wall time is not zero
1 parent 53e6d38 commit 31eb6b4

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

caso/record.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ def ssm_message(self):
232232
# done. In order to get objects correctly serialized we need to convert to JSON,
233233
# then reload the model
234234
serialized_record = json.loads(self.model_dump_json(**opts))
235+
# CPU and Wall duration may be 0 as we are converting float to int when creating
236+
# the record, here we impose at least 1 second to avoid reporting no cpu time
237+
for f in ["CpuDuration", "WallDuration"]:
238+
if f in serialized_record and serialized_record[f] == 0:
239+
serialized_record[f] = 1
235240
aux = [f"{k}: {v}" for k, v in serialized_record.items()]
236241
aux.sort()
237242
return "\n".join(aux)

caso/tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ def valid_cloud_record() -> dict:
335335
return valid_cloud_records_dict[0]
336336

337337

338+
@pytest.fixture()
339+
def zero_cpu_cloud_record() -> caso.record.CloudRecord:
340+
"""Get a fixture for the CloudRecord with 0 cpu and wall time."""
341+
record = caso.record.CloudRecord(**valid_cloud_records_fields[0])
342+
record.cpu_duration = 0
343+
record.wall_duration = 0
344+
return record
345+
346+
338347
@pytest.fixture()
339348
def valid_cloud_records() -> typing.List[dict]:
340349
"""Get a fixture for valid records as a dict."""

caso/tests/test_record.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ def test_cloud_record_custom_cpu(cloud_record):
8888
assert cloud_record.cpu_duration == cpu
8989

9090

91+
def test_cloud_record_zero_cpu(zero_cpu_cloud_record):
92+
"""Test a cloud record with 0 CPU time is correctly rendered."""
93+
ssm_message = zero_cpu_cloud_record.ssm_message()
94+
assert "CpuDuration: 1" in ssm_message
95+
assert "WallDuration: 1" in ssm_message
96+
97+
9198
def test_ip_record(ip_record):
9299
"""Test that an IP record is correctly generated."""
93100
assert isinstance(ip_record.measure_time, datetime.datetime)

0 commit comments

Comments
 (0)