加强文明网站内容建设,做家旅游的视频网站好,网络优化分为,archigo建筑网站✎ 码甲说 hello#xff0c;老伙计们#xff0c;又有半个多月没见了#xff0c;今天给大家分享一个干货编程小技巧#xff0c;上至架构师、下至开发者、运维男、QA#xff0c; 得此利器#xff0c;事半功倍。cURL在我的眼里#xff0c;就是一个httpClient手办#xff… ✎ 码甲说 hello老伙计们又有半个多月没见了今天给大家分享一个干货编程小技巧上至架构师、下至开发者、运维男、QA 得此利器事半功倍。cURL在我的眼里就是一个httpClient手办老伙计们知道怎么获得cURL请求的具体耗时吗 cURL支持格式化输出请求的详细信息请参阅cURL手册页的-w、–write outformat获取更多信息。如题我们只关注如何知晓cURL请求的时间细节, 下面时间以s为单位。1. 创建一个文本文件curl-format.txt 粘贴下面内容time_namelookup: %{time_namelookup}s\ntime_connect: %{time_connect}s\ntime_appconnect: %{time_appconnect}s\ntime_pretransfer: %{time_pretransfer}s\ntime_redirect: %{time_redirect}s\ntime_starttransfer: %{time_starttransfer}s\n----------\ntime_total: %{time_total}s\n2.发起请求url -w curl-format.txt -o /dev/null -s http://wordpress.com/在windows机器上是curl -w curl-format.txt -o NUL -s http://wordpress.com/旁白解释-w curl-format.txt 通知cURL使用格式化的输出文件-o /dev/null 将请求的输出重定向到/dev/null-s 通知cURL不显示进度条http://wordpress.com/ 是我们请求的URL请使用引号包围尤其当你的URL包含查询字符串文本输出time_namelookup: 0.001stime_connect: 0.037stime_appconnect: 0.000stime_pretransfer: 0.037stime_redirect: 0.000s
time_starttransfer: 0.092s----------time_total: 0.164s输出的啥意思呢我解释一下time_namelookupDNS 域名解析的时间就是把http://wordpress.com 转换成ip地址的过程time_connectTCP 连接建立的时间就是三次握手的时间time_appconnectSSL/SSH等上层协议建立连接的时间比如 connect/handshake 的时间time_pretransfer从请求开始到响应开始传输的时间time_starttransfer从请求开始到第一个字节将要传输的时间time_total这次请求花费的全部时间制作成Linux/Mac快捷命令(alise 别名)alias curltimecurl -w \$HOME/.curl-format.txt\ -o /dev/null -s 制作成Linux/Mac 独立脚本脚本不需要单独的包含格式化的文本。在可执行路径中创建名为curltime的文件粘贴下面内容#!/bin/bashcurl -w - -o /dev/null -s $ EOFtime_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_appconnect: %{time_appconnect}\ntime_pretransfer: %{time_pretransfer}\ntime_redirect: %{time_redirect}\ntime_starttransfer: %{time_starttransfer}\n----------\ntime_total: %{time_total}\n
EOF制作成windows快捷方式bat批处理把下面的命令写入curltime.bat:curl -w %~dp0curl-format.txt -o NUL -s %*以上手段后curltime wordpress.org就可以拿到cURL的请求耗时。 cURL还有一个小技巧模拟连接/传输超时。连接超时时间用--connect-timeout参数来指定数据传输的最大允许时间用-m参数来指定。连接超时的话出错提示形如curl: (28) connect() timed out!数据传输的最大允许时间超时的话出错提示形如curl: (28) Operation timed out after 2000 milliseconds with 0 bytes received ●有关[Http持久连接]的一切卷给你看●HTTP1.1 Keep-Alive到底算不算长连接●宝藏好物gRPCurl●SignalR 开发到生产部署闭坑指南●SignalR在React/Go技术栈的实践●我是状态机 一颗永远骚动的机器引擎●你怕是对MD5算法有误解点个在看你最好看仅代表此刻认知文章永久更新地址请移步原文