Nk_ttfb_all
Description
This is a Bash function named nk_ttfb_all that measures the time-to-first-byte (TTFB) of multiple websites. It does this by calling another function called gen_ttfb_results, which first prints a table header, and then uses a loop to iterate over all the domains obtained via another function called nk_list_all_domains. For each domain, curl is used to retrieve the website, following any redirects, and limiting the query to 5 seconds. The -s option silences download progress and the -o option outputs the results to /dev/null since we don’t need them. Finally, the -w option is used to output the TTFB time and the total time taken to download the page.
The gen_ttfb_results function outputs the results in a format that is then passed through the column command to format the output as a table before being displayed to the user.
Example
No additional input is needed.
[root@cloudvpsserver ~]# nk_ttfb_all Site TTFB Total nkern.net 0.121499 0.121572
Code
nk_ttfb_all() { # This is basically just a wrapper that formats the rsults of gen_ttfb_results gen_ttfb_results() { # First print the headers of the table. echo "Site TTFB Total" # Now for every domain on the server. (obtained via nk_list_all_domains) for domain in $(nk_list_all_domains); do # Print the domain name, and it's curl ttfb result. # Use curl to get the ttfb of the site. # -L to follow links so that a 301 doesn't influence it # -m 5 limit the query to 5 seconds. # -s Be silent about the download info. # -o output the results to /dev/null since we don't need them. # -w write out, time_total and time_starttransfer echo "$domain" "$(curl -L -m 5 -s -o /dev/null https://"$domain" -w "%{time_starttransfer} %{time_total} \n" )" done } # run gen_ttfb_results and format the output. gen_ttfb_results | column -t }