MATTREAGAN
iOS & macOS engineer,
designer, game creator
Author: Matt Reagan
« Previous | Next »

Web Service Speed Tests with curl
Recently I wanted to test the time to first byte of a REST web service. As it turns out, curl works really well for this using the -w flag.

Syntax

curl -w <format> <URL> -s -o /dev/null

Example

for i in `seq 1 5`; \
do curl \
-w "Connect time: %{time_connect} Time to first byte: %{time_starttransfer} Total: %{time_total} \n" \
"http://example.com/1/endpoint" \
-s -o /dev/null; \
done

Explanation

for i in `seq 1 5`; do <command>; done
Repeats the curl command several times to better gauge the average response time.

curl -w "Connect time: %{time_connect} Time to first byte: %{time_starttransfer} Total: %{time_total} \n"
This is where the magic of the --write-out / -w flag is included. The format string specifies what data to print, and the variables within the braces are curl-provided tokens which are substituted with the appropriate value. For a full list of options check out the curl manpage.

<URL>
The URL to test.

-H "Authorization: Bearer <OAuth token>"
It's not shown in the example above, but if the URL requires OAuth authentication, the -H flag here allows the token to be included for the Authorization header field.

-s
Silences the standard curl progress meter.

-o /dev/null
Since we don't care about the actual payload for this test, it is written to /dev/null.

Result

Connect time: 0.026 Time to first byte: 0.271 Total time: 0.271 
Connect time: 0.030 Time to first byte: 0.287 Total time: 0.287 
Connect time: 0.031 Time to first byte: 0.296 Total time: 0.296 
Connect time: 0.031 Time to first byte: 0.281 Total time: 0.281 
Connect time: 0.031 Time to first byte: 0.287 Total time: 0.287

The output is clean, easy-to-read, and only the specific information we were looking for is printed. curl makes it easy to test web service speeds, and the built-in timing variables simplify the process of gauging of where unexpected delays might be occurring. Thanks to: Martin Spier for his post on this.