Skip to content

Commit

Permalink
Update k6 scenarios
Browse files Browse the repository at this point in the history
kevariable committed Dec 29, 2024
1 parent d2fe9a1 commit 65aec7b
Showing 5 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion k6/src/create-contact.js
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ export function setup() {
first_name: 'Contact',
last_name: `No ${i}`,
email: `contact${i}@example.com`,
phone: `i`
phone: `+${i}`
})
}

21 changes: 2 additions & 19 deletions k6/src/register.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from 'k6/http';
import {check, fail} from 'k6';
import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';
import {createUser} from "./supports/user.js";

export const options = {
vus: 10,
@@ -64,25 +65,7 @@ const registerCallback = ({ uniqueId, password }) => {
name: `user-${uniqueId}`
})

const userRegistered = http.post('http://app:3000/api/users', data, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})

const userRegisteredResponse = userRegistered.json()

const userRegisteredCheck = check(userRegistered, {
'Register response status must 201': userRegistered.status === 201,
'Register response data must not null': userRegisteredResponse.data !== null ,
})

if (! userRegisteredCheck) {
fail(`Failed to registering user-${uniqueId}`)
}

return userRegisteredResponse
return createUser(data)
}

export default function() {
50 changes: 50 additions & 0 deletions k6/src/scenarios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createContact } from './supports/contact.js'
import {createUser, loginUser} from "./supports/user.js";
import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';
import { vu } from 'k6/execution'

export const options = {
scenarios: {
createContacts: {
exec: 'createContacts',
executor: 'shared-iterations',
vus: 20,
iterations: 200,
maxDuration: '15s',
},

userRegistration: {
exec: 'userRegistration',
executor: 'constant-vus',
vus: 20,
duration: '15s',
},
},
};

export const createContacts = () => {
const username = `kevin${(vu.idInInstance % 9) + 1}`
const loginRequest = JSON.stringify({
username,
password: 'password'
})

createContact(JSON.stringify({
first_name: 'Contact',
last_name: `No 1`,
email: `contact1@example.com`,
phone: '+01'
}), loginUser(loginRequest).token)
}

export const userRegistration = () => {
const uniqueId = uuidv4()

const request = JSON.stringify({
username: uniqueId,
password: 'password',
name: uniqueId
})

createUser(request)
}
3 changes: 2 additions & 1 deletion k6/src/supports/contact.js
Original file line number Diff line number Diff line change
@@ -12,8 +12,9 @@ export const createContact = (createContactRequest, token) => {

const createContactResponse = createContact.json()


check(createContact, {
'response must be 200': createContact.status === 200,
'response contains id': createContactResponse.data.id !== undefined,
'response contains id': createContactResponse.data?.id !== undefined,
})
}
24 changes: 23 additions & 1 deletion k6/src/supports/user.js
Original file line number Diff line number Diff line change
@@ -17,8 +17,30 @@ export function loginUser(loginUserRequest){
})

if (!checkLogin) {
fail(`Login failed user ${username}`)
fail(`Login failed user ${loginUserRequest}`)
}

return response
}

export function createUser(data) {
const userRegistered = http.post('http://app:3000/api/users', data, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})

const userRegisteredResponse = userRegistered.json()

const userRegisteredCheck = check(userRegistered, {
'Register response status must 201': userRegistered.status === 201,
'Register response data must not null': userRegisteredResponse.data !== null ,
})

if (! userRegisteredCheck) {
fail(`Failed to registering ${data}`)
}

return userRegisteredResponse
}

0 comments on commit 65aec7b

Please sign in to comment.