Get various response times for a site (DNS resolution time, response time, transmission time) _linux shell

Source: Internet
Author: User
Tags curl local time remote ftp server response code ssh

Sometimes in order to test the network situation, it takes time to return each phase, such as DNS resolution, time spent establishing a connection, the time it took to establish a connection to the preparation of a transmission, the time spent building a connection to the transmission, the duration of the process, the amount of data downloaded, the download speed, the amount of data uploaded, Upload speed and so on. The following script gets the above information:

Curl's data see: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html

Copy Code code as follows:

###################################
### author:www.ttlsa.com ###
### QQ Group: 39514058 ###
### e-mail:service@ttlsa.com ###
###################################
Use strict;
Use Data::D umper;
Use Www::curl::easy;

if (! @ARGV) {
Print "Usaging: $ url\n";
Print "For example: $ www.ttlsa.com\n";
Exit
}

My $curl = new Www::curl::easy;
Open My $response _body, ">/dev/null";
$curl->setopt (curlopt_header,1);
$curl->setopt (curlopt_url, $ARGV [0]);
$curl->setopt (curlopt_writedata,\ $response _body);
$curl->perform;
My $err = $curl->errbuf;
if (! $err) {
 my $st = &gettime;
 my $http _code = $curl->getinfo (curlinfo_response_code);
 my $http _dns_time = $curl->getinfo (curlinfo_namelookup_time);
 my $http _conn_time = $curl->getinfo (curlinfo_connect_time);
  #my $http _app_time = $curl->getinfo (curlinfo_appconnect_time);
 my $http _pre_tran_time = $curl->getinfo (curlinfo_pretransfer_time);
 my $http _start_tran_time = $curl->getinfo (curlinfo_starttransfer_time);
 my $http _total_time = $curl->getinfo (curlinfo_total_time);
 my $http _size_down = $curl->getinfo (curlinfo_size_download);
 my $http _speed_down = $curl->getinfo (curlinfo_speed_download);

printf "Local_time:%s, Http_code:%d, dns_time:%.3fms, Conn_time:%.3fms, Pre_tran_time:%.3fms, Start_tran_time:%.3FMS , Total_time:%.3fms, Size_download:%db, Speed_download:%db/s ", ($st, $http _code, $http _dns_time, $http _conn_time,$ Http_pre_tran_time, $http _start_tran_time, $http _total_time, $http _size_down, $http _speed_down);
Write

Format stdout_top=
Site various response time Details-@| |
$%
=========================
+---------------------+------+-------------+--------------+--------------------------+------------------------+ -------------+-----------+------------+
| local time | Status | DNS Resolution Time | Establish Connection Time | From establishing a connection to preparing a transfer time | from establishing a connection to starting transmission time | Whole Process Time | Download Data | Average Download speed |
+---------------------+------+-------------+--------------+--------------------------+------------------------+ -------------+-----------+------------+
.

 format stdout=
 |@<<<<<<<<<<<<<<<<<<< <| @<<<<| @<<<<<<<<<<<| @<<<<<<<<<<<<| @<<<<<<<<<<<<<<<<<<<<<<<<| @<<<<<<<<<<<<<<<<<<<<<<| @<<<<<<<<<<<| @<<<<<<<<<| @<<<<<<<<<<|
  $st, $http _code, $http _dns_time. " MS ", $http _conn_time." MS ", $http _pre_tran_time." MS ", $http _start_tran_time." MS ", $http _total_time." MS ", $http _size_down." B ", $http _speed_down." b/S "
 +---------------------+------+-------------+--------------+--------------------------+---------- --------------+--------------+----------+------------+
 
}else{
 print "Error: $err \ n";
}

Sub GetTime ()
{
My @time = (localtime) [5,4,3,2,1,0];
$time [0]+=1900;
$time [1]+=1;
Return sprintf ("%04u-%02u-%02u%02u:%02u:%02u", @time);
}





The same commands under the shell command are as follows:

Copy Code code as follows:

# Curl-o/dev/null-s-W%{http_code}:%{http_connect}:%{time_namelookup}:%{time_connect}:%{time_pretransfer}:%{time _starttransfer}:%{time_total}:%{size_download}:%{speed_download} www.ttlsa.com


Not explained, specifically see Man curl

use CURL to get various response times of the site –dns resolution time, response time, transmission time

Copy Code code as follows:

Curl-o/dev/null-s-W%{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_ Pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} digdeeply.org


