Skip to content

Instantly share code, notes, and snippets.

@astleychen
Created August 15, 2019 13:39
Show Gist options
  • Save astleychen/64bad21bfc5000b78a5f14e4149d82a1 to your computer and use it in GitHub Desktop.
Save astleychen/64bad21bfc5000b78a5f14e4149d82a1 to your computer and use it in GitHub Desktop.
Timing with cURL

Concept about measuring network latency

Methods for latency test

  • PING - ICMP ping does not give a good indication of end-user latency.
  • Time To First Byte (TTFB) - A good way to measure time to first HTTP response is to issue a curl command repeatedly to the server to get a response from the web server.

The Theory for Ideal

When comparing results, be aware that latency on fiber links is constrained mainly by the distance and the speed of light in fiber, which is roughly 200,000 km/s (or 124,724 miles/s).

(distance) km * 2 / 200,000 km/s * 1000 ms/s = (theory latency) milliseconds (ms)

An observed latency of approximately 1.5 times the ideal.

Commandlines for test

# PING
ping -c 5 <hostname>

Using cURL

# Setup .curlrc to format output
echo '-w "dnslookup: %{time_namelookup} | tcp_connect: %{time_connect} | ssl_connect: %{time_appconnect} | TTFB: %{time_starttransfer} | total: %{time_total} | size: %{size_download}\n"' > ~/.curlrc

Example: dnslookup: 0.000628 | tcp_connect: 0.004423 | ssl_connect: 0.020630 | TTFB: 0.020725 | total: 0.221988 | size: 24541

# cURL with re-used connetion
for ((i=0;i<50;i++)); \
  do curl -so /dev/null \
  https://<hostname>:<port>/<uri>; \
done

Sample measurements

# Timing within GCP asia-east1
dnslookup: 0.004333 | tcp_connect: 0.009022 | ssl_connect: 0.022893 | TTFB: 0.023090 | total: 0.180719 | size: 24089
# Timing from TWN public to GCP asia-east1
dnslookup: 0.005239 | tcp_connect: 0.019696 | ssl_connect: 0.056586 | TTFB: 0.056742 | total: 0.254575 | size: 24097
# Timing from AWS ap-northeast-1 to GCP asia-east1
dnslookup: 0.000624 | tcp_connect: 0.040884 | ssl_connect: 0.125147 | TTFB: 0.125250 | total: 0.353387 | size: 24622

Ref

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment