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.
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:
This command sends a GET request to the specified URL. Now, to measure and display timing details, modify it as follows:
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 namedcurl-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:
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:
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:
| Metric | Description |
| time_namelookup | Time to resolve the domain name. |
| time_connect | Time to establish the initial connection. |
| time_appconnect | Time taken for app-level connection (SSL handshake, etc.). |
| time_pretransfer | Time just before data transfer begins. |
| time_redirect | Time spent in redirects. |
| time_starttransfer | Time until the first byte is received. |
| time_total | Total 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.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.