iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >如何获取站点的各类响应时间
  • 350
分享到

如何获取站点的各类响应时间

2023-06-09 19:06:31 350人浏览 独家记忆
摘要

这篇文章主要讲解了“如何获取站点的各类响应时间”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何获取站点的各类响应时间”吧!有时候为了测试网络情况,需要返回每个阶段的耗时时间,比如DNS解析

这篇文章主要讲解了“如何获取站点的各类响应时间”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何获取站点的各类响应时间”吧!

有时候为了测试网络情况,需要返回每个阶段的耗时时间,比如DNS解析耗时,建立连接所消耗的时间,从建立连接到准备传输所使用的时间,从建立连接到传输开始所使用的时间,整个过程耗时,下载的数据量,下载速度,上传数据量,上传速度等等。下面的脚本获取以上信息:

代码如下:

###################################
### author: www.ttlsa.com   ###
### QQ群: 39514058     ###
### E-mail: service@ttlsa.com  ###
###################################
use strict;
use Data::Dumper;
use WWW::Curl::Easy;

if(!@ARGV){
 print "Usaging: $0 url\n";
 print "For example: $0 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=
 站点各类响应时间明细-@||
 $%
 =========================
 +---------------------+------+-------------+--------------+--------------------------+------------------------+-------------+-----------+------------+
 | 本地时间 | 状态 | DNS解析时间 | 建立连接时间 | 从建立连接到准备传输时间 |从建立连接到开始传输时间| 整个过程时间| 下载数据量|平均下载速度|
 +---------------------+------+-------------+--------------+--------------------------+------------------------+-------------+-----------+------------+
 .

 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);
}


如何获取站点的各类响应时间

shell命令下也有相同的命令如下所示:

代码如下:

# 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 


使用 cURL 获取站点的各类响应时间 – dns解析时间,响应时间,传输时间

代码如下:

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 


这是一个本人博客站点执行 curl 命令的情况。输出通常是 html 代码,通过 -o 参数发送到 /dev/null。-s 参数去掉所有状态信息。-w 参数让 curl 输出的计时器的状态信息。

如何获取站点的各类响应时间

一次http请求中的各个时间段-dns解析,等待服务器响应,获取内容等

下边对-w参数做个详细的解释,由我(DigDeeply)翻译。有不对的地方请大家指出。(英文原文:http://curl.haxx.se/docs/manpage.html)
以下是可用的变量名:

 -w, --write-out
 以下变量会按CURL认为合适的格式输出,输出变量需要按照%{variable_name}的格式,如果需要输出%,double一下即可,即%%,同时,\n是换行,\r是回车,\t是TAB。

url_effective The URL that was fetched last. This is most meaningful if you've told curl to follow location: headers.

filename_effective The ultimate filename that curl writes out to. This is only meaningful if curl is 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状态码,如200成功,301转向,404未找到,500服务器错误等。(The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.)

http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)

time_total 总时间,按秒计。精确到小数点后三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)

time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间。(The time, in seconds, it took from the start until the name resolving was completed.)

time_connect 连接时间,从开始到建立tcp连接完成所用时间,包括前边DNS解析时间,如果需要单纯的得到连接时间,用这个time_connect时间减去前边time_namelookup时间。以下同理,不再赘述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)

time_appconnect 连接建立完成时间,如SSL/ssh等建立连接或者完成三次握手时间。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))

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 重定向时间,包括到最后一次传输前的几次重定向的DNS解析,连接,预传输,传输时间。(The time, in seconds, it took for 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 开始传输时间。在发出请求之后,WEB 服务器返回数据的第一个字节所用的时间(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.)

size_download 下载大小。(The total amount of bytes that were downloaded.)

size_upload 上传大小。(The total amount of bytes that were uploaded.)

size_header 下载的header的大小(The total amount of bytes of the downloaded headers.)

size_request 请求的大小。(The total amount of bytes that were sent in the HTTP request.)

speed_download 下载速度,单位-字节每秒。(The average download speed that curl measured for the complete download. Bytes per second.)

speed_upload 上传速度,单位-字节每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)

content_type 就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)

num_connects Number of 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 was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)

ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)

ssl_verify_result ssl认证结果,返回0表示认证成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))

若多次使用-w参数,按最后一个的格式输出。If this option is used several times, the last one will be used.

感谢各位的阅读,以上就是“如何获取站点的各类响应时间”的内容了,经过本文的学习后,相信大家对如何获取站点的各类响应时间这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: 如何获取站点的各类响应时间

