Created
March 5, 2021 16:49
-
-
Save deepakshrma/13dc141c97c2bb4ff3b4235ee30ecc83 to your computer and use it in GitHub Desktop.
Forget about JMeter, Start writing K6 scripts for Load Testing and Performance Monitoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Forget about JMeter, Start writing K6 scripts for Load Testing and Performance Monitoring | |
| Performance testing or monitoring is not new to us. Whenever we create an API, We do need testing. For code and components, We write unit tests and integration tests. However, for performance, We need to load test and monitor the result. | |
| A Gist page for article: [Forget about JMeter, Start writing K6 scripts for Load Testing and Performance Monitoring](https://medium.com/@deepak-v/writing-k6-scripts-for-load-testing-and-performance-monitoring-25cb2aefa23c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // tests/main.js | |
| import http from "k6/http"; | |
| // Take port from env variable | |
| const apiEndpoint = `http://${__ENV.API_HOST}:${__ENV.API_PORT}`; | |
| export default function () { | |
| http.get(`${apiEndpoint}/users`); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env | |
| REPORT_DATE=$(date +"%Y_%m_%dT%H_%M_%S") | |
| ## Define Environment varibles | |
| PORT=3000 | |
| API_HOST=0.0.0.0 | |
| ## Run node app/index.js | |
| PORT=3000 node app/index.js &>/dev/null & | |
| ## Get node app pid | |
| api_pid=$! | |
| k6 run -e API_PORT=$PORT -e API_HOST=$API_HOST --config config.json --out csv="reports/report_$REPORT_DATE.csv" tests/main.js | |
| ## Kill node app | |
| echo "killing node api" | |
| kill -KILL $api_pid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "stages": [ | |
| { "duration": "5s", "target": 2 }, | |
| { "duration": "2s", "target": 0 } | |
| ], | |
| "thresholds": { "http_req_duration": ["avg<100", "p(95)<200"] }, | |
| "noConnectionReuse": true, | |
| "userAgent": "MyK6UserAgentString/1.0" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import http from "k6/http"; | |
| // Take port from env variable | |
| const apiEndpoint = `http://${__ENV.API_HOST}:${__ENV.API_PORT}`; | |
| export default function () { | |
| http.get(`${apiEndpoint}/users`); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import http from "k6/http"; | |
| // Take port from env variable | |
| const apiEndpoint = `http://${__ENV.API_HOST}:${__ENV.API_PORT}`; | |
| export default function () { | |
| const randomId = Math.ceil(Math.random() * 99 + 1); | |
| http.get(`${apiEndpoint}/user/ID_${randomId}`); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { sleep } from "k6"; | |
| // import modules | |
| import getAllUsersTest from "./getAllUsers.test.js"; | |
| import userByIdTest from "./userById.test.js"; | |
| export default function () { | |
| getAllUsersTest(); | |
| sleep(1); | |
| userByIdTest(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment