Precise Measurement of HTTP Request and Response Times Using cURL

Oct 31, 2025 · Programming · 14 views · 7.8

Keywords: cURL | HTTP performance | timing measurement | web services | performance analysis

Abstract: This article provides a comprehensive guide on using cURL's -w parameter and formatted output to accurately measure various stages of HTTP requests, including DNS resolution, TCP connection, SSL handshake, server processing time, and total duration. Through format files, aliases, and scripts, detailed performance metrics can be easily obtained for web service analysis and optimization.

Introduction

Accurately measuring the timing of various stages in HTTP requests is crucial for web service development and performance analysis. Traditional time commands only provide total duration without distinguishing between request and response phases. cURL, as a powerful command-line tool, offers extensive formatting options to obtain detailed timing information.

Detailed Explanation of cURL Timing Parameters

cURL supports custom output formatting through the -w parameter, which includes multiple time-related variables:

Basic Usage Method

First, create a format file curl-format.txt with the following content:

time_namelookup:  %{time_namelookup}s

time_connect:  %{time_connect}s

time_appconnect:  %{time_appconnect}s

time_pretransfer:  %{time_pretransfer}s

time_redirect:  %{time_redirect}s

time_starttransfer:  %{time_starttransfer}s

----------

time_total:  %{time_total}s

Then use the following command for measurement:

curl -w "@curl-format.txt" -o /dev/null -s -X POST -d @file server:port

Parameter explanation:

Output Result Analysis

After executing the command, you will get output similar to:

time_namelookup:  0.001s

time_connect:  0.037s

time_appconnect:  0.000s

time_pretransfer:  0.037s

time_redirect:  0.000s

time_starttransfer:  0.092s

----------

time_total:  0.164s

By analyzing these timing data, you can:

Advanced Configuration Methods

Linux/Mac Alias Setup

For convenience, create an alias:

alias curltime="curl -w \"@$HOME/.curl-format.txt\" -o /dev/null -s "

Then simply use: curltime wordpress.org

Standalone Script Implementation

Create a curltime script file:

#!/bin/bash

curl -w @- -o /dev/null -s "$@" <<'EOF'

time_namelookup:  %{time_namelookup}

time_connect:  %{time_connect}

time_appconnect:  %{time_appconnect}

time_pretransfer:  %{time_pretransfer}

time_redirect:  %{time_redirect}

time_starttransfer:  %{time_starttransfer}

----------

time_total:  %{time_total}

EOF

Set execution permissions: chmod +x curltime

Windows Batch File

Create a curltime.bat file:

curl -w "@%~dp0curl-format.txt" -o NUL -s %*

Practical Application Examples

For JSON web service performance testing:

curl -w "@curl-format.txt" -o /dev/null -s -X POST \
-H "Content-Type: application/json" \
-d '{"key": "value"}' \
http://api.example.com/endpoint

Combined with detailed log output:

curl -w "@curl-format.txt" -o /dev/null --trace-time -v \
-X POST -d @data.json https://api.service.com/process

Performance Optimization Recommendations

Based on timing measurement results, consider the following optimization measures:

Conclusion

cURL's formatted output functionality provides powerful tools for HTTP request performance analysis. Through proper configuration and usage, precise measurement of various request stages can be achieved, providing data support for web service performance optimization. It is recommended to integrate these measurement methods in actual projects and establish continuous performance monitoring mechanisms.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.