【问题标题】:Execute functions once for all Threads对所有线程执行一次函数
【发布时间】: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


    【解决方案1】:

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

    如果您正在调试事物,您只需使用一个 VU 即可运行您的脚本。或者,如果由于某种原因您需要在调试时运行多个 VU,您可以通过检查 __VU execution context variable 在单个 VU 中打印调试日志:

    if (__VU == 1) {
        // print logs
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多