Skip to content

Improve latency calculation based on server timings and connect times - #127

Merged
carlosefr merged 3 commits into
mainfrom
server_time_estimates
Jul 22, 2026
Merged

Improve latency calculation based on server timings and connect times#127
carlosefr merged 3 commits into
mainfrom
server_time_estimates

Conversation

@carlosefr

Copy link
Copy Markdown
Collaborator

Improve accuracy of latency calculations by making use of additional "thinking time" sent by the server, and by using the observed TCP RTT as fine tuning.

@ask-bonk ask-bonk Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: Improve latency calculation based on server timings and connect times

Summary

This PR improves latency accuracy in the BandwidthEngine by:

  1. Richer Server-Timing parsingcfGetServerTime now matches cfReqDur/cfRequestDuration (case-insensitive, with variants) and falls back to summing cfSpeed* sub-metrics, replacing the previous single generic dur= match.
  2. TCP RTT fine-tuning — a new serverTimeDelta is calibrated from fresh TCP connections (HTTP < 3) by comparing the derived server time (ttfb - connectTime) against the server-reported time, blended with an EMA (weight 0.75). This delta is threaded through MeasurementEngine, ParallelLatency, and persisted across sub-engines so calibration carries forward.
  3. Config/doc cleanupsestimatedServerTime default changed 10 → 0; logAimApiUrl default repointed from aim.cloudflare.com/__log to speed.cloudflare.com/__results; matching README/AGENTS.md doc updates.
  4. Idle labeling — idle (non-loaded) latency requests now tagged with during=idle query param.
  5. Tests — new unit test covering the three cfGetServerTime branches; cfGetServerTime is now exported.

Overall the approach is reasonable and unit tests pass (101/101). The main issues are the unconditional debug console.log statements, which should not ship in a library, plus a few correctness edge cases worth confirming. Details inline.

Comment thread src/engines/BandwidthEngine/BandwidthEngine.ts
Comment thread src/engines/BandwidthEngine/BandwidthEngine.ts Outdated
Comment thread src/engines/BandwidthEngine/BandwidthEngine.ts
Comment thread src/engines/BandwidthEngine/BandwidthEngine.ts
Comment thread src/config/defaultConfig.ts
Comment thread src/config/defaultConfig.ts
Comment thread src/index.ts
@ask-bonk

ask-bonk Bot commented Jul 21, 2026

Copy link
Copy Markdown

Review posted successfully to PR #127 with 7 inline comments. Here's a summary of what I reviewed and flagged:

What the PR changes

Improves latency accuracy in BandwidthEngine via three main mechanisms:

  1. Richer Server-Timing parsingcfGetServerTime now prefers cfReqDur/cfRequestDuration (case-insensitive variants) and falls back to summing cfSpeed* sub-metrics.
  2. TCP RTT fine-tuning — a new serverTimeDelta, calibrated from fresh TCP connections (HTTP < 3) using an EMA (weight 0.75), threaded through MeasurementEngine and ParallelLatency and carried forward across sub-engines.
  3. Config/doc cleanupsestimatedServerTime default 10 → 0, logAimApiUrl repointed to speed.cloudflare.com/__results, plus README/AGENTS.md updates and during=idle labeling.

Also adds a unit test for cfGetServerTime (all 101 unit tests pass locally).

Issues flagged inline

  • High: Unconditional console.log debug statements (lines 469–504) ship to all library consumers on every request — inconsistent with the codebase's console.warn-only convention.
  • Correctness: ping floor lowered from 1e-2 to 0, allowing 0ms latency data points that can skew percentile/jitter stats.
  • Correctness/confirm: serverTimeDelta only propagates back via onFinished/onConnectionError — may be lost on other abort paths.
  • Confirm intent: default endpoint change (aim.cloudflare.com/__log → relative /__results) and estimatedServerTime default change (impacts HTTP/3 where calibration is disabled).
  • Minor: cfSpeed[a-zA-Z]* regex silently sums any future cfSpeed-prefixed metric; connectTime reused-connection semantics worth a clarifying comment.

The review was posted as COMMENT (not approve/request-changes), and no commits were pushed.

github run

@carlosefr
carlosefr force-pushed the server_time_estimates branch 3 times, most recently from 5f4bbb2 to 7f71259 Compare July 22, 2026 09:11
Improve accuracy of latency calculations by making use of additional "thinking time" sent by the server, and by using the observed TCP RTT as fine tuning.
@carlosefr
carlosefr force-pushed the server_time_estimates branch from 7f71259 to d19d74e Compare July 22, 2026 09:40
@carlosefr
carlosefr merged commit 82d38b0 into main Jul 22, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants