Skip to content

feat(timing): add --timing request phase breakdown (#14)#21

Merged
kavix merged 1 commit into
kavix:mainfrom
AzarAI-TOP:feat/timing-breakdown
Jun 16, 2026
Merged

feat(timing): add --timing request phase breakdown (#14)#21
kavix merged 1 commit into
kavix:mainfrom
AzarAI-TOP:feat/timing-breakdown

Conversation

@AzarAI-TOP

Copy link
Copy Markdown
Contributor

Closes #14.

Adds a --timing flag that prints a per-phase breakdown of where a request spent its time, matching the issue's proposed output:

── TIMING ──────────────────────────────────────
  DNS Lookup         1ms
  TCP Connection     1ms
  TLS Handshake      0s
  Server Processing  704ms
  Content Transfer   0s
  Total              706ms

How it works

  • Connect / TLS / request-written / first-response-byte are collected with net/http/httptrace.
  • DNS is timed separately around the client's own concurrent resolver. kurl resolves hostnames in a custom DialContext (Cloudflare 1.1.1.1 raced against the system resolver), which httptrace's DNS hooks can't observe, so the --timing path records the DNS phase right there.
  • The client returns once response headers are available — before the body is read — so it measures up to the first response byte and exposes TimeToFirstByte. main owns the wall-clock window around the body read and derives the content-transfer phase as Total - TimeToFirstByte.
  • Phase durations use "latest wins" timestamps, so across a redirect chain a reused keep-alive connection correctly shows 0s for the phases it skipped rather than stale values.
  • --timing is persisted by kurl save / kurl run.

Acceptance criteria

  • Add --timing flag
  • Measure and display DNS, TCP, TLS, TTFB (server processing), transfer phases
  • Use Go's httptrace package for accurate measurements (plus explicit DNS timing for kurl's custom resolver)
  • Format output cleanly aligned
  • Add test coverage

Tests

  • client: phase() helper edge cases; Fetch populates timing against a local httptest server (TCP/TTFB positive, TLS zero for http) and leaves Timing nil when not requested.
  • main: --timing flag parsing (set and default).
  • printer: rendered output format includes every phase label and value plus Total.

go test ./..., go vet ./..., and gofmt are all clean. Verified manually against both HTTPS (Cloudflare) and plain-HTTP endpoints.

🤖 Generated with Claude Code

Add a --timing flag that prints how long each phase of a request took:
DNS lookup, TCP connection, TLS handshake, server processing (TTFB),
content transfer, and total.

The phases the Go transport can observe (connect, TLS, request-written,
first-response-byte) are collected with net/http/httptrace. DNS is timed
separately around the client's own concurrent resolver, since that
resolution happens in a custom DialContext that httptrace cannot see.

The client returns before the body is read, so it measures up to the
first response byte and exposes TimeToFirstByte; main owns the overall
wall clock and derives the content-transfer phase
(Total - TimeToFirstByte). --timing is also persisted by save/run.

Tests cover the phase() helper, that Fetch populates timing against a
local server (and leaves it nil when not requested), the --timing flag
parsing, and the rendered output format.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@kavix kavix merged commit a794178 into kavix:main Jun 16, 2026
1 check passed
@kavix kavix self-requested a review June 16, 2026 07:38
@AzarAI-TOP AzarAI-TOP deleted the feat/timing-breakdown branch June 17, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add request/response timing breakdown

2 participants