Skip to content

Commit

Permalink
Cleanup load test script. (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
conradwt authored Aug 4, 2024
1 parent 799b69c commit b3e5292
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions performance/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { check, sleep } from 'k6';
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js';

// const http = new Httpx({
// baseUrl: 'http://localhost:4000'
// });

const http = new Httpx();
const http = new Httpx({
baseURL: 'http://localhost:4000',
headers: {
'Content-Type': 'application/json',
},
});

const pauseMin = 2;
const pauseMax = 6;
Expand All @@ -26,29 +27,35 @@ export const options = {
thresholds: {
// load_generator_memory_used_percent: [{ threshold: 'value<=80', abortOnFail: true }],
// load_generator_cpu_percent: [{ threshold: 'value<=80', abortOnFail: true }],
http_req_duration: ['p(95)<=1000'],
http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], // http errors should be less than 1%, otherwise abort the test
http_req_duration: ['p(95) <= 100'], // 95% of the requests must complete in 100ms.
http_req_duration: ['p(99) <= 100'], // 99% of the requests must complete in 100ms.
},
scenarios: {
getPersonByID: {
executor: 'ramping-vus',
gracefulStop: '30s',
// gracefulStop: '30s',
startVUs: 0,
stages: [
{ target: 20, duration: '1m' },
{ target: 20, duration: '3m30s' },
{ target: 0, duration: '1m' },
{ duration: '1m', target: 20 }, // ramp up
{ duration: '1m30s', target: 20 }, // stable
{ duration: '1m', target: 0 }, // ramp down to 0 users
],
gracefulRampDown: '30s',
// gracefulRampDown: '30s',
gracefulRampDown: '0s',
exec: 'getPersonByID',
},
getPeople: {
executor: 'ramping-vus',
gracefulStop: '30s',
// gracefulStop: '30s',
startVUs: 0,
stages: [
{ target: 20, duration: '1m' },
{ target: 20, duration: '3m30s' },
{ target: 0, duration: '1m' },
{ duration: '1m', target: 20 }, // ramp up
{ duration: '1m30s', target: 20 }, // stable
{ duration: '1m', target: 0 }, // ramp down to 0 users
],
gracefulRampDown: '30s',
// gracefulRampDown: '30s',
gracefulRampDown: '0s',
exec: 'getPeople',
},
},
Expand All @@ -58,14 +65,14 @@ export const options = {

export function getPersonByID() {
// Get Person By ID
getPersonByID(randomIntBetween(1, 4))
_getPersonByID(randomIntBetween(1, 4))

// Add random delay
sleep(randomIntBetween(pauseMin, pauseMax))
}

export async function getPersonByID(id) {
const query = gql`
export async function _getPersonByID(id) {
const query = `
query GetPersonByID($id: ID!) {
person(id: $id) {
firstName
Expand All @@ -82,10 +89,8 @@ export async function getPersonByID(id) {
}
`;

http.addHeader('Content-Type', 'application/json')

const response = await http.asyncPost(
'http://localhost:4000/graphql',
'/graphql',
JSON.stringify({
query: query,
variables: {
Expand All @@ -99,6 +104,7 @@ export async function getPersonByID(id) {
})
)

// check that response is 200
check(response, {
"status was 200": (r) => r.status === 200
})
Expand All @@ -108,14 +114,14 @@ export async function getPersonByID(id) {

export function getPeople() {
// Get People
getPeople()
_getPeople()

// Add random delay
sleep(randomIntBetween(pauseMin, pauseMax))
}

export async function getPeople() {
const query = gql`
export async function _getPeople() {
const query = `
query GetPeople {
people {
id
Expand All @@ -134,10 +140,8 @@ export async function getPeople() {
}
`;

http.addHeader('Content-Type', 'application/json')

const response = await http.asyncPost(
'http://localhost:4000/graphql',
'/graphql',
JSON.stringify({
query: query
},
Expand All @@ -148,6 +152,7 @@ export async function getPeople() {
})
)

// check that response is 200
check(response, {
"status was 200": (r) => r.status === 200
})
Expand Down

0 comments on commit b3e5292

Please sign in to comment.