Logger function called by multiple threads(virtual users).I want to execute function printDebugLogs(debugLogsRepo) and printResponseCodeRepo(responseCodeRepo) only after given duration passed i.e when IsElapsedTime return true.Currently all threads executes this functions many times.
//Logger function execute by multithreads
var debugLogsRepo = []
var responseCodeRepo=new Map();
var duration;
var startTime=new Date().getSeconds();
export function Logger(url, request, response, reqFrom, conf) {
//If logging enable
if (conf.logging) {
//ClienSide logging enable
if (conf.clientSideLog) {
//If request failed
pushFailedRequest(url, request, response, reqFrom, debugLogsRepo);
}
//Insert all response codes(i.e pass and failed)
pushResponseCodeStats(response, responseCodeRepo)
//Condition based on which flush logs
if ((IsTimeElapsed(conf))) {
printDebugLogs(debugLogsRepo);
printResponseCodeRepo(responseCodeRepo)
}
}
}
//If duration has been passed
export function IsTimeElapsed(conf) {
var duration = conf.logInterval;
var currentTime = new Date().getSeconds();
if ((Number(startTime) + Number(duration)) <= currentTime) {
startTime = new Date().getSeconds();
return true
}
return false;
}
Each VU in k6 is an independently running JavaScript runtime, potentially on separate machines even, so you can't synchronize such things between VUs.
If you're debugging things, you can simply run your script with only one VU. Or, if for some reason you need to run multiple VUs while you debug, you can print your debug logs in just a single VU by checking the __VU
execution context variable:
if (__VU == 1) {
// print logs
}