MyTetra Share
Делитесь знаниями!
How do I measure request and response times at once using cURL?
Время создания: 02.02.2018 11:47
Текстовые метки: curl test time
Раздел: cURL, wget

347

down vote

favorite

199


I have a web service that receives data in JSON format, processes the data, and then returns the result to the requester.


I want to measure the request, response, and total time using cURL.


My example request looks like:


curl -X POST -d @file server:port


and I currently measure this using the time command in Linux:


time curl -X POST -d @file server:port


The time command only measures total time, though - which isn't quite what I am looking for.


Is there any way to measure request and response times using cURL?

curl time upload download measure

shareimprove this question

edited Aug 13 '13 at 17:27

asked Aug 13 '13 at 17:21

sdasdadas

8,4631343113

add a comment

8 Answers

active

oldest

votes

up vote

895

down vote

accepted

+100


From this brilliant blog post... https://blog.josephscott.org/2011/10/14/timing-details-with-curl/


cURL supports formatted output for the details of the request (see the cURL manpage for details, under -w, –write-out <format>). For our purposes we’ll focus just on the timing details that are provided.


Create a new file, curl-format.txt, and paste in:


time_namelookup: %{time_namelookup}\n

time_connect: %{time_connect}\n

time_appconnect: %{time_appconnect}\n

time_pretransfer: %{time_pretransfer}\n

time_redirect: %{time_redirect}\n

time_starttransfer: %{time_starttransfer}\n

----------\n

time_total: %{time_total}\n


Make a request:


curl -w "@curl-format.txt" -o /dev/null -s "http://wordpress.com/"


Or on Windows, it's...


curl -w "@curl-format.txt" -o NUL -s "http://wordpress.com/"



What this does:


-w "@curl-format.txt" tells cURL to use our format file

-o /dev/null redirects the output of the request to /dev/null

-s tells cURL not to show a progress meter

"http://wordpress.com/" is the URL we are requesting. Use quotes particularly if your URL has "&" query string parameters



And here is what you get back:


time_namelookup: 0.001

time_connect: 0.037

time_appconnect: 0.000

time_pretransfer: 0.037

time_redirect: 0.000

time_starttransfer: 0.092

----------

time_total: 0.164



Make a Windows shortcut (aka BAT file)


Put this command in CURLTIME.BAT (in the same folder as curl.exe)


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


Then you can simply call...


curltime wordpress.org


shareimprove this answer

edited Aug 30 '17 at 6:33

matse

53

answered Mar 25 '14 at 3:55

Simon East

27.1k109189


show 3 more comments

up vote

88

down vote


Here is the answer:


curl -X POST -d @file server:port -w %{time_connect}:%{time_starttransfer}:%{time_total}


All of the variables used with -w can be found in man curl.

shareimprove this answer

edited Apr 8 '17 at 14:36

Corey Goldberg

32.4k20101115

answered Aug 13 '13 at 17:31

sdasdadas

8,4631343113


add a comment

up vote

36

down vote


A shortcut you can add to your .bashrc etc, based on other answers here:


function perf {

curl -o /dev/null -s -w "%{time_connect} + %{time_starttransfer} = %{time_total}\n" "$1"

}


Usage:


> perf stackoverflow.com

0.521 + 0.686 = 1.290


shareimprove this answer

edited May 13 '16 at 0:23

answered Apr 15 '15 at 15:58

mahemoff

19.5k1788147


add a comment

up vote

34

down vote


To measure response time with curl use following command:


curl -o /dev/null -s -w %{time_total} http://www.google.com


To get different types of times use following command:


curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} http://www.google.com


Source: Get response time with curl

shareimprove this answer

answered Feb 24 '15 at 10:06

ThoQ

7,40413733

add a comment

up vote

10

down vote


If you want to analyze or summarize the latency you can try apache bench:


ab -n [number of samples] [url]


For example:


ab -n 100 http://www.google.com/


It will show:


This is ApacheBench, Version 2.3 <$Revision: 1757674 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/


Benchmarking www.google.com (be patient).....done



Server Software: gws

Server Hostname: www.google.com

Server Port: 80