本文链接: https://www.lsjlt.com/news/257527.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何获取站点的各类响应时间
    这篇文章主要讲解了“如何获取站点的各类响应时间”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何获取站点的各类响应时间”吧!有时候为了测试网络情况,需要返回每个阶段的耗时时间,比如DNS解析...
    99+
    2023-06-09
  • mysql如何获取时间整点
    目录mysql获取时间整点1.获取当天整点时间2.当前时间往前推的时间点总结mysql获取时间整点 1.获取当天整点时间 SELECT * FROM ( SELECT DATE_FORMAT(...
    99+
    2023-02-07
    mysql获取时间整点 mysql获取时间 mysql获取整点时间
  • js如何以天为单位获取响应的时间戳
    小编给大家分享一下js如何以天为单位获取响应的时间戳,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!以“天”为单位获取响应的时间戳通常都会获取三天前的时间,12 个...
    99+
    2023-06-27
  • js如何获取当天24点的时间
    这篇文章将为大家详细讲解有关js如何获取当天24点的时间,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。js获取当天24点的时间new Date(new ...
    99+
    2024-04-02
  • Vue如何获取两个时间点之间的所有间隔时间
    目录获取两个时间点之间的所有间隔时间获取两个时间之间的 间隔时间获取两个日期之间的间隔日期获取指定两个月份之间的 间隔月份获取两个时间点之间的所有间隔时间 说明 使用的是vue@2....
    99+
    2024-04-02
  • MySQL获取当前时间的各种方式
    1 获取当前完整时间 1.1 now()函数 select now(); 输出: 2023-02-15 10:46:17 1.2 sysdate()函数 select sysdate(); 输出: 2023-02-15 10...
    99+
    2023-08-31
    mysql 数据库 Powered by 金山文档
  • javascript如何获取时间戳
    这篇文章主要讲解了“javascript如何获取时间戳”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“javascript如何获取时间戳”吧!js/javascript获取时间戳的5种方法获取...
    99+
    2023-07-05
  • ubuntu如何获取时间戳
    ubuntu获取时间戳的方法:打开终端命令行模式。直接执行“date”命令进行获取。#用法date [OPTION]... [+FORMAT]date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss...
    99+
    2024-04-02
  • linux如何获取时间戳
    linux中获取时间戳的方法:1、打开linux终端;2、在终端命令行中输入“date”命令进行获取时间戳即可。具体操作步骤:在linux系统桌面中使用快捷键【Ctrl+Alt+T】打开linux终端命令行模式。在linux终端命令行中输入...
    99+
    2024-04-02
  • MySQL获取时间、格式转换各类操作的方法是什么
    本篇内容介绍了“MySQL获取时间、格式转换各类操作的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所...
    99+
    2022-11-30
    mysql
  • php中的时间戳如何获取
    本篇内容介绍了“php中的时间戳如何获取”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!为了实现任何时间相关的功能,必须要用到时间戳。在PHP...
    99+
    2023-07-05
  • PHP获取时间戳、获取天周月的起始时间、指定时间所在周、指定时间的各个周等相关函数
    目录 一、时间戳和日期互相转换 二、PHP获取今日、昨日、上周、本周、上月、本月的起始时间戳 三、获取当前周的每天的起始时间 四、获取周的起始时间 1、根据指定时间获取所在周的起始时间和结束时间 2、通过时间戳 获取某周的开始时间和结束时间...
    99+
    2023-09-01
    php
  • php如何获取时间并改成时间戳
    这篇文章主要介绍“php如何获取时间并改成时间戳”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“php如何获取时间并改成时间戳”文章能帮助大家解决问题。PHP是一种广泛应用于互联网的编程语言,它提供了...
    99+
    2023-07-05
  • jquery如何获取当前时间
    使用jquery获取当前时间的方法:1.新建html项目,引入jquery;2.使用getFullYear()、getMonth()+1、getDate()、getHours()、getSeconds()、getSeconds()方法获取当...
    99+
    2024-04-02
  • MySQL中如何获取时间戳
    这篇文章主要介绍了MySQL中如何获取时间戳,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。UNIX_TIMESTAMP(date):获取时间戳SELECT UNIX...
    99+
    2023-06-16
  • html如何获取当前时间
    html中获取当前时间的方法:1、创建html文件;2、给新建文件添加html代码架构;3、使用div标签来显示获取当前时间的值;4、使用javascript中Date对象并通过new Date()获取当前时间即可。具体方法如下:在操作系统...
    99+
    2024-04-02
  • oracle如何获取当前时间
    在Oracle数据库中,可以使用SYSDATE函数来获取当前的日期和时间。SYSDATE函数返回当前日期和时间,包含时分秒。示例如下...
    99+
    2024-04-17
    oracle
  • android如何获取当前时间
    要获取当前时间,可以使用系统提供的Date类或Calendar类来获取。以下是使用Date类和Calendar类获取当前时间的示例代...
    99+
    2024-04-02
  • mysql如何获取当前时间
    在MySQL中,可以使用以下函数来获取当前时间:1. NOW(): 返回当前日期和时间的值,以 'YYYY-MM-DD HH...
    99+
    2023-08-16
    mysql
  • php如何获取北京时间
    这篇文章主要介绍“php如何获取北京时间”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“php如何获取北京时间”文章能帮助大家解决问题。在PHP中,我们可以使用date()函数来获取当前服务器时间。但...
    99+
    2023-07-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作