From 65aec7b5767f6ae2e1ebe6e5f0a80b655ff9d106 Mon Sep 17 00:00:00 2001 From: Kevin Abrar Khansa Date: Sun, 29 Dec 2024 16:12:09 +0700 Subject: [PATCH] Update k6 scenarios --- k6/src/create-contact.js | 2 +- k6/src/register.js | 21 ++-------------- k6/src/scenarios.js | 50 ++++++++++++++++++++++++++++++++++++++ k6/src/supports/contact.js | 3 ++- k6/src/supports/user.js | 24 +++++++++++++++++- 5 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 k6/src/scenarios.js diff --git a/k6/src/create-contact.js b/k6/src/create-contact.js index 159dca0..2b3fa1b 100644 --- a/k6/src/create-contact.js +++ b/k6/src/create-contact.js @@ -15,7 +15,7 @@ export function setup() { first_name: 'Contact', last_name: `No ${i}`, email: `contact${i}@example.com`, - phone: `i` + phone: `+${i}` }) } diff --git a/k6/src/register.js b/k6/src/register.js index 11d0a02..aa2194b 100644 --- a/k6/src/register.js +++ b/k6/src/register.js @@ -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() { diff --git a/k6/src/scenarios.js b/k6/src/scenarios.js new file mode 100644 index 0000000..81d950c --- /dev/null +++ b/k6/src/scenarios.js @@ -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) +} \ No newline at end of file diff --git a/k6/src/supports/contact.js b/k6/src/supports/contact.js index d13df8f..e911d97 100644 --- a/k6/src/supports/contact.js +++ b/k6/src/supports/contact.js @@ -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, }) } \ No newline at end of file diff --git a/k6/src/supports/user.js b/k6/src/supports/user.js index 858ead5..7c711f5 100644 --- a/k6/src/supports/user.js +++ b/k6/src/supports/user.js @@ -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 } \ No newline at end of file