Created
March 16, 2020 10:51
-
-
Save SlyNet/5cdb3f2b2f3a1b279c161f0b08f6594a to your computer and use it in GitHub Desktop.
Loadtest.js
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, group, sleep } from 'k6' | |
import { Trend, Rate, Counter } from 'k6/metrics' | |
// var host = "http://localhost/headquarters" | |
//var tag = "6KCM4SN5" | |
// var host = "https://hqrc.mysurvey.solutions" | |
// var host = "http://192.168.88.24:57627" | |
// var tag = "HSD4X2KZ" | |
var host = __ENV.K6_HOST || 'http://127.0.0.1:5001' | |
var tag = __ENV.K6_WEBINTERVIEW || '5GSCZL2D' | |
var longestRun = __ENV.K6_LONGESTRUN || '5m' | |
//var host = "http://192.168.88.85:9700" | |
//var tag = "YLN9CJNW" | |
// var regex = /WebInterview\/([a-f\d]+)\//gm | |
var answeringTrend = new Trend('answering') | |
var interviewCreated = new Counter('interview_created') | |
export let errorRate = new Rate('answer_errors') | |
export let slowAnswersRate = new Rate('slow_answer') | |
const questionnaire = { | |
text_skills: { | |
type: 'Text', | |
id: 'acc0f49ab85e7ad2a3df13e52da6b576' | |
}, | |
single_select_1: { | |
type: 'SingleOption', | |
id: '77e784315e850239a811db91beb6c455' | |
}, | |
multi_select_1: { | |
type: 'MultiOption', | |
id: 'd335d7aaebff5eab8cba7b5432136e0c' | |
}, | |
curr_gps: { | |
type: 'Gps', | |
id: 'd912d67e11499883c6a7462325ee99ce' | |
}, | |
hobbies: { | |
type: 'TextList', | |
id: 'b6d1100cb6fdb9385251d2be85f5ce4f' | |
}, | |
linked_to_list: { | |
type: 'SingleOption', | |
id: 'f0296a9d711740ba907ee4965c55c4dd' | |
}, | |
num_fingers: { | |
type: 'Integer', | |
id: 'eb7e86a6f62b490e04b5fbccf039de63' | |
} | |
} | |
var interviewWrap = function (interviewId) { | |
return { | |
interviewId, | |
getInterviewDetails() { | |
return http | |
.get(`${host}/api/webinterview/getInterviewDetails?interviewId=${interviewId}`) | |
.json() | |
}, | |
getSidebarChildSectionsOf(section) { | |
return http | |
.get(`${host}/api/webinterview/getFullSectionInfo?interviewId=${interviewId}§ionId=${section}`) | |
.json() | |
}, | |
getFullSectionInfo(section) { | |
return http | |
.get(`${host}/api/webinterview/getFullSectionInfo?interviewId=${interviewId}§ionId=${section}`) | |
.json() | |
}, | |
getCompleteInfo() { | |
return http | |
.get( | |
`${host}/api/webinterview/getCompleteInfo?interviewId=${interviewId}` | |
) | |
.json() | |
}, | |
complete(comment) { | |
var res = http.post( | |
`${host}/api/webinterview/commands/completeInterview?interviewId=${interviewId}`, | |
JSON.stringify( | |
{ | |
comment: comment | |
}), | |
{ | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
} | |
) | |
check(res, { | |
'complete': r => r.status == 200, | |
'complete duration': r => r.timings.duration < 200 | |
}) | |
}, | |
getInterviewStatus() { | |
return http | |
.get( | |
`${host}/api/webinterview/getInterviewStatus?interviewId=${interviewId}` | |
) | |
.json() | |
}, | |
answerQuestion(answer, questionId, questionType) { | |
var res = http.post( | |
`${host}/api/webinterview/commands/answer${questionType}Question?interviewId=${interviewId}`, | |
JSON.stringify({ | |
answer, | |
identity: questionId | |
}), | |
{ | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
} | |
) | |
sleep(4000) | |
answeringTrend.add(res.timings.waiting) | |
var ok = check(res, { | |
'status was 200': r => r.status == 200 | |
}) | |
slowAnswersRate.add(check(res, { | |
'transaction time > 1000': r => r.timings.duration > 1000 | |
})) | |
errorRate.add(!ok) | |
return res | |
} | |
} | |
} | |
export let options = { | |
stages: [ | |
//{ duration: "1m", target: 100 }, | |
// {duration: "2m", target: 300 }, | |
//{ target: 1 } | |
//{duration: "120s", target: 1000} | |
// { duration: "30s", target: 1 }, | |
//{ duration: "30s", target: 200 }, | |
// { duration: "30s", target: 400 }, | |
// { duration: "30s", target: 600 }, | |
// { duration: "30s", target: 800 }, | |
{ duration: "1m", target: 100 }, | |
{ duration: "1m", target: 200 }, | |
{ duration: "1m", target: 300 }, | |
{ duration: "1m", target: 400 }, | |
{ duration: longestRun, target: 10000 }, | |
{ duration: "30s", target: 0 }, | |
], | |
systemTags: [] | |
} | |
function getRandomInt(max) { | |
return Math.floor(Math.random() * Math.floor(max)) | |
} | |
// creating one interview to warm | |
export function setup() { | |
const page = `${host}/WebInterview/${tag}/Start` | |
const start = http.get(page) | |
const antiForgeryCookie = start.cookies['CSRF-TOKEN'][0].value | |
const interviewPage = http.post(page, { | |
__RequestVerificationToken: antiForgeryCookie | |
}) | |
} | |
export default function (data) { | |
var interview = null | |
var details = null | |
let section = null | |
group('setting up', () => { | |
const page = `${host}/WebInterview/${tag}/Start` | |
const start = http.get(page) | |
const antiForgeryCookie = start.cookies['CSRF-TOKEN'][0].value | |
const interviewPage = http.post(page, { | |
__RequestVerificationToken: antiForgeryCookie | |
}) | |
check(interviewPage, { | |
time: r => r.status == 200 | |
}) | |
interviewCreated.add(1) | |
var url = interviewPage.url | |
var interviewId = url.slice(url.length - 38, url.length - 6) | |
interview = interviewWrap(interviewId) | |
details = interview.getInterviewDetails() | |
section = interview.getFullSectionInfo(details.firstSectionId) | |
}) | |
group('answering', () => { | |
group(`text`, () => { | |
interview.answerQuestion( | |
'text' + Math.random(), | |
questionnaire.text_skills.id, | |
questionnaire.text_skills.type | |
) | |
}) | |
group(`single-select`, () => { | |
interview.answerQuestion( | |
getRandomInt(12) + 1, | |
questionnaire.single_select_1.id, | |
questionnaire.single_select_1.type | |
) | |
}) | |
group(`multi-select`, () => { | |
let answer = [] | |
for (let i = 0; i < 12; i++) { | |
if (answer.length === 6) break | |
const option = getRandomInt(12) + 1 | |
if (!answer.includes(option)) answer.push(option) | |
} | |
interview.answerQuestion( | |
answer, | |
questionnaire.multi_select_1.id, | |
questionnaire.multi_select_1.type | |
) | |
}) | |
group('gps', () => { | |
interview.answerQuestion( | |
{ longitude: 36.227558, latitude: 50.00687 }, | |
questionnaire.curr_gps.id, | |
questionnaire.curr_gps.type | |
) | |
}) | |
let listAnswer = [] | |
group('list', () => { | |
for (let i = 0; i < 5; i++) { | |
listAnswer.push({ | |
value: i, | |
text: 'text' + Math.random() | |
}) | |
} | |
interview.answerQuestion( | |
listAnswer, | |
questionnaire.hobbies.id, | |
questionnaire.hobbies.type | |
) | |
}) | |
group('linked to list', () => { | |
const answerIndex = getRandomInt(listAnswer.length) | |
interview.answerQuestion( | |
answerIndex, | |
questionnaire.linked_to_list.id, | |
questionnaire.linked_to_list.type | |
) | |
}) | |
group('numeric', () => { | |
const answer = getRandomInt(10) | |
interview.answerQuestion( | |
answer, | |
questionnaire.num_fingers.id, | |
questionnaire.num_fingers.type | |
) | |
}) | |
}) | |
group('completion', () => { | |
interview.getCompleteInfo() | |
interview.getInterviewStatus() | |
interview.complete('text' + Math.random()) | |
}) | |
} | |
export function teardown(data) { | |
// 4. teardown code | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment