Created
December 24, 2021 12:17
-
-
Save GaetanoPiazzolla/f611b1646348b7612198ce4fde707995 to your computer and use it in GitHub Desktop.
K6 test looping through 3 endpoints
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'; | |
import { check, sleep } from 'k6'; | |
import { Trend, Rate } from 'k6/metrics'; | |
const getTrend = new Trend('Get Books'); | |
const getErrorRate = new Rate('Get Books error'); | |
const postTrend = new Trend('Add Book'); | |
const postErrorRate = new Rate('Add Book error'); | |
const orderTrend = new Trend('Add Order'); | |
const orderErrorRate = new Rate('Add Order error'); | |
export let options = { | |
stages: [ | |
{ duration: "10s", target: `${__ENV.USERS}` }, | |
{ duration: "100s", target: `${__ENV.USERS}` }, | |
{ duration: "10s", target: 0 } | |
] | |
}; | |
export default function () { | |
const url = `http://nginx:4000/spring-${__ENV.TYPE}/` | |
const params = { | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
}; | |
const addBookBody = JSON.stringify({ | |
author: `Author Name ${__ITER}`, | |
isbn: `${__VU}`, | |
title: `always the same title`, | |
year: 1900 | |
}); | |
const requests = { | |
'Get Books': { | |
method: 'GET', | |
url: url +'books', | |
params: params, | |
}, | |
'Add Book': { | |
method: 'POST', | |
url: url+'books', | |
params: params, | |
body: addBookBody, | |
}, | |
'Add Order': { | |
method: 'POST', | |
url: url + 'orders?bookIsbn=11111111&firstName=Gaetano', | |
params: params, | |
body: null | |
} | |
}; | |
const responses = http.batch(requests); | |
const getResp = responses['Get Books']; | |
const postResp = responses['Add Book']; | |
const addOrderResp = responses['Add Order']; | |
check(getResp, { | |
'status is 200': (r) => r.status === 200, | |
}) || getErrorRate.add(1); | |
getTrend.add(getResp.timings.duration); | |
check(postResp, { | |
'status is 200': (r) => r.status === 200, | |
}) || postErrorRate.add(1); | |
postTrend.add(postResp.timings.duration); | |
check(addOrderResp, { | |
'status is 200': (r) => r.status === 200, | |
}) || orderErrorRate.add(1); | |
orderTrend.add(addOrderResp.timings.duration); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code snippet Related to medium article:
https://blog.devgenius.io/an-epic-tale-comparing-jdbc-and-r2dbc-in-a-real-world-scenario-part-2-2-d908df49651c#823f-8a39553a5cf4