打印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許可獲得許可。