Last active
April 10, 2020 06:03
-
-
Save MarkZhangTW/4b99dfb2fbbbb1482f7eb7cce41cf2aa to your computer and use it in GitHub Desktop.
Curl format for displaying time infomation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Reference: https://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl | |
curl_format="\ | |
%{time_namelookup}:\ | |
%{time_connect}:%{time_appconnect}:\ | |
%{time_pretransfer}:%{time_redirect}:\ | |
%{time_starttransfer}:%{time_total}" | |
curl_result=$(echo "$curl_format" | curl -s -o /dev/null -w @- $@) | |
IFS=':' read -r -a array <<< "$curl_result" | |
namelookup="${array[0]}" | |
connect="${array[1]}" | |
appconnect="${array[2]}" | |
pretransfer="${array[3]}" | |
redirect="${array[4]}" | |
starttransfer="${array[5]}" | |
total="${array[6]}" | |
calc_delta () { awk -v a="$1" -v b="$2" 'BEGIN {printf "%.6f", b-a}'; } | |
delta_connect=$(calc_delta $namelookup $connect) | |
if [ "$appconnect" = "0.000000" ]; then | |
delta_appconnect=$appconnect | |
delta_pretransfer=$(calc_delta $connect $pretransfer) | |
else | |
delta_appconnect=$(calc_delta $connect $appconnect) | |
delta_pretransfer=$(calc_delta $appconnect $pretransfer) | |
fi | |
if [ "$redirect" = "0.000000" ]; then | |
delta_redirect=$redirect | |
delta_starttransfer=$(calc_delta $pretransfer $starttransfer) | |
else | |
delta_redirect=$(calc_delta $pretransfer $redirect) | |
delta_starttransfer=$(calc_delta $redirect $starttransfer) | |
fi | |
delta_total=$(calc_delta $starttransfer $total) | |
echo_format="\ | |
DNS Lookup: ${namelookup}s | |
TCP Connect: ${connect}s - $delta_connect | |
TLS Connect: ${appconnect}s - $delta_appconnect | |
Pre-transfer: ${pretransfer}s - $delta_pretransfer | |
Redirect: ${redirect}s - $delta_redirect | |
TTFB: ${starttransfer}s - $delta_starttransfer | |
--------- | |
Total: ${total}s - $delta_total" | |
echo "$echo_format" |
Author
MarkZhangTW
commented
Apr 10, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment