Make hostname optional

This commit is contained in:
Alex Hyett 2023-09-27 09:51:36 +01:00
parent 2f551f4d4a
commit 72f0bcd3a8
6 changed files with 39 additions and 16 deletions

View file

@ -1,37 +1,48 @@
# performance-testing # performance-testing
The code from my YouTube video on "How to do Performance Testing with k6" 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 ```sh
cd src cd src
dotnet run dotnet run
``` ```
Using Docker:
```sh
docker-compose up
```
Then in a seperate terminal window run the tests using the commands below. 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 ## Simple Test
```sh ```sh
k6 run -e HOSTNAME=localhost:5157 tests/simple-test.js k6 run tests/simple-test.js
``` ```
## Stress Test ## Stress Test
```sh ```sh
k6 run -e HOSTNAME=localhost:5157 tests/stress-test.js k6 run tests/stress-test.js
```
## Spike Test
```sh
k6 run -e HOSTNAME=localhost:5157 tests/spike-test.js
``` ```
## Load Test ## Load Test
```sh ```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 ## Soak Test
```sh ```sh
k6 run -e HOSTNAME=localhost:5157 tests/soak-test.js k6 run tests/soak-test.js
``` ```

View file

@ -2,6 +2,8 @@ import http from 'k6/http';
import { sleep } from 'k6'; import { sleep } from 'k6';
import { SharedArray } from 'k6/data'; import { SharedArray } from 'k6/data';
if (hostname == null) hostname = 'localhost:5157';
export const options = { export const options = {
stages: [ stages: [
{ duration: '5m', target: 200 }, // ramp up { duration: '5m', target: 200 }, // ramp up
@ -30,6 +32,6 @@ const dates = new SharedArray('dates', function () {
export default () => { export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)]; const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); http.get(`http://${hostname}/age/${randomDate}`);
sleep(1); sleep(1);
}; };

View file

@ -1,10 +1,14 @@
import http from 'k6/http'; import http from 'k6/http';
var hostname = __ENV.HOSTNAME;
if (hostname == null) hostname = 'localhost:5157';
export const options = { export const options = {
vus: 1, vus: 1,
duration: '10s' duration: '10s'
}; };
export default () => { export default () => {
http.get(`http://${__ENV.HOSTNAME}/age/1987-09-01`); http.get(`http://${hostname}/age/1987-09-01`);
}; };

View file

@ -2,6 +2,8 @@ import http from 'k6/http';
import { sleep } from 'k6'; import { sleep } from 'k6';
import { SharedArray } from 'k6/data'; import { SharedArray } from 'k6/data';
if (hostname == null) hostname = 'localhost:5157';
export const options = { export const options = {
stages: [ stages: [
{ duration: '5m', target: 200 }, // ramp up { duration: '5m', target: 200 }, // ramp up
@ -27,6 +29,6 @@ const dates = new SharedArray('dates', function () {
export default () => { export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)]; const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); http.get(`http://${hostname}/age/${randomDate}`);
sleep(1); sleep(1);
}; };

View file

@ -2,6 +2,8 @@ import http from 'k6/http';
import { sleep } from 'k6'; import { sleep } from 'k6';
import { SharedArray } from 'k6/data'; import { SharedArray } from 'k6/data';
if (hostname == null) hostname = 'localhost:5157';
export const options = { export const options = {
stages: [ stages: [
{ duration: '10s', target: 200 }, // ramp up { duration: '10s', target: 200 }, // ramp up
@ -31,6 +33,6 @@ const dates = new SharedArray('dates', function () {
export default () => { export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)]; const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); http.get(`http://${hostname}/age/${randomDate}`);
sleep(1); sleep(1);
}; };

View file

@ -2,6 +2,8 @@ import http from 'k6/http';
import { sleep } from 'k6'; import { sleep } from 'k6';
import { SharedArray } from 'k6/data'; import { SharedArray } from 'k6/data';
if (hostname == null) hostname = 'localhost:5157';
export const options = { export const options = {
stages: [ stages: [
{ duration: '1m', target: 200 }, // ramp up { duration: '1m', target: 200 }, // ramp up
@ -31,6 +33,6 @@ const dates = new SharedArray('dates', function () {
export default () => { export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)]; const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`); http.get(`http://${hostname}/age/${randomDate}`);
sleep(1); sleep(1);
}; };