Hello @eyeveebee This is my sample code.
import http from 'k6/http';
import { check, sleep, group} from "k6";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/2.4.0/dist/bundle.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.3/index.js";
import { Rate } from 'k6/metrics';
// Define a list of scenarios with different configurations
let EXEC_agentDetail = true
let EXEC_agentWallet = true
let EXEC_businessGroup = false
const scenariosStore = [
{ vus: 3, duration: '15s', name: 'agentDetail', enable: EXEC_agentDetail, execGroup: allCase.agentDetail_Testcase },
{ vus: 3, duration: '15s', name: 'agentWallet', enable: EXEC_agentWallet, execGroup: allCase.agentWallet_Testcase },
{ vus: 3, duration: '15s', name: 'businessGroup', enable: EXEC_businessGroup, execGroup: allCase.businessGroup_Testcase },
];
export let options = {
scenarios: {},
thresholds: {
http_req_failed: ['rate<0.01'],
},
summaryTrendStats: ['count', 'avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)'],
};
let result = 0;
let isFirstScenario = true; // เพิ่มตัวแปรเพื่อตรวจสอบว่าเป็น Scenario แรก
for (let i = 0; i < scenariosStore.filter(element=>{
return element.enable===true
}).length; i++) {
let scenarioName = scenariosStore[i].name
let groupName = scenariosStore[i].execGroup
let VUS = scenariosStore[i].vus
let DURATION = scenariosStore[i].duration
if (isFirstScenario) { // ตรวจสอบว่าเป็น Scenario แรก
options.scenarios[scenarioName] = {
executor: 'constant-vus',
exec: groupName,
vus: VUS,
duration: DURATION,
startTime: "0s", // ตั้งค่า startTime เป็น "0s" สำหรับ Scenario แรก
};
isFirstScenario = false; // กำหนดให้ไม่เป็น Scenario แรกในการประมวลผลถัดไป
} else {
options.scenarios[scenarioName] = {
executor: 'constant-vus',
exec: groupName,
vus: VUS,
duration: DURATION,
startTime: result + "s",
};
}
result+=0.5
let waitingTime = parseInt(DURATION); // กำหนด startTime ตามค่า result และเพิ่ม 1 วินาที
result+=waitingTime
}
for (let key in options.scenarios) {
// Each scenario automaticall tags the metrics it generates with its own name
let scenariosName = key
let httpReqDuration = `http_req_duration{scenario:${scenariosName}}`;
let iterationGroup = `iterations{scenario:${scenariosName}}`;
let myRate = `myRate{scenario:${scenariosName}}`;
// Check to prevent us from overwriting a threshold that already exists
if (!options.thresholds[httpReqDuration]) {
options.thresholds[httpReqDuration] = [];
options.thresholds[iterationGroup] = [];
options.thresholds[myRate] = [];
}
// 'max>=0' is a bogus condition that will always be fulfilled
options.thresholds[httpReqDuration].push('max>=0', 'p(95) < 1000');
options.thresholds[iterationGroup].push('rate>=0');
options.thresholds[myRate].push('rate>=0');
}
const groupToken = [
{ enable: EXEC_agentDetail, tokenName: "agentDetail_Token", tokenSetup: "TOKEN" },
{ enable: EXEC_agentWallet, tokenName:"agentWallet_Token", tokenSetup: "TOKEN" },
{ enable: EXEC_businessGroup, tokenName:"businessGroup_Token", tokenSetup: "TOKEN" },
];
export function setup(){
let sellingToken = execSetupToken(groupToken.filter(element=>{
return element.enable===true
}))
return sellingToken
}
const myRate = new Rate('myRate');
export function execSetupToken(groupToken) {
for (let i = 0; i < groupToken.length; i++) {
console.log("get_some_token");
}
}
export function agentDetail_Testcase() {
group('agentDetail_Testcase', function () {
const apiUrl = 'https://www.google.com/';
let response = http.get(apiUrl);
check(response, {
'status is 200': (r) => r.status === 200,
});
myRate.add(response.timings.duration);
});
}
export function agentWallet_Testcase() {
group('agentWallet_Testcase', function () {
const apiUrl = 'https://www.google.com/';
let response = http.get(apiUrl);
check(response, {
'status is 200': (r) => r.status === 200,
});
myRate.add(response.timings.duration);
});
}
export function businessGroup_Testcase() {
group('businessGroup_Testcase', function () {
const apiUrl = 'https://www.google.com/';
let response = http.get(apiUrl);
check(response, {
'status is 200': (r) => r.status === 200,
});
myRate.add(response.timings.duration);
});
}
const groupName = [
{enable: EXEC_agentDetail, Testcase: agentDetail_Testcase},
{enable: EXEC_agentWallet, Testcase: agentWallet_Testcase},
{enable: EXEC_businessGroup, Testcase: businessGroup_Testcase},
];
export default function allCase() {
execTestcase(groupName.filter(element=>{
return element.enable===true
}));
sleep(1);
};
function execTestcase(groupName) {
for (let i = 0; i < groupName.length; i++) {
groupName[i].Testcase();
}
}
export function handleSummary(data) {
for (let key in options.scenarios) {
let duration = parseInt(options.scenarios[key].duration);
let ratePerScenario = data.metrics[`iterations{scenario:${key}}`].values.count / duration;
data.metrics[`iterations{scenario:${key}}`].values.rate = ratePerScenario;
}
return {
"sellingScenario.html": htmlReport(data),
stdout: textSummary(data, { indent: " ", enableColors: true }),
};
}