From 89aa415954492875959e0c9ff090fa7ab461a92f Mon Sep 17 00:00:00 2001 From: Nytelife26 Date: Tue, 29 Oct 2024 18:52:33 +0000 Subject: [PATCH] fix: handle server-timing headers with regex --- cfspeedtest/cloudflare.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cfspeedtest/cloudflare.py b/cfspeedtest/cloudflare.py index cc88211..0b54e5e 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.""" @@ -191,9 +194,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("=")[1].split(",")[0]) / 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 )