Document Path: /

Document Length: 12419 bytes


Concurrency Level: 1

Time taken for tests: 10.700 seconds

Complete requests: 100

Failed requests: 97

(Connect: 0, Receive: 0, Length: 97, Exceptions: 0)

Total transferred: 1331107 bytes

HTML transferred: 1268293 bytes

Requests per second: 9.35 [#/sec] (mean)

Time per request: 107.004 [ms] (mean)

Time per request: 107.004 [ms] (mean, across all concurrent requests)

Transfer rate: 121.48 [Kbytes/sec] received


Connection Times (ms)

min mean[+/-sd] median max

Connect: 20 22 0.8 22 26

Processing: 59 85 108.7 68 911

Waiting: 59 85 108.7 67 910

Total: 80 107 108.8 90 932


Percentage of the requests served within a certain time (ms)

50% 90

66% 91

75% 93

80% 95

90% 105

95% 111

98% 773

99% 932

100% 932 (longest request)


shareimprove this answer

answered Jun 30 '17 at 16:46

Andong

2,03421017

add a comment

up vote

4

down vote


I made a friendly formatter for sniffing curl requests to help with debugging ( see comments for usage ). It contains's every known output parameter you can write out in an easy to read format.


https://gist.github.com/manifestinteractive/ce8dec10dcb4725b8513

shareimprove this answer

answered Aug 17 '15 at 4:08

Manifest Interactive

66455

add a comment

up vote

4

down vote


The following is inspired by Simon's answer. It's self-contained (doesn't require a separate format file), which makes it great for inclusion into .bashrc.


curl_time() {

curl -so /dev/null -w "\

namelookup: %{time_namelookup}s\n\

connect: %{time_connect}s\n\

appconnect: %{time_appconnect}s\n\

pretransfer: %{time_pretransfer}s\n\

redirect: %{time_redirect}s\n\

starttransfer: %{time_starttransfer}s\n\

-------------------------\n\

total: %{time_total}s\n" "$@"

}


Futhermore, it should work with all arguments that curl normally takes, since the "$@" just passes them through. For example, you can do:


curl_time -X POST -H "Content-Type: application/json" -d '{"key": "val"}' https://postman-echo.com/post


Output:


namelookup: 0,125000s

connect: 0,250000s

appconnect: 0,609000s

pretransfer: 0,609000s

redirect: 0,000000s

starttransfer: 0,719000s

-------------------------

total: 0,719000s


shareimprove this answer

edited Dec 22 '17 at 16:31

answered Dec 22 '17 at 16:21

Konstantin

71411119

add a comment

up vote

0

down vote


Hey is better than Apache Bench, has fewer issues with SSL


./hey https://google.com -more

Summary:

Total: 3.0960 secs

Slowest: 1.6052 secs

Fastest: 0.4063 secs

Average: 0.6773 secs

Requests/sec: 64.5992


Response time histogram:

0.406 [1] |

0.526 [142] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎

0.646 [1] |

0.766 [6] |∎∎

0.886 [0] |

1.006 [0] |

1.126 [0] |

1.246 [12] |∎∎∎

1.365 [32] |∎∎∎∎∎∎∎∎∎

1.485 [5] |∎

1.605 [1] |


Latency distribution:

10% in 0.4265 secs

25% in 0.4505 secs

50% in 0.4838 secs

75% in 1.2181 secs

90% in 1.2869 secs

95% in 1.3384 secs

99% in 1.4085 secs


Details (average, fastest, slowest):

DNS+dialup: 0.1150 secs, 0.0000 secs, 0.4849 secs

DNS-lookup: 0.0032 secs, 0.0000 secs, 0.0319 secs

req write: 0.0001 secs, 0.0000 secs, 0.0007 secs

resp wait: 0.2068 secs, 0.1690 secs, 0.4906 secs

resp read: 0.0117 secs, 0.0011 secs, 0.2375 secs


Status code distribution:

[200] 200 responses


shareimprove this answer

answered Sep 8 '17 at 14:22

Jonathan

1,3681427


 
MyTetra Share v.0.53
Яндекс индекс цитирования