【发布时间】:2020-01-22 12:15:55
【问题描述】:
记录器 由多个线程(虚拟用户)调用的函数。我想执行 函数 printDebugLogs(debugLogsRepo) 和 printResponseCodeRepo(responseCodeRepo) 仅在给定持续时间过去后,即 IsElapsedTime 时 return true。目前所有线程都会多次执行这个函数。
//Logger函数多线程执行
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;
}
【问题讨论】:
标签: javascript k6