Skip to content

Commit

Permalink
update question/answer IDs & change payload to accomodate Arrays (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
JurreBrandsenInfoSupport authored Mar 27, 2024
1 parent d1a2f43 commit 545a20b
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions stress-test/payload.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
/* eslint-disable import/no-anonymous-default-export */
// ts-check

import http from "k6/http";

/**
* Array of possible answer options.
* @type {string[]}
*/
const answerOptions = [
"clu2mvriq007ky5dmjuelhsbj",
"clu2mvris007ly5dmufv524uo",
"clu2mvriu007my5dm9i11gvpx",
"clu2mvriw007ny5dml4kztguo",
"clu8gvcn2002niw792r89j29l",
"clu8gvfgb007liw79mb9lgzvx",
"clu8gvfgk007miw799dv29tsi",
"clu8gvfgo007niw79wefxw4j9",
];

/**
* Array of question IDs.
* @type {string[]}
*/
const questionIds = [
"clu2mvqfz000hy5dmey2nwyee",
"clu2mvqgo000ly5dmbsjzn75c",
"clu2mvqgz000ny5dmqziyomfg",
"clu2mvqtq002ny5dmz7ncmnp9",
"clu2mvqtw002oy5dm9odncalw",
"clu2mvqu1002py5dmppihq4ow",
"clu2mvqu8002qy5dmrs7ezr2c",
"clu2mvque002ry5dmg291mb7i",
"clu2mvquk002sy5dmzjjbvj1s",
"clu2mvqve002wy5dmqkmwyyqd",
"clu8gvcn2002niw792r89j29l",
"clu8gvcnd002oiw79e1b3aee2",
"clu8gvcnn002piw79czd19e3q",
"clu8gvco4002qiw79jsvxpwwe",
"clu8gvcoo002siw79cgwq4z54",
"clu8gvcof002riw79eaasuvr9",
"clu8gvcpx002wiw79zwe692bg",
"clu8gvct10035iw79tzi5sdiz",
"clu8gvcy9003kiw79yqx50bmn",
"clu8gvd0n003qiw79hg9r2iz2",
];

/**
* Function to simulate user behavior by making a GET request to fetch page content and a POST request to set question results.
*/
export function simulateUserBehavior() {
export default function () {
const surveyGeneralUrl = "http://localhost:3000/survey/general";

const surveyGeneralResponse = http.get(surveyGeneralUrl);
Expand Down Expand Up @@ -59,8 +62,8 @@ export function simulateUserBehavior() {
}

// Extract the UserId from the HTML content
const userIdStartIndex = surveyGeneralResponse.body?.indexOf("UserId:") + 8; // Length of 'UserId:' is 7
const userIdEndIndex = surveyGeneralResponse.body?.indexOf(
const userIdStartIndex = surveyGeneralResponse.body.indexOf("UserId:") + 8; // Length of 'UserId:' is 7
const userIdEndIndex = surveyGeneralResponse.body.indexOf(
"</div>",
userIdStartIndex,
);
Expand All @@ -72,21 +75,27 @@ export function simulateUserBehavior() {
let userId = userIdString.split('"')[2];

// remove the \ from the userId
userId = userId?.replace(/\\/g, "");
if (userId !== undefined) {
userId = userId.replace(/\\/g, "");
}

// Make a POST request to /api/trpc/survey.setQuestionResult with extracted userId
const setQuestionResultUrl =
"http://localhost:3000/api/trpc/survey.setQuestionResult?batch=1";
const payload = {
0: {
json: {
userId: userId,
questionId: questionIds[Math.floor(Math.random() * questionIds.length)],
answerId:
answerOptions[Math.floor(Math.random() * answerOptions.length)],
},
json: [
{
userId: userId,
questionId:
questionIds[Math.floor(Math.random() * questionIds.length)],
answerId:
answerOptions[Math.floor(Math.random() * answerOptions.length)],
},
],
},
};

const headers = {
"Content-Type": "application/json",
};
Expand Down

0 comments on commit 545a20b

Please sign in to comment.