This is a blog site to carry out the curl command. The output is usually HTML code, sent to/dev/null by the-o parameter. The-s parameter removes all state information. The-w parameter lets curl the state information of the timer output.



-dns parsing of each time period in an HTTP request, waiting for server response, getting content, etc.

Here is a detailed explanation of the-w argument, translated by Me (digdeeply). Please point out the wrong place. (Original English: http://curl.haxx.se/docs/manpage.html)
The following variable names are available:

-W,--write-out
The following variables are output in the format that curl thinks fit, output variables need to be in the format of%{variable_name}, if you need to output%,double, that is percent percent, at the same time, \ n is newline, \ r is carriage return, \ t is tab.

Url_effective the URL that is fetched last. This is most meaningful if your ' ve told Curl to follow Location:headers.

Filename_effective the ultimate filename that Curl writes out to. This are only meaningful if curl are told to write to a file with the--remote-name or--output option. It ' s most useful in combination with the--remote-header-name option. (Added in 7.25.1)

Http_code HTTP status code, such as 200 success, 301 turn, 404 Not Found, 500 server error, etc. (The numerical response code that is found in the last retrieved HTTP (s) or FTP (s) transfer. In 7.18.2 the alias Response_code is added to show the same info.)

Http_connect the numerical code that is found in the last response (from a proxy) to a Curl connect request. (Added in 7.12.4)

Time_Total total time, in seconds. Accurate to three digits after the decimal point. (The total time, in seconds, which is the full operation lasted.) The time is displayed with millisecond resolution.)

Time_namelookup The DNS resolution time, from the start of the request to the time the DNS resolution was completed. (The time, in seconds, it took from the start until the name resolving is completed.)

Time_connect connection time, from start to establish TCP connection to complete the time, including the front DNS resolution time, if you need to get a simple connection time, with this time_connect time minus the front time_namelookup time. The following is the same, no longer repeat. (The time, in seconds, it took from the "start until" the TCP connect to the remote host (or proxy) is completed.)

The Time_appconnect connection establishes the completion time, such as SSL/SSH, etc. to establish a connection or to complete three handshake times. (The time, in seconds, it took from the "start until" ssl/ssh/etc Connect/handshake to the remote host is completed. (Added in 7.19.0))

Time_pretransfer the time from the beginning to the preparation of the transfer. (The time, in seconds, it took from the start until the file transfer is just about to begin. This includes all Pre-transfer commands and negotiations that are specific to the particular protocol (s) involved.)

Time_redirect redirect time, including DNS resolution, connection, pre transfer, and transmission time of several redirects before the last transmission. (The time, in seconds, it took to all redirection steps include name lookup, connect, Pretransfer and transfer before the Final transaction was started. Time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3))

Time_starttransfer start transmission time. The time that the WEB server has taken to return the first byte of data after the request was made (the times, in seconds, it took from the "start until" and "the" I just about to being Tran Sferred. This includes Time_pretransfer and also the "time" the server needed to calculate the result.)

size_download Download size. (The total amount of bytes that were downloaded.)

Size_upload upload size. (The total amount of bytes that were uploaded.)

Size_header the size of the header that is downloaded (the total amount of bytes of the downloaded headers.)

The size of the size_request request. (The total amount of bytes this were sent in the HTTP request.)

Speed_download download speed, unit-byte per second. (the average download speed that curl measured for the complete download. Bytes per second.)

Speed_upload upload speed, unit-byte per second. (the average upload speed that curl measured for the complete upload. Bytes per second.)

Content_Type is Content-type, needless to say, this is a visit to my blog home return results sample (text/html; charset=utf-8); (The Content-type Document, if there is any.)

Num_connects number of the new connects made in the recent transfer. (Added in 7.12.3)

Num_redirects number of redirects that were followed in the request. (Added in 7.12.3)

Redirect_url when a HTTP request is made without-l to follow redirects, this variable'll show the actual URL a Redirec T would take to. (Added in 7.18.2)

Ftp_entry_path the initial path Libcurl ended up in and logging on to the remote FTP server. (Added in 7.15.4)

Ssl_verify_result SSL authentication results, return 0 to indicate successful authentication. (The result of the SSL Peer certificate verification This is requested. 0 means the verification was successful. (Added in 7.19.0))

If you use the-w parameter more than once, output in the last format. If This option is used several times, the last one would be used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.