Skip to content

Measuring website metrics with curl

I have written a short bash function for measuring website metrics such as DNS lookup, redirects, redirect time, the first byte (TTFB), connect time and the total time.

Short version (Only TTFB)

function ttfb() {
    if [ $# -eq 0 ]
        then
                echo "Usage: ttfb url"
        else
                curl -o /dev/null \
                        -H 'Cache-Control: no-cache' \
                        -s \
                        -w "Connect: %{time_connect} TTFB: %{time_starttransfer} Total time: %{time_total} \n" \
                        $1
        fi
    }

Usage example:

aelius@macbook:~$ ttfb 
Usage: ttfb url
aelius@macbook:~$ ttfb https://www.unixteacher.org/
Connect: 0.046315 TTFB: 0.157112 Total time: 0.157400 
aelius@macbook:~$ 

Long version (DNS Lookup, Redirects, Redirect time, First byte, Connect time, Total time)

function metrics {
        if [ $# -eq 0 ]
                then
                        echo "Usage: metrics url"
                else
                        curl -H 'Cache-Control: no-cache' -Lw "DNS Lookup: %{time_namelookup} seconds \nRedirects: %{time_redirect} seconds with %{num_redirects} redirects \nFirst Byte: %{time_starttransfer} seconds \nConnect Time: %{time_connect} seconds \nTotal Time: %{time_total} seconds\n" -so /dev/null $1
                fi
        }

Usage example:

aelius@macbook:~$ metrics 
Usage: metrics url
aelius@macbook:~$ metrics https://www.unixteacher.org/
DNS Lookup: 0.009266 seconds 
Redirects: 0.000000 seconds with 0 redirects 
First Byte: 0.173887 seconds 
Connect Time: 0.051254 seconds 
Total Time: 0.174168 seconds
aelius@macbook:~$

References:
https://en.wikipedia.org/wiki/Time_to_first_byte
https://curl.haxx.se/docs/manual.html

Published inFreeBSDLinuxMacOS XMisc
© 2019 - Marian Amza. All rights reserved.
Powered by Wordpress & Debian