Skip to content

Fix format_duration() truncating each timestamp before subtracting - #3536

Merged
cyberw merged 1 commit into
locustio:masterfrom
Lesereingrape:fix/format-duration-truncation
Oct 3, 2026
Merged

cyberw merged 1 commit into
locustio:masterfrom
Lesereingrape:fix/format-duration-truncation

Conversation

@Lesereingrape

Copy link
Copy Markdown
Contributor

format_duration() truncated both operands before subtracting them, so the fractional part is lost twice: int(1700000002.1) - int(1700000000.9) is 2 although only 1.2 s elapsed.

get_html_report() feeds it stats.start_time and stats.last_request_timestamp (locust/html.py:99), so the duration in the HTML report is a whole second too long whenever start_time has the larger fractional part, and two runs of identical length can be displayed differently depending on where the whole-second boundary falls.

This commit truncates once, on the difference. Inputs that already are whole seconds format exactly as before, so the values in the existing dates_checks table are unchanged.

Fixes #3535

How was it checked

On master at c6c51480, with the new test parametrization added and the source line untouched (baseline):

locust/test/test_date.py  ->  3 failed, 2 passed, 30 skipped
FAILED ...[1700000000.9-1700000002.1-1 second]   '2 seconds' != '1 second'
FAILED ...[1700000000.8-1700000001.4-0 seconds]  '1 second' != '0 seconds'
FAILED ...[1700000000.7-1700086401.3-1 day]      '1 day and 1 second' != '1 day'

after the one-line change:

locust/test/test_date.py  ->  5 passed, 30 skipped
ruff check locust/util/date.py locust/test/test_date.py  ->  All checks passed
ruff format --check (same files)  ->  2 files already formatted

locust/test/test_web.py -k "report or duration or timestamp" gives 7 failed, 1 passed both before and after this change on this Windows machine (pre-existing GET /stats/report 500s because the built web UI assets are missing locally), so no difference is introduced there.

Drafted with AI assistance in this session; no human reviewed it before submission.

@cyberw
cyberw merged commit 80c03a8 into locustio:master Oct 3, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

format_duration() inflates the duration by a second because it truncates each timestamp separately

2 participants