diff --git a/cfspeedtest/cloudflare.py b/cfspeedtest/cloudflare.py index 682f796..7deaa80 100644 --- a/cfspeedtest/cloudflare.py +++ b/cfspeedtest/cloudflare.py @@ -7,6 +7,7 @@ from __future__ import annotations import logging +import re import statistics import time from enum import Enum @@ -16,6 +17,8 @@ log = logging.getLogger("cfspeedtest") +TIMING_DURATION_RE = re.compile(r"dur=([0-9.]+)") + class TestType(Enum): """The type of an individual test.""" @@ -97,12 +100,10 @@ def jitter_from(latencies: list[float]) -> float | None: """Compute jitter as average deviation between consecutive latencies.""" if len(latencies) < 2: return None - return statistics.mean( - [ - abs(latencies[i] - latencies[i - 1]) - for i in range(1, len(latencies)) - ] - ) + return statistics.mean([ + abs(latencies[i] - latencies[i - 1]) + for i in range(1, len(latencies)) + ]) class TestMetadata(NamedTuple): @@ -191,10 +192,8 @@ def run_test(self, test: TestSpec) -> TestTimers: test.type.value, url, data=data, timeout=self.timeout ) coll.full.append(time.time() - start) - coll.server.append( - float(r.headers["Server-Timing"].split(",")[0].split("=")[1]) - / 1e3 - ) + timing_match = TIMING_DURATION_RE.search(r.headers["Server-Timing"]) + coll.server.append(float(timing_match.group(1)) / 1e3) coll.request.append( r.elapsed.seconds + r.elapsed.microseconds / 1e6 )