Grafana K6 備忘單:效能工程師應該了解的一切(包含範例和最佳實務)
1.Grafana K6簡介
Grafana K6 是一款專為效能測試而設計的開源工具。它非常適合大規模測試 API、微服務和網站,讓開發人員和測試人員深入了解系統效能。本備忘單將涵蓋每位性能工程師開始使用 Grafana K6 時應了解的關鍵面向。
什麼是 Grafana K6?
Grafana K6 是一款面向開發人員和測試人員的現代負載測試工具,它使效能測試變得簡單、可擴展,並且易於整合到 CI 管道中。
什麼時候使用它?
- 負載測試
- 壓力測試
- 峰值測試
- 效能瓶頸偵測
- API 檢定
- 瀏覽器測試
- 混沌工程
2. Grafana K6 備忘單:基本面
2.1.安裝
透過 Homebrew 或 Docker 安裝 Grafana K6:
brew install k6 # Or with Docker docker run -i grafana/k6 run - <script.js> <h4> 2.2.使用公共 REST API 進行基本測試 </h4> <p>以下是如何使用公用 REST API 來執行簡單測試。 <br> </p> <pre class="brush:php;toolbar:false">import http from "k6/http"; import { check, sleep } from "k6"; // Define the API endpoint and expected response export default function () { const res = http.get("https://jsonplaceholder.typicode.com/posts/1"); // Define the expected response const expectedResponse = { userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto", }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); sleep(1); }
2.2.1 運行Web儀表板的測試和使用
要執行測試並在 Web 儀表板中查看結果,我們可以使用以下命令:
K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run ./src/rest/jsonplaceholder-api-rest.js
這將在報告資料夾中產生一個名為 html-report.html 的報告。
但我們也可以透過造訪以下 URL 在 Web 儀表板中查看結果:
http://127.0.0.1:5665/
存取 URL 後,我們可以在 Web 儀表板中即時查看測試結果。
2.3.使用公共 GraphQL API 進行測試
使用公用 GraphQL API 的範例。
如果您不知道什麼是 GraphQL API,可以存取以下網址:什麼是 GraphQL? 。
有關我們將要使用的 GraphQL API 的更多信息,您可以訪問以下 URL 的文檔:GraphQL Pokémon。
有關如何測試 GraphQL API 的更多信息,您可以訪問以下網址:GraphQL 測試。
這是一個簡單的測試,用於透過名稱獲取寶可夢並檢查回應是否成功。
import http from "k6/http"; import { check } from "k6"; // Define the query and variables const query = ` query getPokemon($name: String!) { pokemon(name: $name) { id name types } }`; const variables = { name: "pikachu", }; // Define the test function export default function () { const url = "https://graphql-pokemon2.vercel.app/"; const payload = JSON.stringify({ query: query, variables: variables, }); // Define the headers const headers = { "Content-Type": "application/json", }; // Make the request const res = http.post(url, payload, { headers: headers }); // Define the expected response const expectedResponse = { data: { pokemon: { id: "UG9rZW1vbjowMjU=", name: "Pikachu", types: ["Electric"], }, }, }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); }
3. 建構績效項目的最佳實踐
3.1.集中配置
在一個地方定義全域設定選項,例如效能閾值、虛擬使用者 (VU) 數量和持續時間,以便於修改。
brew install k6 # Or with Docker docker run -i grafana/k6 run - <script.js> <h4> 3.2.程式碼模組化 </h4> <h4> 3.2.1. REST API 的常數和請求 </h4> <p>將程式碼分離成可重複使用的模組,例如,將常數和請求與測試邏輯分開。 </p> <p>對於我們的 REST API 範例,我們可以建立一個 Constants.js 檔案來儲存 API 的基本 URL,並建立一個 requests-jsonplaceholder.js 檔案來儲存與 API 互動的函數。 <br> </p> <pre class="brush:php;toolbar:false">import http from "k6/http"; import { check, sleep } from "k6"; // Define the API endpoint and expected response export default function () { const res = http.get("https://jsonplaceholder.typicode.com/posts/1"); // Define the expected response const expectedResponse = { userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto", }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); sleep(1); }
現在我們可以建立 requests-jsonplaceholder.js 檔案來儲存與 API 互動的函數。
K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run ./src/rest/jsonplaceholder-api-rest.js
3.2.2. REST API 測試腳本中請求的集成
最後,我們可以建立測試腳本 jsonplaceholder-api-rest.js 來使用我們在 requests-jsonplaceholder.js 檔案中建立的函數。
http://127.0.0.1:5665/
我們的腳本程式碼現在更容易理解,並且如果 URL、參數發生變化或需要添加新方法,則需要進行更改的位置會集中起來,從而使我們的解決方案更易於擴展隨著時間的推移。
我們可以透過創建更多的原子函數來進一步改進我們的腳本,如果有必要,我們可以重複使用這些原子函數來創建更複雜的場景,這樣就更容易理解我們的測試腳本的作用。例如,如果我們想測試貼文是否存在,我們可以建立一個取得貼文並回傳回應的函數,然後我們可以在測試腳本 jsonplaceholder-api-rest.js 中使用此函數。
import http from "k6/http"; import { check } from "k6"; // Define the query and variables const query = ` query getPokemon($name: String!) { pokemon(name: $name) { id name types } }`; const variables = { name: "pikachu", }; // Define the test function export default function () { const url = "https://graphql-pokemon2.vercel.app/"; const payload = JSON.stringify({ query: query, variables: variables, }); // Define the headers const headers = { "Content-Type": "application/json", }; // Make the request const res = http.post(url, payload, { headers: headers }); // Define the expected response const expectedResponse = { data: { pokemon: { id: "UG9rZW1vbjowMjU=", name: "Pikachu", types: ["Electric"], }, }, }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); }
3.2.3. GraphQL API 的常數和請求
我們可以修改constants.js檔案以新增GraphQL API的基本URL和我們需要使用的標頭。
// ./src/config/options.js export const options = { stages: [ { duration: '1m', target: 100 }, // ramp up to 100 VUs { duration: '5m', target: 100 }, // stay at 100 VUs for 5 mins { duration: '1m', target: 0 }, // ramp down ], thresholds: { http_req_duration: ['p(95) <p>現在我們可以建立 requests-graphql-pokemon.js 檔案來儲存與 GraphQL API 互動的函式。 <br> </p> <pre class="brush:php;toolbar:false">// ./src/utils/constants.js export const BASE_URLS = { REST_API: 'https://jsonplaceholder.typicode.com', };
3.2.4. GraphQL API 測試腳本中的請求集成
此時我們可以建立測試腳本來使用我們在 requests-graphql-pokemon.js 檔案中建立的函數。我們將創建一個簡單的測試腳本,用於獲取口袋妖怪的數據並檢查響應是否成功。
// ./src/utils/requests-jsonplaceholder.js import { BASE_URLS } from './constants.js'; import http from 'k6/http'; export function getPosts() { return http.get(`${BASE_URLS.REST_API}/posts`); } export function getPost(id) { return http.get(`${BASE_URLS.REST_API}/posts/${id}`); } export function createPost(post) { return http.post(`${BASE_URLS.REST_API}/posts`, post); } export function updatePost(id, post) { return http.put(`${BASE_URLS.REST_API}/posts/${id}`, post); } export function deletePost(id) { return http.del(`${BASE_URLS.REST_API}/posts/${id}`); }
與api rest 的範例相同,我們可以透過創建更多原子函數來改進我們的腳本,如果需要的話,我們可以重複使用這些原子函數來創建更複雜的場景,這樣就更容易理解我們的測試腳本的內容是的。
還有更好的方法來優化並對回應和請求結果進行更好的參數化,您認為我們可以做什麼?
3.3.動態數據和參數化
使用動態資料來模擬更真實的場景並載入不同的資料集。 K6 允許我們使用共享數組從檔案載入資料。共享數組是一種儲存資料的方式,所有 VU 都可以存取。
我們可以建立一個 users-config.js 檔案來從 JSON 檔案 users.json 載入使用者資料。
brew install k6 # Or with Docker docker run -i grafana/k6 run - <script.js> <pre class="brush:php;toolbar:false">import http from "k6/http"; import { check, sleep } from "k6"; // Define the API endpoint and expected response export default function () { const res = http.get("https://jsonplaceholder.typicode.com/posts/1"); // Define the expected response const expectedResponse = { userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto", }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); sleep(1); }
然後我們可以在測試腳本 jsonplaceholder-api-rest.js 中使用它。
K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run ./src/rest/jsonplaceholder-api-rest.js
4. 項目結構
組織良好的專案結構有助於維護和擴展您的測試。以下是建議的資料夾結構:
http://127.0.0.1:5665/
這種結構有助於保持專案組織有序、可擴展且易於維護,避免專案根目錄混亂。
另一種選擇是按功能將測試腳本分組到資料夾中,您可以測試和比較對您的上下文最有意義的內容。例如,如果您的專案是關於進行交易的錢包,您可以為每種類型的交易(存款、提款、轉帳等)建立一個資料夾,並且在每個資料夾內您可以擁有該特定交易的測試腳本。
import http from "k6/http"; import { check } from "k6"; // Define the query and variables const query = ` query getPokemon($name: String!) { pokemon(name: $name) { id name types } }`; const variables = { name: "pikachu", }; // Define the test function export default function () { const url = "https://graphql-pokemon2.vercel.app/"; const payload = JSON.stringify({ query: query, variables: variables, }); // Define the headers const headers = { "Content-Type": "application/json", }; // Make the request const res = http.post(url, payload, { headers: headers }); // Define the expected response const expectedResponse = { data: { pokemon: { id: "UG9rZW1vbjowMjU=", name: "Pikachu", types: ["Electric"], }, }, }; // Assert the response is as expected check(res, { "status is 200": (r) => r.status === 200, "response is correct": (r) => JSON.stringify(JSON.parse(r.body)) === JSON.stringify(expectedResponse), }); }
在第二個範例中,我們有一個更複雜的資料結構,但我們仍然可以重複使用為第一個範例建立的相同請求函數。
結論
K6 效能測試對於識別瓶頸和確保應用程式可擴展性至關重要。透過遵循模組化程式碼、集中配置和使用動態資料等最佳實踐,工程師可以建立可維護和可擴展的效能測試腳本。
大大的擁抱。
查理自動化
以上是Grafana Kheat 表:性能工程師應該知道的一切的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)