From 72f0bcd3a803307299dcf4fc5578be6eac6728f5 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Wed, 27 Sep 2023 09:51:36 +0100 Subject: [PATCH] Make hostname optional --- README.md | 33 ++++++++++++++++++++++----------- tests/load-test.js | 4 +++- tests/simple-test.js | 6 +++++- tests/soak-test.js | 4 +++- tests/spike-test.js | 4 +++- tests/stress-test.js | 4 +++- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index aadc4f8..362cb81 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,48 @@ # performance-testing The code from my YouTube video on "How to do Performance Testing with k6" -You first need to run the API: +You first need to run the API which you can do in 2 ways. + +Using dotnet: ```sh cd src dotnet run ``` +Using Docker: +```sh +docker-compose up +``` + Then in a seperate terminal window run the tests using the commands below. -I have added a HOSTNAME variable to each of the scripts which needs to be added when running them. +Note if you want to run these scripts against an API hosted on another machine you can use the HOSTNAME environment variable. e.g. + +```sh +k6 run -e HOSTNAME=192.168.1.1:5157 tests/simple-test.js +``` ## Simple Test ```sh -k6 run -e HOSTNAME=localhost:5157 tests/simple-test.js +k6 run tests/simple-test.js ``` ## Stress Test ```sh -k6 run -e HOSTNAME=localhost:5157 tests/stress-test.js -``` - -## Spike Test -```sh -k6 run -e HOSTNAME=localhost:5157 tests/spike-test.js +k6 run tests/stress-test.js ``` ## Load Test ```sh -k6 run -e HOSTNAME=localhost:5157 tests/load-test.js +k6 run tests/load-test.js +``` + +## Spike Test +```sh +k6 run tests/spike-test.js ``` ## Soak Test ```sh -k6 run -e HOSTNAME=localhost:5157 tests/soak-test.js +k6 run tests/soak-test.js ``` \ No newline at end of file diff --git a/tests/load-test.js b/tests/load-test.js index 184b14e..4ee2c1b 100644 --- a/tests/load-test.js +++ b/tests/load-test.js @@ -2,6 +2,8 @@ import http from 'k6/http'; import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; +if (hostname == null) hostname = 'localhost:5157'; + export const options = { stages: [ { duration: '5m', target: 200 }, // ramp up @@ -30,6 +32,6 @@ const dates = new SharedArray('dates', function () { export default () => { const randomDate = dates[Math.floor(Math.random() * dates.length)]; - http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); + http.get(`http://${hostname}/age/${randomDate}`); sleep(1); }; \ No newline at end of file diff --git a/tests/simple-test.js b/tests/simple-test.js index f548518..a715042 100644 --- a/tests/simple-test.js +++ b/tests/simple-test.js @@ -1,10 +1,14 @@ import http from 'k6/http'; +var hostname = __ENV.HOSTNAME; + +if (hostname == null) hostname = 'localhost:5157'; + export const options = { vus: 1, duration: '10s' }; export default () => { - http.get(`http://${__ENV.HOSTNAME}/age/1987-09-01`); + http.get(`http://${hostname}/age/1987-09-01`); }; \ No newline at end of file diff --git a/tests/soak-test.js b/tests/soak-test.js index f817682..76a140a 100644 --- a/tests/soak-test.js +++ b/tests/soak-test.js @@ -2,6 +2,8 @@ import http from 'k6/http'; import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; +if (hostname == null) hostname = 'localhost:5157'; + export const options = { stages: [ { duration: '5m', target: 200 }, // ramp up @@ -27,6 +29,6 @@ const dates = new SharedArray('dates', function () { export default () => { const randomDate = dates[Math.floor(Math.random() * dates.length)]; - http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); + http.get(`http://${hostname}/age/${randomDate}`); sleep(1); }; \ No newline at end of file diff --git a/tests/spike-test.js b/tests/spike-test.js index 59317f3..2fb6846 100644 --- a/tests/spike-test.js +++ b/tests/spike-test.js @@ -2,6 +2,8 @@ import http from 'k6/http'; import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; +if (hostname == null) hostname = 'localhost:5157'; + export const options = { stages: [ { duration: '10s', target: 200 }, // ramp up @@ -31,6 +33,6 @@ const dates = new SharedArray('dates', function () { export default () => { const randomDate = dates[Math.floor(Math.random() * dates.length)]; - http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); + http.get(`http://${hostname}/age/${randomDate}`); sleep(1); }; \ No newline at end of file diff --git a/tests/stress-test.js b/tests/stress-test.js index 2c1781f..db6b355 100644 --- a/tests/stress-test.js +++ b/tests/stress-test.js @@ -2,6 +2,8 @@ import http from 'k6/http'; import { sleep } from 'k6'; import { SharedArray } from 'k6/data'; +if (hostname == null) hostname = 'localhost:5157'; + export const options = { stages: [ { duration: '1m', target: 200 }, // ramp up @@ -31,6 +33,6 @@ const dates = new SharedArray('dates', function () { export default () => { const randomDate = dates[Math.floor(Math.random() * dates.length)]; - http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); + http.get(`http://${hostname}/age/${randomDate}`); sleep(1); }; \ No newline at end of file