Skip to content

Commit

Permalink
Add k6 tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel González Lopes <[email protected]>
  • Loading branch information
dgzlopes committed Apr 3, 2023
1 parent 007b1f2 commit 33ab2ee
Show file tree
Hide file tree
Showing 15 changed files with 664 additions and 22 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ build
# secrets
.env

bin
bin

summary.json
screenshot.png
screenshots
29 changes: 29 additions & 0 deletions k6/01.basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import http from "k6/http";
import { check, sleep } from "k6";

const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';

export const options = {
vus: 5,
duration: '10s',
};

export default function () {
let restrictions = {
"maxCaloriesPerSlice": 1000,
"mustBeVegetarian": false,
"excludedIngredients": ["pepperoni"],
"excludedTools": ["knife"],
"maxNumberOfToppings": 6,
"minNumberOfToppings": 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': 23423,
},
});
check(res, { "status is 200": (res) => res.status === 200 });
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
sleep(1);
}
32 changes: 32 additions & 0 deletions k6/02.stages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import http from "k6/http";
import { check, sleep } from "k6";

const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';

export const options = {
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
};

export default function () {
let restrictions = {
"maxCaloriesPerSlice": 1000,
"mustBeVegetarian": false,
"excludedIngredients": ["pepperoni"],
"excludedTools": ["knife"],
"maxNumberOfToppings": 6,
"minNumberOfToppings": 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': 23423,
},
});
check(res, { "status is 200": (res) => res.status === 200 });
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
sleep(1);
}
44 changes: 44 additions & 0 deletions k6/03.lifecycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import http from "k6/http";
import { check, sleep } from "k6";

const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';

export const options = {
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
};

export function setup() {
let res = http.get(BASE_URL)
if (res.status !== 200) {
throw new Error(`Got unexpected status code ${res.status} when trying to setup. Exiting.`)
}
}

export default function () {
let restrictions = {
"maxCaloriesPerSlice": 1000,
"mustBeVegetarian": false,
"excludedIngredients": ["pepperoni"],
"excludedTools": ["knife"],
"maxNumberOfToppings": 6,
"minNumberOfToppings": 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': 23423,
},
});
check(res, { "status is 200": (res) => res.status === 200 });
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
sleep(1);
}

export function teardown(){
// TODO: Send notification to Slack
console.log("That's all folks!")
}
50 changes: 50 additions & 0 deletions k6/04.metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import http from "k6/http";
import { check, sleep } from "k6";
import { Trend, Counter } from "k6/metrics";

const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';

export const options = {
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
};

const pizzas = new Counter('quickpizza_number_of_pizzas');
const ingredients = new Trend('quickpizza_ingredients');

export function setup() {
let res = http.get(BASE_URL)
if (res.status !== 200) {
throw new Error(`Got unexpected status code ${res.status} when trying to setup. Exiting.`)
}
}

export default function () {
let restrictions = {
"maxCaloriesPerSlice": 500,
"mustBeVegetarian": false,
"excludedIngredients": ["pepperoni"],
"excludedTools": ["knife"],
"maxNumberOfToppings": 6,
"minNumberOfToppings": 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': 23423,
},
});
check(res, { "status is 200": (res) => res.status === 200 });
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
pizzas.add(1);
ingredients.add(res.json().pizza.ingredients.length);
sleep(1);
}

export function teardown(){
// TODO: Send notification to Slack
console.log("That's all folks!")
}
50 changes: 50 additions & 0 deletions k6/05.thresholds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import http from "k6/http";
import { check, sleep } from "k6";
import { Trend, Counter } from "k6/metrics";

const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';

export const options = {
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<500', 'p(99)<1000'],
quickpizza_ingredients: [{ threshold: 'avg<6', abortOnFail: false }],
},
};

const pizzas = new Counter('quickpizza_number_of_pizzas');
const ingredients = new Trend('quickpizza_ingredients');

export function setup() {
let res = http.get(BASE_URL)
if (res.status !== 200) {
throw new Error(`Got unexpected status code ${res.status} when trying to setup. Exiting.`)
}
}

export default function () {
let restrictions = {
"maxCaloriesPerSlice": 500,
"mustBeVegetarian": false,
"excludedIngredients": ["pepperoni"],
"excludedTools": ["knife"],
"maxNumberOfToppings": 6,
"minNumberOfToppings": 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': 23423,
},
});
check(res, { "status is 200": (res) => res.status === 200 });
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
pizzas.add(1);
ingredients.add(res.json().pizza.ingredients.length);
sleep(1);
}
56 changes: 56 additions & 0 deletions k6/06.summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import http from "k6/http";
import { check, sleep } from "k6";
import { Trend, Counter } from "k6/metrics";

const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';

export const options = {
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<500', 'p(99)<1000'],
quickpizza_ingredients: [{ threshold: 'avg<6', abortOnFail: false }],
},
};

const pizzas = new Counter('quickpizza_number_of_pizzas');
const ingredients = new Trend('quickpizza_ingredients');

export function setup() {
let res = http.get(BASE_URL)
if (res.status !== 200) {
throw new Error(`Got unexpected status code ${res.status} when trying to setup. Exiting.`)
}
}

export default function () {
let restrictions = {
"maxCaloriesPerSlice": 500,
"mustBeVegetarian": false,
"excludedIngredients": ["pepperoni"],
"excludedTools": ["knife"],
"maxNumberOfToppings": 6,
"minNumberOfToppings": 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': 23423,
},
});
check(res, { "status is 200": (res) => res.status === 200 });
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
pizzas.add(1);
ingredients.add(res.json().pizza.ingredients.length);
sleep(1);
}

export function handleSummary(data) {
return {
'summary.json': JSON.stringify(data, null, 2),
}
}
75 changes: 75 additions & 0 deletions k6/07.scenarios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import http from "k6/http";
import { check, sleep } from "k6";
import { Trend, Counter } from "k6/metrics";

const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';

export const options = {
scenarios: {
smoke: {
exec: "getPizza",
executor: "constant-vus",
vus: 1,
duration: "10s",
},
stress: {
exec: "getPizza",
executor: "ramping-vus",
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
gracefulRampDown: "5s",
startTime: "10s",
},
},
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<500', 'p(99)<1000'],
quickpizza_ingredients: [{ threshold: 'avg<6', abortOnFail: false }],
},
};

const pizzas = new Counter('quickpizza_number_of_pizzas');
const ingredients = new Trend('quickpizza_ingredients');

export function setup() {
let res = http.get(BASE_URL)
if (res.status !== 200) {
throw new Error(`Got unexpected status code ${res.status} when trying to setup. Exiting.`)
}
}

export function getPizza() {
let restrictions = {
"maxCaloriesPerSlice": 500,
"mustBeVegetarian": false,
"excludedIngredients": ["pepperoni"],
"excludedTools": ["knife"],
"maxNumberOfToppings": 6,
"minNumberOfToppings": 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': 23423,
},
});
check(res, { "status is 200": (res) => res.status === 200 });
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
pizzas.add(1);
ingredients.add(res.json().pizza.ingredients.length);
sleep(1);
}

export function teardown(){
// TODO: Send notification to Slack
console.log("That's all folks!")
}

export function handleSummary(data) {
return {
'summary.json': JSON.stringify(data, null, 2),
}
}
Loading

0 comments on commit 33ab2ee

Please sign in to comment.