From 1a415414c7e1d3ff075de5667839957054db6aa9 Mon Sep 17 00:00:00 2001 From: obradovichv <53901450+obradovichv@users.noreply.github.com> Date: Mon, 16 Aug 2021 17:22:12 +0200 Subject: [PATCH 1/3] fix(l10n): use UTC time on server and localize time on client Locust uses server time to store the time of a task in stats history. When fetching the initial stats history for the client (such on on page refresh) it is written by the server using using this stored time. While rendering a chart on the client, locust uses the localized client time for the time the stats history was fetched. This would lead to a jump in the x-axis time series when server and client time zones differed. Stats history (stats.py) now derives time from the time component of the current datetime in UTC instead of server time zone. This aligns it with other time stores used throughout locust - particularly the start_time and end_time of reports - that make use of time.time(), which is in UTC. The client UI report (report.html) now localizes the server-rendered times from history (formerly in server TZ, now in UTC). This aligns it with the client-side report timekeeping of locust.js updateStats() used for the x-axis of charts. Issue: #1835 --- locust/stats.py | 2 +- locust/templates/stats_data.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/locust/stats.py b/locust/stats.py index 90b90a18b9..745360b397 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -791,7 +791,7 @@ def stats_history(runner): break if runner.state != "stopped": r = { - "time": datetime.datetime.now().strftime("%H:%M:%S"), + "time": datetime.datetime.utcnow().strftime("%H:%M:%S"), "current_rps": stats.total.current_rps or 0, "current_fail_per_sec": stats.total.current_fail_per_sec or 0, "response_time_percentile_95": stats.total.get_current_response_time_percentile(0.95) or 0, diff --git a/locust/templates/stats_data.html b/locust/templates/stats_data.html index 2219f1db38..8adc13b657 100644 --- a/locust/templates/stats_data.html +++ b/locust/templates/stats_data.html @@ -1,6 +1,6 @@ {% set time_data = [] %}{% set user_count_data = [] %}{% set current_rps_data = [] %}{% set current_fail_per_sec_data = [] %}{% set response_time_percentile_50_data = [] %}{% set response_time_percentile_95_data = [] %}{% for r in history %}{% do time_data.append(r.time) %}{% do user_count_data.append({"value": r.user_count}) %}{% do current_rps_data.append({"value": r.current_rps, "users": r.user_count}) %}{% do current_fail_per_sec_data.append({"value": r.current_fail_per_sec, "users": r.user_count}) %}{% do response_time_percentile_50_data.append({"value": r.response_time_percentile_50, "users": r.user_count}) %}{% do response_time_percentile_95_data.append({"value": r.response_time_percentile_95, "users": r.user_count}) %}{% endfor %} var stats_history = { - "time": {{ time_data | tojson }}, + "time": {{ time_data | tojson }}.map(server_time => new Date(new Date().setUTCHours(...(server_time.split(":")))).toLocaleTimeString()), "user_count": {{ user_count_data | tojson }}, "current_rps": {{ current_rps_data | tojson }}, "current_fail_per_sec": {{ current_fail_per_sec_data | tojson }}, From c1d7c3c4022ed80ee62c2536a986ff156008c30b Mon Sep 17 00:00:00 2001 From: obradovichv <53901450+obradovichv@users.noreply.github.com> Date: Mon, 16 Aug 2021 17:55:32 +0200 Subject: [PATCH 2/3] fix(l10n): localize report start and end datetimes The "during" start and end datetimes of "Download Report" (/stats/report) were using server-rendered times, i.e.: strftime("%Y-%m-%d %H:%M:%S"). When server and client time zones differed, the times of the report would not match the client time zone. These datetimes are now localized similarly to the rest of the report. --- locust/templates/report.html | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/locust/templates/report.html b/locust/templates/report.html index 98949431fd..69524007ce 100644 --- a/locust/templates/report.html +++ b/locust/templates/report.html @@ -44,7 +44,7 @@

Locust Test Report

{% if show_download_link %}

Download the Report

{% endif %} -

During: {{ start_time }} - {{ end_time }}

+

During: {{ start_time }} - {{ end_time }}

Target Host: {{ host }}

@@ -219,6 +219,20 @@

Charts

] }); } + + $(".l10n.datetime").html((index, currentContent) => { + if (!currentContent || !currentContent.includes(" ") || !currentContent.includes(":")) { + return currentContent; + } + + let [date, time] = currentContent.split(" "); + let utcDateTime = new Date(); + let [year, month, day] = date.split("-") + utcDateTime.setUTCFullYear(year, parseInt(month) - 1, day); + utcDateTime.setUTCHours(...(time.split(":"))); + + return utcDateTime.toLocaleString(); + }); From dccf5d493e7d3cbeed8f7f950f8e9c7553495b4f Mon Sep 17 00:00:00 2001 From: obradovichv <53901450+obradovichv@users.noreply.github.com> Date: Tue, 17 Aug 2021 00:23:39 +0000 Subject: [PATCH 3/3] refactor(l10n): parse datetime with built-in function Simplify date parsing. The date value is converted to ISO8601-like format in UTC (Z) and parsed using built-in Date.parse(). --- locust/templates/report.html | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/locust/templates/report.html b/locust/templates/report.html index 69524007ce..94b88174e3 100644 --- a/locust/templates/report.html +++ b/locust/templates/report.html @@ -225,13 +225,7 @@

Charts

return currentContent; } - let [date, time] = currentContent.split(" "); - let utcDateTime = new Date(); - let [year, month, day] = date.split("-") - utcDateTime.setUTCFullYear(year, parseInt(month) - 1, day); - utcDateTime.setUTCHours(...(time.split(":"))); - - return utcDateTime.toLocaleString(); + return new Date(Date.parse(currentContent.replace(" ", "T") + ".000Z")).toLocaleString(); });