0

由多个线程(虚拟用户)调用的记录器函数。我只想在给定的持续时间过去后执行函数 printDebugLogs(debugLogsRepo) 和 printResponseCodeRepo(responseCodeRepo),即当 IsElapsedTime 返回 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;
}
4

1 回答 1

1

k6 中的每个 VU 都是一个独立运行的 JavaScript 运行时,甚至可能在不同的机器上,所以你不能在 VU 之间同步这些东西。

如果你在调试东西,你可以只用一个 VU 运行你的脚本。或者,如果由于某种原因您需要在调试时运行多个 VU,您可以通过检查__VU 执行上下文变量将调试日志打印在一个 VU 中:

if (__VU == 1) {
    // print logs
}
于 2020-01-23T11:22:31.687 回答