From 03e13d2e6ecf17ac4d430983e1105a2bd42e13f2 Mon Sep 17 00:00:00 2001 From: Olivier Briat Date: Mon, 4 Nov 2024 00:28:32 +0100 Subject: [PATCH] Fix timestamp rounding & add tests on html report duration --- locust/test/test_date.py | 14 ++++++-------- locust/test/test_main.py | 3 +++ locust/util/date.py | 11 ++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/locust/test/test_date.py b/locust/test/test_date.py index 67c03e8374..756176dcf8 100644 --- a/locust/test/test_date.py +++ b/locust/test/test_date.py @@ -1,11 +1,9 @@ +from locust.util.date import format_duration, format_safe_timestamp, format_utc_timestamp + from datetime import datetime import pytest -from locust.util.date import format_duration, format_safe_timestamp, format_utc_timestamp - -from .testcases import LocustTestCase - dates_checks = [ { "datetime": datetime(2023, 10, 1, 12, 0, 0), @@ -72,17 +70,17 @@ @pytest.mark.parametrize("check", dates_checks) def test_format_utc_timestamp(check): - assert format_utc_timestamp(int(check["datetime"].timestamp())) == check["utc_timestamp"] + assert format_utc_timestamp(check["datetime"].timestamp()) == check["utc_timestamp"] @pytest.mark.parametrize("check", dates_checks) def test_format_safe_timestamp(check): - assert format_safe_timestamp(int(check["datetime"].timestamp())) == check["safe_timestamp"] + assert format_safe_timestamp(check["datetime"].timestamp()) == check["safe_timestamp"] @pytest.mark.parametrize("check", dates_checks) def test_format_duration(check): global dates_checks - start_time = int(dates_checks[0]["datetime"].timestamp()) - end_time = int(check["datetime"].timestamp()) + start_time = dates_checks[0]["datetime"].timestamp() + end_time = check["datetime"].timestamp() assert format_duration(start_time, end_time) == check["duration"] diff --git a/locust/test/test_main.py b/locust/test/test_main.py index aab8916f38..cf562bf5d9 100644 --- a/locust/test/test_main.py +++ b/locust/test/test_main.py @@ -1181,6 +1181,9 @@ def test_html_report_option(self): # make sure host appears in the report self.assertIn("https://test.com/", html_report_content) self.assertIn('"show_download_link": false', html_report_content) + self.assertRegex(html_report_content, r'"start_time": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"') + self.assertRegex(html_report_content, r'"end_time": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"') + self.assertRegex(html_report_content, r'"duration": "\d* seconds?"') def test_run_with_userclass_picker(self): with temporary_file(content=MOCK_LOCUSTFILE_CONTENT_A) as file1: diff --git a/locust/util/date.py b/locust/util/date.py index 505a71ab86..d29a761948 100644 --- a/locust/util/date.py +++ b/locust/util/date.py @@ -1,19 +1,16 @@ -import decimal -import numbers -import re -from datetime import datetime, timedelta, timezone +from datetime import datetime, timezone def format_utc_timestamp(unix_timestamp): - return datetime.fromtimestamp(unix_timestamp, timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + return datetime.fromtimestamp(int(unix_timestamp), timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") def format_safe_timestamp(unix_timestamp): - return datetime.fromtimestamp(unix_timestamp).strftime("%Y-%m-%d-%Hh%M") + return datetime.fromtimestamp(int(unix_timestamp)).strftime("%Y-%m-%d-%Hh%M") def format_duration(start_time, end_time): - seconds = end_time - start_time + seconds = int(end_time) - int(start_time) days = seconds // 86400 hours = (seconds % 86400) // 3600 minutes = (seconds % 3600) // 60