Add k6 test files

This commit is contained in:
Alex Hyett 2023-09-26 11:52:17 +01:00
parent 85cfb05060
commit 90651ed6aa
8 changed files with 180 additions and 12 deletions

View file

@ -1,2 +1,32 @@
# 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:
```sh
cd src
dotnet run
```
Then in a seperate terminal window run the tests using the commands below.
I have added a HOSTNAME variable to each of the scripts (except simple-test.js) which needs to be added when running them.
## 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
```
## Load Test
```sh
k6 run -e HOSTNAME=localhost:5157 tests/load-test.js
```
## Soak Test
```sh
k6 run -e HOSTNAME=localhost:5157 tests/soak-test.js
```

View file

@ -1,6 +1,5 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

View file

@ -19,16 +19,6 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7255;http://localhost:5157",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,

35
tests/load-test.js Normal file
View file

@ -0,0 +1,35 @@
import http from 'k6/http';
import { sleep } from 'k6';
import { SharedArray } from 'k6/data';
export const options = {
stages: [
{ duration: '5m', target: 200 }, // ramp up
{ duration: '20m', target: 200 }, // stable
{ duration: '5m', target: 0 }, // ramp-down to 0 users
],
thresholds: {
http_req_duration: ['p(99)<10'], // 99% of requests must complete within 10ms
}
};
const dates = new SharedArray('dates', function () {
var dates = [];
var currentDate = new Date();
var minDate = new Date();
minDate.setFullYear(currentDate.getFullYear() - 100);
for (var i = 0; i < 100; i++) {
var randomTime = Math.random() * (currentDate.getTime() - minDate.getTime());
var randomDate = new Date(minDate.getTime() + randomTime);
dates.push(randomDate.toISOString());
}
return dates;
});
export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`);
sleep(1);
};

10
tests/simple-test.js Normal file
View file

@ -0,0 +1,10 @@
import http from 'k6/http';
export const options = {
vus: 1,
duration: '10s'
};
export default () => {
http.get('http://localhost:5157/age/1987-09-01');
};

32
tests/soak-test.js Normal file
View file

@ -0,0 +1,32 @@
import http from 'k6/http';
import { sleep } from 'k6';
import { SharedArray } from 'k6/data';
export const options = {
stages: [
{ duration: '5m', target: 200 }, // ramp up
{ duration: '8h', target: 200 }, // stable
{ duration: '5m', target: 0 }, // ramp-down to 0 users
],
};
const dates = new SharedArray('dates', function () {
var dates = [];
var currentDate = new Date();
var minDate = new Date();
minDate.setFullYear(currentDate.getFullYear() - 100);
for (var i = 0; i < 100; i++) {
var randomTime = Math.random() * (currentDate.getTime() - minDate.getTime());
var randomDate = new Date(minDate.getTime() + randomTime);
dates.push(randomDate.toISOString());
}
return dates;
});
export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`);
sleep(1);
};

36
tests/spike-test.js Normal file
View file

@ -0,0 +1,36 @@
import http from 'k6/http';
import { sleep } from 'k6';
import { SharedArray } from 'k6/data';
export const options = {
stages: [
{ duration: '10s', target: 200 }, // ramp up
{ duration: '1m', target: 200 }, // stable
{ duration: '10s', target: 2000 }, // ramp up
{ duration: '5m', target: 2000 }, // stable
{ duration: '10s', target: 200 }, // ramp up
{ duration: '1m', target: 200 }, // stable
{ duration: '10s', target: 0 }, // ramp-down to 0 users
],
};
const dates = new SharedArray('dates', function () {
var dates = [];
var currentDate = new Date();
var minDate = new Date();
minDate.setFullYear(currentDate.getFullYear() - 100);
for (var i = 0; i < 100; i++) {
var randomTime = Math.random() * (currentDate.getTime() - minDate.getTime());
var randomDate = new Date(minDate.getTime() + randomTime);
dates.push(randomDate.toISOString());
}
return dates;
});
export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`);
sleep(1);
};

36
tests/stress-test.js Normal file
View file

@ -0,0 +1,36 @@
import http from 'k6/http';
import { sleep } from 'k6';
import { SharedArray } from 'k6/data';
export const options = {
stages: [
{ duration: '1m', target: 200 }, // ramp up
{ duration: '5m', target: 200 }, // stable
{ duration: '1m', target: 400 }, // ramp up
{ duration: '5m', target: 400 }, // stable
{ duration: '1m', target: 800 }, // ramp up
{ duration: '5m', target: 800 }, // stable
{ duration: '5m', target: 0 }, // ramp-down to 0 users
],
};
const dates = new SharedArray('dates', function () {
var dates = [];
var currentDate = new Date();
var minDate = new Date();
minDate.setFullYear(currentDate.getFullYear() - 100);
for (var i = 0; i < 100; i++) {
var randomTime = Math.random() * (currentDate.getTime() - minDate.getTime());
var randomDate = new Date(minDate.getTime() + randomTime);
dates.push(randomDate.toISOString());
}
return dates;
});
export default () => {
const randomDate = dates[Math.floor(Math.random() * dates.length)];
http.get(`http://${__ENV.HOSTNAME}/age/${randomDate}`);
sleep(1);
};