Skip to content

Commit

Permalink
fix: handle server-timing headers with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytelife26 committed Oct 29, 2024
1 parent 9f03463 commit a08cda1
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions cfspeedtest/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import logging
import re
import statistics
import time
from enum import Enum
Expand All @@ -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."""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit a08cda1

Please sign in to comment.