打印http/s请求的各个阶段的时间持续时间,例如DNS查找,TLS握手,第一个字节等时间等。与Time Command类似,该命令类似,该命令测量了处理时间, nettime命令测量HTTP/S请求时间。您可以找到有关使用Node.J.理解和测量HTTP时间的更多信息。
注意:命令行选项在0.x和1.x版本之间更改,以使它们与卷曲兼容。如果使用nettime命令行工具,请检查受影响的选项:
-e, --ignore-certificate => -k, --insecure
-u, --unit => -t, --time-unit
-U, --user => -u, --user
程序化接口没有变化,并且保持兼容。
确保安装了Node.js> = 14.8。全球安装nettime软件包,并打印示例网站的时间:
$ npm install -g nettime
$ nettime https://www.google.com
Phase Finished Duration
-----------------------------------
Socket Open 0.023s 0.023s
DNS Lookup 0.024s 0.001s
TCP Connection 0.053s 0.029s
TLS Handshake 0.133s 0.079s
First Byte 0.174s 0.041s
Content Transfer 0.176s 0.002s
Socket Close 0.177s 0.001s
-----------------------------------
Status Code: OK (200)没有任何参数打印使用指令的nettime :
Usage: nettime [options] <URL>
Options:
-V, --version output the version number
-0, --http1.0 use HTTP 1.0
--http1.1 use HTTP 1.1 (default)
--http2 use HTTP 2.0
-c, --connect-timeout <ms> maximum time to wait for a connection
-d, --data <data> data to be sent using the POST verb
-f, --format <format> set output format: text, json, raw
-H, --header <header> send specific HTTP header
-i, --include include response headers in the output
-I, --head use HEAD verb to get document info only
-k, --insecure ignore certificate errors
-L, --location follow redirects
-o, --output <file> write the received data to a file
-t, --time-unit <unit> set time unit: ms, s+ns
-u, --user <credentials> credentials for Basic Authentication
-X, --request <verb> specify HTTP verb to use for the request
-C, --request-count <count> count of requests to make (default: 1)
-D, --request-delay <ms> delay between two requests
-A, --average-timings print an average of multiple request timings
-h, --help output usage information
The default output format is "text" and time unit "ms". Other options
are compatible with curl. Timings are printed to the standard output.
Examples:
$ nettime -f json https://www.github.com
$ nettime --http2 -C 3 -A https://www.google.com
确保您使用node.js> = 14.8。本地安装nettime软件包,并获得等待响应的时间,并下载示例网页的内容:
npm install --save nettime const { nettime , getDuration } = require ( 'nettime' )
nettime ( 'https://www.google.com' )
. then ( result => {
if ( result . statusCode === 200 ) {
let timings = result . timings
let waiting = getDuration ( [ 0 , 0 ] , timings . firstByte )
let downloading = getDuration ( timings . firstByte , timings . contentTransfer )
console . log ( 'Waiting for the response:' , nettime . getMilliseconds ( waiting ) + 'ms' )
console . log ( 'Downloading the content:' , nettime . getMilliseconds ( downloading ) + 'ms' )
}
} )
. catch ( error => console . error ( error ) )主模块导出一个发出HTTP/S请求并返回结果对象的函数。该函数携带属性nettime , getDuration , getMilliseconds和isRedirect ,因此可以将导出作为具有多个静态功能的对象消耗:
const nettime = require ( 'nettime' )
const { nettime , getDuration } = require ( 'nettime' )输入参数是一个带有URL的字符串,用于与多个属性进行请求或对象。
输入对象可以包含:
url :带有URL的字符串以提出请求。credentials :具有username和password字符串属性的对象,用于格式化基本身份验证HTTP标头。data :使用HTTP动词POST和默认情况下的content类型application/x-www-form-urlencoded发送到服务器的字符串或缓冲区。failOnOutputFileError :布尔值,用于防止请求时机操作失败,如果写入输出文件失败。如果设置为false ,则该错误将在标准输出上打印,并且过程退出代码将设置为2。仅当指定outputFile时才有效。默认值为true 。headers :将标题名称作为字符串键和标头值作为字符串值的对象。httpVersion :带有协议版本('1.0','1.1'或'2.0')的字符串将发送到服务器。 (Node.js HTTP support is hard-coded for 1.1. There can be a difference between 1.0 and 1.1 on the server side only. Node.js supports HTTP/2 in the version 8.4.0 or newer with the --expose-http2 command-lime option and in the version 8.8.1 or newer out-of-the-box. Alternatively, you can install a "http2" module as a polyfill.)followRedirects :布尔值继续提出请求,如果原始响应包含重定向的HTTP状态代码includeHeaders :布尔值,用于在承诺结果对象中加入带有响应标头的属性headers ( Object )。如果指定了outputFile ,则将标题写入输出文件的开头。method :在HTTP请求中使用的HTTP动词; GET是默认值,除非未设置-i或-d选项。outputFile :将收到数据写入的文件路径。rejectUnauthorized :布尔值拒绝完成HTTPS请求,如果网站证书的验证失败,则将其设置为true (默认);将其设置为false使请求忽略证书错误。returnResponse :布尔值包括在承诺结果对象中包含带有收到数据的属性response ( Buffer )。requestCount :用于提出多个请求的整数而不是一个请求。requestDelay :整数在每个两个请求之间引入一个延迟(以毫秒为单位)。默认值为100。timeout :设置最长时间(以毫秒为单位)的最长时间(以毫秒为单位)在流产之前应花费。结果对象包含:
httpVersion :http版本,服务器使用(String)响应的HTTP版本。statusCode :响应(整数)的HTTP状态代码。statusMessage :状态代码(字符串)的HTTP状态消息。timings :来自请求的各个阶段的定时属性的对象。时机是一个数组,具有两个整数 - 秒和纳秒秒以来通过了请求,如Process.hrtime返回。headers :带有响应标头的可选对象,如果由选项includeHeaders启用。url :带有请求的URL的可选字符串,如果选项followRedirects设置为true 。 {
"httpVersion" : '1.1' ,
"statusCode" : 200 ,
"statusMessage" : "OK" ,
"timings" : {
"socketOpen" : [ 0 , 13260126 ] ,
"dnsLookup" : [ 0 , 13747391 ] , // Optional, if hostname was specified
"tcpConnection" : [ 0 , 152135165 ] ,
"tlsHandshake" : [ 0 , 433219351 ] , // Optional, if HTTPS protocol was used
"firstByte" : [ 1 , 888887072 ] ,
"contentTransfer" : [ 1 , 891221207 ] ,
"socketClose" : [ 1 , 893156380 ]
}
}如果选项requestCount大于1 ,则结果对象将以与requestCount值相同的长度返回。如果选项followRedirects我们设置为true ,则结果对象将根据重定向响应的存在和计数返回长度的数组。
注意: time-unit参数不仅影响命令行脚本的“文本”输出格式,还影响“ JSON”一个。如果设置为“ MS”,则定时值将以毫秒打印。如果设置为“ s+ns”,则将定时以process.hrtime的格式打印为数组。以编程方式调用nettime函数,将始终以process.hrtime格式为数组。
从nettime/lib/timings模块中将以下功能暴露为命名的导出,以帮助处理Process.hrtime的时序格式和来自多个请求的时间:
getDuration(start, end) :计算两个时间之间的差异。期望两个数组中的两个数组。getMilliseconds(timing) :将时间安排转换为毫秒。期望在process.hrtime的格式中有一个数组,并将结果返回整数。computeAverageDurations(multipleTimings) :计算事件时间数组的平均持续时间。该数组应该包含与nettime响应中的timings对象相同的对象。返回的对象将包含相同的键,指向process.hrtime格式的事件持续时间。createTimingsFromDurations(timings, startTime) :重建事件持续时间的事件时间。 timings对象应该包含与nettime响应中的timings对象相同的键,但指向process.hrtime格式中的事件持续时间。返回的对象将包含相同的键,但指向process.hrtime格式中的事件时间。 startTime参数可以振荡事件时间。默认值不移位 - [0, 0] 。isRedirect(statusCode) :检查HTTP状态代码是否表示重定向。如果是返回,则true为false 。这些方法也可以单独需要:
const { isRedirect } = require ( 'nettime' )
const {
getDuration , getMilliseconds ,
computeAverageDurations , createTimingsFromDurations
} = require ( 'nettime/lib/timings' )方法getDuration , getMilliseconds和isRedirect也可以作为从主要nettime模块导出的nettime函数的静态方法访问。
代替正式的StyleGuide,请注意维护现有的编码样式。添加任何新功能或更改功能的单元测试。用咕unt弹并测试您的代码。
版权(C)2017-2022 Ferdinand Prantl
根据MIT许可获得许可。