Skip to content

Instantly share code, notes, and snippets.

@theprogrammerin
Last active March 22, 2022 09:37
Show Gist options
  • Save theprogrammerin/d00ec2c92f8c4f80df37c20129510055 to your computer and use it in GitHub Desktop.
Save theprogrammerin/d00ec2c92f8c4f80df37c20129510055 to your computer and use it in GitHub Desktop.
Curl timing test for a URL

Bash Curl timing test

Usage bash test.sh "<url>" <no_samples>

Params

  • url - sample URL to test.
  • no_samples - no of samples to run. Default: 10

Example

$ bash test.sh "https://example.com" 5
0.015165 0.214462 0.630342 0.630874 0.000000 0.831220 0.831465
0.010218 0.209856 0.627238 0.628216 0.000000 0.827366 0.827506
0.008061 0.204611 0.612787 0.612946 0.000000 0.810696 0.811175
0.010656 0.209876 0.622439 0.622684 0.000000 0.823202 0.823371
0.008611 0.206133 0.626103 0.626606 0.000000 0.826558 0.826718

-----------------------------
url:     https://example.com
samples: 5
-----------------------------
time_namelookup:    0.0105422
time_connect:       0.208988
time_appconnect:    0.623782
time_pretransfer:   0.624266
time_redirect:      0
time_starttransfer: 0.82381
time_total:         0.824048
-----------------------------
#!/bin/bash
time_namelookup=0.0
time_connect=0.0
time_appconnect=0.0
time_pretransfer=0.0
time_redirect=0.0
time_starttransfer=0.0
time_total=0.0
count=0
if [ -n "$2" ]; then
samples=$2
else
samples=10
fi
for i in $(seq $samples); do
result=`curl -w "%{time_namelookup} %{time_connect} %{time_appconnect} %{time_pretransfer} %{time_redirect} %{time_starttransfer} %{time_total}" -o /dev/null -s "$1"`
echo $result
read -a timing_data <<< $result
time_namelookup=`echo "$time_namelookup ${timing_data[0]}" | awk '{print $1 + $2}'`
time_connect=`echo "$time_connect ${timing_data[1]}" | awk '{print $1 + $2}'`
time_appconnect=`echo "$time_appconnect ${timing_data[2]}" | awk '{print $1 + $2}'`
time_pretransfer=`echo "$time_pretransfer ${timing_data[3]}" | awk '{print $1 + $2}'`
time_redirect=`echo "$time_redirect ${timing_data[4]}" | awk '{print $1 + $2}'`
time_starttransfer=`echo "$time_starttransfer ${timing_data[5]}" | awk '{print $1 + $2}'`
time_total=`echo "$time_total ${timing_data[6]}" | awk '{print $1 + $2}'`
count=`echo "$count 1" | awk '{print $1 + $2}'`
done
time_namelookup=`echo "$time_namelookup $count" | awk '{print $1 / $2}'`
time_connect=`echo "$time_connect $count" | awk '{print $1 / $2}'`
time_appconnect=`echo "$time_appconnect $count" | awk '{print $1 / $2}'`
time_pretransfer=`echo "$time_pretransfer $count" | awk '{print $1 / $2}'`
time_redirect=`echo "$time_redirect $count" | awk '{print $1 / $2}'`
time_starttransfer=`echo "$time_starttransfer $count" | awk '{print $1 / $2}'`
time_total=`echo "$time_total $count" | awk '{print $1 / $2}'`
echo ""
echo "-----------------------------"
echo "url: $1"
echo "samples: $count"
echo "-----------------------------"
echo "time_namelookup: $time_namelookup"
echo "time_connect: $time_connect"
echo "time_appconnect: $time_appconnect"
echo "time_pretransfer: $time_pretransfer"
echo "time_redirect: $time_redirect"
echo "time_starttransfer: $time_starttransfer"
echo "time_total: $time_total"
echo "-----------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment