Load Testing With K6
前言 POSTMAN 通常是目前在做 API 層級的測試時的首選,不過通常 API 的測試不會只有 End-to-End 的測試就結束了,有些較完整的還會包含負載測試(Load Test),不過若只有用 POSTMAN 就想要完成 Load test 那就辛苦了。 POSTMAN Test Script 這邊我們就使用 POSTMAN 自帶的 Collection (Postman Echo) 來做實驗,這邊簡單起見就只抓一個 Get method 來做。匯出的檔案大概會長以下這樣: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 { "info": { "_postman_id": "{_postman_id}", "name": "{your_collection_name}", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "{your_request_name}", "event": [ { "listen": "test", "script": { "id": "e6b1db6b-fac1-4f5c-820e-d0a6806b1a1e", "exec": [ "tests[\"Body contains headers\"] = responseBody.has(\"headers\");", "tests[\"Body contains args\"] = responseBody.has(\"args\");", "tests[\"Body contains url\"] = responseBody.has(\"url\");", "", "var responseJSON;", "", "try { responseJSON = JSON.parse(responseBody); }", "catch (e) { }", "", "", "tests[\"Args key contains argument passed as url parameter\"] = 'test' in responseJSON.args" ], "type": "text/javascript" } } ], "request": { "method": "GET", "header": [], "url": { "raw": "https://postman-echo.com/get?test=123", "protocol": "https", "host": [ "postman-echo", "com" ], "path": [ "get" ], "query": [ { "key": "test", "value": "123" } ] }, "description": "The HTTP `GET` request method is meant to retrieve data from a server. The data\nis identified by a unique URI (Uniform Resource Identifier). \n\nA `GET` request can pass parameters to the server using \"Query String \nParameters\". For example, in the following request,\n\n> http://example.com/hi/there?hand=wave\n\nThe parameter \"hand\" has the value \"wave\".\n\nThis endpoint echoes the HTTP headers, request parameters and the complete\nURI requested." }, "response": [] } ], "protocolProfileBehavior": {} } 這個方式的缺點就是 Postman 匯出的最小單位是 Collection ,所以很可能實際運用時你會需要把本機測試用的 Collection 與壓力測試用的分成兩個 Collection。 ...