Cui Mingda

Cui Mingda

Oct 1, 2020

Measuring Website Loading Speed With Curl

Easy ways

You only need to install curl-speed, that’s all.

git clone https://github.com/cuimingda/curl-speed.git
ln -s "$(pwd)/curl-speed/curl-speed.sh" /usr/local/bin/curl-speed

Then you can test speed of any URL or domain in one command, and here is an example while executing.

~ curl-speed github.com
https://github.com/	@13.250.177.223	3.014883

appconnect:    0.587596
connect:       0.153894
namelookup:    0.004798
pretransfer:   0.660505
redirect:      0.140610
starttransfer: 0.802653

Hard Ways

curl-speed is just a wrapper of curl, the key is following command:

curl -sSL -w "$message" --user-agent "$userAgent" --connect-timeout 10 -o /dev/null "$1"

There are variables can be used in the message, here is a list related to time things.

Variable Description
time_appconnect The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0)
time_connect The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0)
time_namelookup The time, in seconds, it took from the start until the name resolving was completed.
time_pretransfer The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
time_redirect The time, in seconds, it took for all redirection steps including name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3)
time_starttransfer The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
time_total The total time, in seconds, that the full operation lasted.

We can show these variables in the message, format is %{variableName}, for example:

curl -sSL -w "%{time_total}" -o /dev/null github.com

Considering -L option, the final url is not the one requested. The website may have several IPs, but you tried one of them, may be fast, or not. So, we need know the final URL and remote IP:

Variable Description
url_effective The URL that was fetched last. This is most meaningful if you’ve told curl to follow location: headers.
remote_ip The remote IP address of the most recently done connection - can be either IPv4 or IPv6 (Added in 7.29.0)

Conclution

cURL is a strong tool and has many options, we can solve our real problems by this tool. When the website could not be open, or just very slow, maybe we can use curl-speed to test website, found issues and final fix them.