Monday, August 21, 2017

Calculating the Request and Response Time With the Curl Command

In some occassions where we are doing performance related analysis, we may need to do a direct call to a particular service and get the time spent between request and response. The below steps will give an option to use curl to achieve the requirement.

1) Create a file called performance-format.txt and add the below content.
 url_effective: %{url_effective}\n  
 http_code: %{http_code}\n  
 content_type: %{content_type}\n  
 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  

Below are the details of the used parameters, you can refer more on [1] and [2].
[1] https://curl.haxx.se/docs/manpage.html
[2] http://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.html
  • url_effective The URL that was fetched last.
  • http_code The numerical code that was found in the last retrieved HTTP(S) page.  
  • time_connect The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed
  • time_appconnect The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed.
  • time_pretransfer The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
  • time_redirect The time, in seconds, it took for all redirection steps including name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections.
  • time_starttransfer The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
  • time_total The total time, in seconds, that the full operation lasted.

2) Now run the below sample command:

 curl -v http://www.google.com -w "@performance-format.txt" -o test1.txt  
Here, -v will provide more information on the logs shown in console and -w used provide the format of the output.

3) We will get the details as below and also the response will be saved in test1.txt.

 sh-4.3$ curl -v http://www.google.com -w "@performance-format.txt" -o test1.txt                            
 * Rebuilt URL to: http://www.google.com/                                               
  % Total  % Received % Xferd Average Speed  Time  Time   Time Current                            
                  Dload Upload  Total  Spent  Left Speed                             
  0   0  0   0  0   0   0   0 --:--:-- --:--:-- --:--:--   0*  Trying 216.58.197.68...               
 * Connected to www.google.com (216.58.197.68) port 80 (#0)   
 > GET / HTTP/1.1                                                           
 > Host: www.google.com                                                        
 > User-Agent: curl/7.43.0                                                       
 > Accept: */*                                                             
 >                                                                   
 < HTTP/1.1 302 Found                                                         
 < Cache-Control: private                                                       
 < Content-Type: text/html; charset=UTF-8                                               
 < Referrer-Policy: no-referrer                                                    
 < Location: http://www.google.co.in/?gfe_rd=cr&ei=LH-aWayrBMKL8Qez7J6ADw                               
 < Content-Length: 261                                                         
 < Date: Mon, 21 Aug 2017 06:35:24 GMT                                                 
 <                                                                   
 { [261 bytes data]                                                          
 100  261 100  261  0   0  4812   0 --:--:-- --:--:-- --:--:-- 4745                            
 * Connection #0 to host www.google.com left intact                                          
 url_effective: http://www.google.com/                                                 
 http_code: 302                                                            
 content_type: text/html; charset=UTF-8                                                
 time_namelookup: 0.030                                                        
 time_connect: 0.041                                                         
 time_appconnect: 0.000                                                        
 time_pretransfer: 0.042                                                       
 time_redirect: 0.000                                                         
 time_starttransfer: 0.054                                                      
           ----------                                                    
 time_total: 0.054                                                          
 sh-4.3$   


That's it...


3 comments: