Home > front end >  convert some of the response values in curl command
convert some of the response values in curl command

Time:09-10

I use the below script to get the average response time for a specific website. It works just fine. I just need to convert some of the values in the response like the time_total, i want to view it in milliseconds and the size_download in KB format. And in the end of the command where I share the average response time, i also want to print it in milliseconds. Any help is really appreciated.

for ((i=1;i<=50;i  )); do curl -w 'Return Code: %{http_code}; Bytes Received: %{size_download}; Response Time: %{time_total}\n' "https://www.google.com" -m 2 -o /dev/null -s; done |tee /dev/tty|awk '{ sum  = $NF; n   } END { If (n > 0); print "Average Response Time =",sum /n;}'

CodePudding user response:

You can just pipe the curl output through awk and format it as you want like this:

curl -w 'Return Code: %{http_code}; Bytes Received: %{size_download}; Response Time: %{time_total}\n' "https://www.google.com" -m 2 -o /dev/null -s | awk  '{printf "Return Code: %d; KiB Received: %f; Response Time(ms): %f\n", $3, $6/1024, $9*1000}'

So the oneliner is the following:

for ((i=1;i<=50;i  )); do curl -w 'Return Code: %{http_code}; Bytes Received: %{size_download}; Response Time: %{time_total}\n' "https://www.google.com" -m 2 -o /dev/null -s | awk  '{printf "Return Code: %d; KiB Received: %f; Response Time(ms): %f\n", $3, $6/1024, $9*1000}'; done | tee /dev/tty |awk '{ sum  = $NF; n   } END { If (n > 0); print "Average Response Time =",sum /n;}'

You can also format the numbers as you want by puttingg, for example %.2f for 2 decimal precision or %d for integer...

  • Related