From 5bdbb46b8b6acbb8585939b11819eff4dcc2f6e5 Mon Sep 17 00:00:00 2001 From: Ketok Date: Sun, 3 Sep 2023 12:34:29 +0200 Subject: [PATCH] Jitter --- src/speedtest.py | 8 ++++-- src/speedtest_worker.py | 6 ++--- src/ui/gauge.blp | 1 - src/ui/views/test.blp | 59 ++++++++++++++++++++++++++++------------- src/window.py | 8 +++++- 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/speedtest.py b/src/speedtest.py index 46c6886..a3a407f 100644 --- a/src/speedtest.py +++ b/src/speedtest.py @@ -84,11 +84,15 @@ async def check_server(server): async def ping(server): #TODO: jitter and other stuff async with aiohttp.ClientSession() as session: pings = [] - for _ in range(10): + jitters = [] + for i in range(10): start = time.time() async with session.get(server.pingURL, headers=HEADERS) as _: pings.append(time.time() - start) - return sum(pings) / len(pings) * 1000 + + if i != 0: + jitters.append(abs(pings[i] - pings[i - 1])) + return sum(pings) / len(pings) * 1000, sum(jitters) / len(jitters) * 1000 async def download(server, total): async with aiohttp.ClientSession() as session: diff --git a/src/speedtest_worker.py b/src/speedtest_worker.py index 12e1d1c..7a2c0ed 100644 --- a/src/speedtest_worker.py +++ b/src/speedtest_worker.py @@ -46,9 +46,9 @@ async def do_run(self): try: view = self.win.test_view - _ping = await ping(self.server) + _ping, jitter = await ping(self.server) - GLib.idle_add(setattr, view, "ping", str(round(_ping)) + "ms") + GLib.idle_add(view.update_ping, _ping, jitter) GLib.idle_add(view.progress.remove_css_class, "up") GLib.idle_add(view.progress.add_css_class, "dl") @@ -76,7 +76,7 @@ def update(self, gauge, part_two): value = self.total[0] / current_duration if current_duration > 1: - view.updateGauge(gauge, value) + view.update_gauge(gauge, value) view.progress.set_fraction(current_duration / DURATION * 0.5 + (0.5 if part_two else 0.0)) return not self.stop_event.is_set() diff --git a/src/ui/gauge.blp b/src/ui/gauge.blp index 28ecfae..0153311 100644 --- a/src/ui/gauge.blp +++ b/src/ui/gauge.blp @@ -25,7 +25,6 @@ template $Gauge : Box { orientation: vertical; valign: end; halign: center; - margin-bottom: 16; Label { valign: center; diff --git a/src/ui/views/test.blp b/src/ui/views/test.blp index 7d9a495..11b8caa 100644 --- a/src/ui/views/test.blp +++ b/src/ui/views/test.blp @@ -10,16 +10,56 @@ template $TestView : Box { styles [ "osd" ] } + [overlay] Box { orientation: vertical; + spacing: 16; + halign: center; + valign: end; + margin-bottom: 16; + + Box { + orientation: vertical; + + Label { + label: "Ping:"; + styles [ "title-2" ] + } + + Label { + label: bind template.ping; + styles [ "title-3" ] + } + } + + Box { + orientation: vertical; + + Label { + label: "Jitter:"; + styles [ "title-2" ] + } + + Label { + label: bind template.jitter; + styles [ "title-3" ] + } + } + } + + Box { + orientation: vertical; + Label title { label: bind template.server; margin-top: 8; styles [ "title-3" ] } - + Box { orientation: horizontal; + spacing: 64; + margin-bottom: 16; $Gauge download { label: "Download:"; @@ -28,23 +68,6 @@ template $TestView : Box { styles [ "dl" ] } - Box { - orientation: vertical; - valign: end; - width-request: 80; - margin-bottom: 16; - - Label { - label: "Ping:"; - styles [ "title-2" ] - } - - Label { - label: bind template.ping; - styles [ "title-3" ] - } - } - $Gauge upload { label: "Upload:"; vexpand: true; diff --git a/src/window.py b/src/window.py index 9a2b75c..5a6a3eb 100644 --- a/src/window.py +++ b/src/window.py @@ -39,14 +39,19 @@ class TestView(Gtk.Box): download = Gtk.Template.Child() upload = Gtk.Template.Child() ping = GObject.Property(type=str, default="...") + jitter = GObject.Property(type=str, default="...") server = GObject.Property(type=str) progress = Gtk.Template.Child() def __init__(self, **kwargs): super().__init__(**kwargs) + + def update_ping(self, ping, jitter): + self.ping = str(round(ping)) + "ms" + self.jitter = str(round(jitter)) + "ms" - def updateGauge(self, object, speed): + def update_gauge(self, object, speed): speedMb = round(speed / 125_000, 1) object.value = str(speedMb) + "Mbps" object.fill = min(speedMb / 100, 1.0) @@ -56,6 +61,7 @@ def reset(self): obj.value = "..." obj.fill = 0.0 self.ping = "..." + self.jitter = "..." self.progress.set_fraction(0.0) @Gtk.Template(resource_path="/xyz/ketok/Speedtest/ui/views/offline.ui")