由于 Deno 將 javascript/typescript 帶到桌面,我決定創建一個小工具來.php對 NGINX的 PHP-CGI 網關處理檔案進行壓力測驗。
這對僅 html 靜態站點有效,但似乎502: Gateway Timeout仍考慮包含狀態的獲取回應的承諾,fulfilled從而破壞了我創建的邏輯。這實際上沒問題,因為 NGINX 只是告訴您 PHP-CGI 已關閉,而不是 NGINX。
回應包含創建我想要的邏輯所需的值,但我似乎無法訪問它。
我正在使用的代碼:
/***
* ACME HTTP PHP Gateway Stress Tester 5000
*
*/
let promises: Promise<any>[] = [];
let fulfilled: number = 0;
let rejected: number = 0;
const requestCount: number = 1000; // linear/procedural
const parallelCount: number = 100; // parallel/procedural
const fetchUrl: string = 'http://localhost/info.php';
// Fetch payload (GET)
// Null because url instead
const fetchPayload: any = null;
// Fetch options (GET)
const fetchOptions: any = {
method: 'GET',
headers: {
'Accept': '*/*'
}
};
console.clear();
console.log(`
/***
* ACME HTTP Stress Tester 5000
*
* Stress test started... please wait.
*
*/
`)
let requestCounter: number = 0;
// Request counter...
for (let rc = 0; rc < requestCount; rc) {
requestCounter ;
// Parallel counter...
for (let pc = 0; pc < parallelCount; pc) {
// Push fetch calls into Promise array of promises
promises.push(fetch(fetchUrl, fetchOptions));
}
// Execute fetch calls in Parallel
await Promise.allSettled(promises).then((results) => {
results.forEach((result) => {
console.log(result.value); // <--- ERROR HERE
if(result.status == 'fulfilled') fulfilled ; // Fulfilled counter
if(result.status == 'rejected') rejected ; // Rejected counter
});
// Debug output
console.log(' Wave # :', requestCounter);
console.log(' Total Requests :', results.length);
console.log(' Total Fulfilled:', fulfilled);
console.log(' Total Rejected :', rejected);
console.log('— — — — — — — — — — — — — — — — — — — — ')
/***
* Reset these global vars on every new request
* containing 1,000 parallel fetch calls
*
*/
promises = [];
fulfilled = 0;
rejected = 0;
});
}
console.log(' Stress Test complete.');
我得到的回應:
注意兩個不同的statusone fulfilled& one 502。我需要訪問502狀態。
{
status: "fulfilled",
value: Response {
body: ReadableStream {
locked: false
},
bodyUsed: false,
headers: Headers {
connection: "keep-alive",
"content-length": "10001",
"content-type": "text/html; charset=utf-8",
date: "Fri, 31 Dec 2021 05:53:39 GMT",
etag: '"61bed520-2711"',
"feature-policy": "autoplay 'none'; camera 'none'",
"referrer-policy": "no-referrer-when-downgrade",
server: "nginx",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-xss-protection": "1; mode=block"
},
ok: false,
redirected: false,
status: 502,
statusText: "Bad Gateway",
url: "http://localhost/info.php"
}
}
uj5u.com熱心網友回復:
如果您顯示的輸出來自console.log(result.value)然后請注意回傳的資料的結構。那里沒有兩個status欄位。一個是result.value.status: 'fulfilled',另一個是result.value.value.status: 502。
如果你把它分解成這樣的變數可能會更清楚:
results.forEach((result) => {
// console.log(result.value); // <--- ERROR HERE
const obj = result.value;
console.log(`status is ${obj.status}`);
console.log(`response status code is ${obj.value.status}`);
if(obj.status == 'fulfilled') fulfilled ; // Fulfilled counter
if(obj.status == 'rejected') rejected ; // Rejected counter
});
請注意,實作 Promise 并不意味著 API 呼叫有效。這意味著您成功地從您的呼叫中得到了回應。回應本身可能仍然失敗(在這種情況下是 502 錯誤 - 網關錯誤)但 Promise 仍然實作。如果您只關心 Promise 是否實作(已解決)或被拒絕,那么您所擁有的就是正確的。但是,如果您想計算它發出的呼叫是否有效,那么您可能應該執行以下操作:
results.forEach((result) => {
// console.log(result.value); // <--- ERROR HERE
const obj = result.value;
const responseStatus = obj.value.status;
if(obj.status == 'fulfilled' && responseStatus === 200) {
worked ; // call was successful
} else {
failed ; // call failed with either promise rejection or an error
}
});
通常我們更關心實際結果而不是解決了多少承諾。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/399357.html
標籤:javascript 德诺
