Skip to content

Commit c151b6c

Browse files
authored
Merge pull request #1383 from codeflash-ai/codeflash/optimize-pr1318-2026-02-04T14.10.57
⚡️ Speed up method `PrComment.to_json` by 329% in PR #1318 (`fix/js-jest30-loop-runner`)
2 parents 535c640 + d0b859a commit c151b6c

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

codeflash/code_utils/time_utils.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,32 @@ def humanize_runtime(time_in_ns: int) -> str:
1414

1515
if time_in_ns / 1000 >= 1:
1616
time_micro = float(time_in_ns) / 1000
17-
runtime_human = humanize.precisedelta(dt.timedelta(microseconds=time_micro), minimum_unit="microseconds")
18-
19-
units = re.split(r",|\s", runtime_human)[1]
20-
21-
if units in {"microseconds", "microsecond"}:
17+
18+
# Direct unit determination and formatting without external library
19+
if time_micro < 1000:
2220
runtime_human = f"{time_micro:.3g}"
23-
elif units in {"milliseconds", "millisecond"}:
24-
runtime_human = "%.3g" % (time_micro / 1000)
25-
elif units in {"seconds", "second"}:
26-
runtime_human = "%.3g" % (time_micro / (1000**2))
27-
elif units in {"minutes", "minute"}:
28-
runtime_human = "%.3g" % (time_micro / (60 * 1000**2))
29-
elif units in {"hour", "hours"}: # hours
30-
runtime_human = "%.3g" % (time_micro / (3600 * 1000**2))
21+
units = "microseconds" if time_micro >= 2 else "microsecond"
22+
elif time_micro < 1000000:
23+
time_milli = time_micro / 1000
24+
runtime_human = f"{time_milli:.3g}"
25+
units = "milliseconds" if time_milli >= 2 else "millisecond"
26+
elif time_micro < 60000000:
27+
time_sec = time_micro / 1000000
28+
runtime_human = f"{time_sec:.3g}"
29+
units = "seconds" if time_sec >= 2 else "second"
30+
elif time_micro < 3600000000:
31+
time_min = time_micro / 60000000
32+
runtime_human = f"{time_min:.3g}"
33+
units = "minutes" if time_min >= 2 else "minute"
34+
elif time_micro < 86400000000:
35+
time_hour = time_micro / 3600000000
36+
runtime_human = f"{time_hour:.3g}"
37+
units = "hours" if time_hour >= 2 else "hour"
3138
else: # days
32-
runtime_human = "%.3g" % (time_micro / (24 * 3600 * 1000**2))
39+
time_day = time_micro / 86400000000
40+
runtime_human = f"{time_day:.3g}"
41+
units = "days" if time_day >= 2 else "day"
42+
3343
runtime_human_parts = str(runtime_human).split(".")
3444
if len(runtime_human_parts[0]) == 1:
3545
if runtime_human_parts[0] == "1" and len(runtime_human_parts) > 1:

codeflash/models/test_type.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class TestType(Enum):
1010
INIT_STATE_TEST = 6
1111

1212
def to_name(self) -> str:
13-
if self is TestType.INIT_STATE_TEST:
14-
return ""
15-
return _TO_NAME_MAP[self]
13+
return _TO_NAME_MAP.get(self, "")
1614

1715

1816
_TO_NAME_MAP: dict[TestType, str] = {

0 commit comments

Comments
 (0)