Skip to content

Commit

Permalink
Fix timestamp rounding & add tests on html report duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Briat committed Nov 3, 2024
1 parent 6ec2820 commit 03e13d2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
14 changes: 6 additions & 8 deletions locust/test/test_date.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down Expand Up @@ -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"]
3 changes: 3 additions & 0 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 4 additions & 7 deletions locust/util/date.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 03e13d2

Please sign in to comment.