cURL
Request-Response Times
Programming
Web Development
Network Tools

How do I measure request and response times at once using cURL?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

When developing or debugging web applications, it's often crucial to understand the performance of HTTP requests and responses. One of the simplest yet powerful tools available for measuring the timings of these interactions is cURL, a command-line utility used for transferring data using various network protocols.

Understanding cURL

cURL stands for "Client URL" and it supports a wide range of protocols including HTTP, HTTPS, FTP, and more. It's a favorite among developers for its versatility and ease of use in sending requests and receiving responses from a server without user interaction.

Using cURL to Measure Timings

The primary way to measure request and response times with cURL is through its -w, or --write-out option. This option allows you to specify a format string that controls what data cURL outputs after a transfer completes. It does not download the data itself but displays statistics about the transfer.

Basic cURL Command

Here’s a simple cURL command:

bash
curl https://example.com

This command sends a GET request to the specified URL. Now, to measure and display timing details, modify it as follows:

bash
curl -o /dev/null -s -w "@curl-format.txt" https://example.com

Here's what each option means:

  • -o /dev/null: Redirects the output (body of the response) to nowhere, essentially discarding it.
  • -s: Silent mode. It doesn’t show any progress or error messages which makes the output cleaner.
  • -w "@curl-format.txt": Tells cURL to print additional details using the format specified in a file named curl-format.txt.

Creating a Format File

To display timings, you need to specify them in a format file. Create a file named curl-format.txt and include the following parameters:

 
1    time_namelookup:  %{time_namelookup}\n
2    time_connect:      %{time_connect}\n
3    time_appconnect:   %{time_appconnect}\n
4    time_pretransfer:  %{time_pretransfer}\n
5    time_redirect:     %{time_redirect}\n
6    time_starttransfer:%{time_starttransfer}\n
7    ----------\n
8    time_total:        %{time_total}\n

Each variable measures specific timing data:

  • time_namelookup: Time until name resolving was complete.
  • time_connect: Time until the TCP connection to the server was established.
  • time_appconnect: Time until connection to the server was completed (TLS/SSL handshake for HTTPS).
  • time_pretransfer: Time until the file transfer was about to begin.
  • time_redirect: Time spent in redirections.
  • time_starttransfer: Time until the first byte was received.
  • time_total: Total time that the full operation lasted.

This setup ensures that when you run the cURL command, you get an output like:

 
1    time_namelookup:  0.005
2    time_connect:      0.015
3    time_appconnect:   0.107
4    time_pretransfer:  0.107
5    time_redirect:     0.000
6    time_starttransfer:0.214
7    ----------
8    time_total:        0.215

Analyzing Output

The output details how long each phase of the request/response cycle took. This is immensely useful for identifying bottlenecks or performance issues. For example, a long time_connect might indicate network slowness, while a long time_starttransfer can point to server processing delays.

Summary Table

Here's a table summarizing what each timing metric represents:

MetricDescription
time_namelookupTime to resolve the domain name.
time_connectTime to establish the initial connection.
time_appconnectTime taken for app-level connection (SSL handshake, etc.).
time_pretransferTime just before data transfer begins.
time_redirectTime spent in redirects.
time_starttransferTime until the first byte is received.
time_totalTotal time of the request until completion.

Conclusion

cURL is a highly effective tool for measuring HTTP request and response times, providing granular detail into every aspect of the network interaction. By understanding and analyzing these timings, developers can optimize applications and deliver smoother, faster user experiences.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions