PerformanceTotal 服務
wdio-performancetotal-service 是一個第三方套件,如需更多資訊,請參閱 GitHub | npm 注意
對於 WebdriverIO v8,請使用 3.x.x 版本。
對於 WebdriverIO v7,請使用 2.x.x 版本。
對於 WebdriverIO v6,請使用 1.x.x 版本。
透過這個 webdriver.io 的外掛程式,您可以輕鬆地將效能分析加入測試中的任何流程,無論是純粹的 UI、API,或是兩者的組合。 這個外掛程式提供簡單有效的方式來測量各種程序的響應時間,並找出應用程式中潛在的瓶頸。 有了這些資訊,您可以做出明智的決策,進行最佳化和改進,以提升應用程式的整體效能。
安裝
以開發依賴項的方式安裝此模組的最簡單方法是使用以下指令npm install wdio-performancetotal-service --save-dev
使用方式
將 wdio-performancetotal-service 加入您的 wdio.conf.js
exports.config = {
// ...
services: ['performancetotal']
// ...
};
...或使用服務選項
exports.config = {
// ...
services: [
['performancetotal',
// The options (with default values)
{
disableAppendToExistingFile: false,
performanceResultsFileName: "performance-results",
dropResultsFromFailedTest: false,
performanceResultsDirectory: "performance-results",
analyzeByBrowser: false
}]
]
// ...
};
選項
disableAppendToExistingFile
設定為 true
時,新的測試執行將會重新開始,並覆寫任何現有的效能資料。 設定為 false
(預設) 時,效能資料將會新增至現有的資料。
performanceResultsFileName
您可以設定預設的結果檔案名稱 (performance-results
)。 新建立的結果檔案通常會覆寫舊的檔案。 如果您想保留舊的檔案,建議在檔案名稱中新增時間戳記。 例如
...
performanceResultsFileName: `performance-results_${new Date().getTime()}`
...
dropResultsFromFailedTest
預設為 false
。 當值設定為 true
時,將會排除失敗測試中的效能分析。
performanceResultsDirectory
您可以覆寫專案根目錄中結果目錄的預設路徑。 例如...
performanceResultsFileName: "results-dir/performance-total-results"
...
analyzeByBrowser
預設為false
。 如果為 true,效能資料也會依瀏覽器類型分組。
在測試中使用
只需在您需要的地方匯入 performancetotal,無論是在您的測試檔案中還是任何其他類別中。 這個物件提供用於測量測試中效能資料的方法,包括用於開始和結束效能測量的 sampleStart 和 sampleEnd。 以下是如何使用 performancetotal 物件來測量兩個網站啟動效能的範例
// This test case measures the startup performance of Github and SourceForge using the performancetotal object.
import { performancetotal } from "wdio-performancetotal-service";
it("should test github and sourceforge startup performance", () => {
// Start a new performance measurement for Github
performancetotal.sampleStart("GH-Startup");
// Navigate to Github
browser.url("https://github.com/");
// End the Github measurement and save the results
performancetotal.sampleEnd("GH-Startup");
// ...
// Start a new performance measurement for SourceForge
performancetotal.sampleStart("SF-Startup");
// Navigate to SourceForge
await browser.url("https://sourceforge.net/");
// End the SourceForge measurement and save the results
performancetotal.sampleEnd("SF-Startup");
});
您可以在測試中呼叫 performancetotal.getSampleTime(sampleName) 來擷取單個效能範例所花費的時間。 這可讓您檢查特定程式碼區段的效能,並確保其符合您的預期。
// Get the time taken for a single sample
const sampleTime = performancetotal.getSampleTime(sampleName);
取得結果
當所有測試完成後,會在專案的根資料夾中建立一個新的結果目錄 (預設目錄名稱為 performance-results)。 在此目錄中,會建立兩個檔案:performance-results.json 和 performance-results.csv。 這些檔案包含每個範例的分析資料,包括平均時間、平均值標準誤差 (SEM)、樣本數、最小值、最大值、最早時間和最晚時間。 您可以使用這些資料來識別任何效能迴歸或一段時間的改進情況。
Typescript 支援
此外掛程式支援 Typescript。