如何设置阶段运行的迭代次数?

时间:2019-01-08 16:44:03

标签: k6

我正在尝试在K6中设置分阶段设置和运行的运行。我试图找出如何设置K6的方法,以便在阶段完成后开始运行的新迭代。我是在阶段模块中简单地包含迭代,还是需要做其他事情?

1 个答案:

答案 0 :(得分:0)

您基本上想要的是一组给定的阶段,可以长时间重复。

有两种解决方案:

  1. 只需在bash / cmd / shell脚本中循环运行该脚本-运行之间会有一些暂停,因为k6需要再次初始化。此外,您将有多个输出,但这可能不错,请参见下文

  2. 具有所需的阶段数。您可以手动执行此操作,也可以将其编写在测试脚本中:

import http from "k6/http";
import { check, sleep } from "k6";

let stages = [// total 30 seconds
        // Ramp-up from 1 to 5 VUs in 10s
        { duration: "10s", target: 5 },

        // Stay at rest on 5 VUs for 5s
        { duration: "5s" },

        // Ramp-down from 5 to 0 in 15s
        { duration: "15s", target: 0}
    ];

let final_stages = [];

// 30 seconds by 120 seconds is 3600 seconds or 1 hour
for (let i =0 ;i < 120; i++) {
    final_stages = final_stages.concat(stages)
}

export let options = {
    stages: final_stages,
    discardResponseBodies: true
};

export function setup() {
    console.log(JSON.stringify(options.stages));
}
export default function(testStart) {
        // normal execution
        let res = http.get("http://httpbin.org/");
        check(res, { "status is 200": (r) => r.status === 200 });
}

我强烈建议使用--no-summary甚至甚至使用--no-thresholds运行k6,或者仅使用阈值来运行它,因为这样做可能会很耗时,因为您可能会耗尽内存而仅收集数据在k6内部。这意味着您可能应该使用一些storage for the metrics,例如influxdb甚至是Load Impact的Insights。当然,这可能不适用于您的情况-您必须检查:)。

回答我之前理解的问题:“我想拥有阶段,然后我想对脚本进行一些具体的迭代,而不是在给定的时间内执行另一阶段的迭代”

  

简单的答案是否定的。

     

如果您有阶段,则测试的迭代和持续时间是相互关联的   专有-仅考虑其中之一。看着那(这   结束可能的解决方法。

     

详细答案:

     

如果我正确理解,您想要做的就是一堆舞台   然后在所有阶段运行之后,有几个VUS   给定迭代次数。目前不支持此功能,   对于我们(k6团队)来说很有趣,为什么您会需要这个。

     

解决方法::目前,我可以想到两种解决方法-都是hacky:

     
      
  1. 如果您在以后的迭代中不需要测试,请进行两次测试,并制作一个bash / shell / cmd脚本以在一次之后运行它们   另一个。在测试运行期间,您和您之间会有一些暂停   将有两个输出,但肯定会更容易。
  2.   
  3. 在所有阶段都完成了足够长的持续时间以运行所有迭代之后,进行另一个阶段。有一个变量,你   记录整个测试的开始-最好在设置和   然后计算何时到达最后一个阶段。运行多次迭代   根据需要,然后在其余的测试中入睡。
  4.   
     

请务必注意,目前尚无办法   在VU之间进行通信,因此您必须确定   每个10个VU将执行5次以获得50次迭代,也许一个VU将   完成得更快或更晚或类似的事情。

     

import http from "k6/http";
import { check, sleep } from "k6";

export let options = {
    stages: [
        // Ramp-up from 1 to 5 VUs in 10s
        { duration: "10s", target: 5 },

        // Stay at rest on 5 VUs for 5s
        { duration: "5s" },

        // run for some amount of time after that
        { duration: "15s"}
    ],
    discardResponseBodies: true
};
export function setup() {
    return Date.now();
}

let iter = 5; // how many iteration per VU should be done in the iterations part
export default function(testStart) {
    // 15ms is the amount of time for the pre iterations phase
    if (Date.now() - testStart > 15 * 1000) {
        // "iterations part"
        if (iter == 0) { // we have ran out of iterations to do
            sleep(15000) // sleep for the duration of the phase
        }
        iter = iter - 1;
        // debug log
        console.log(__VU, __ITER, iter);

        // the actual part you want  to do
        let res = http.get("http://httpbin.org/");
        check(res, { "status is 200": (r) => r.status === 200 });

    } else {
        // normal execution
        let res = http.get("http://httpbin.org/");
        check(res, { "status is 200": (r) => r.status === 200 });
    }
}
 
  显然,这需要针对您的情况量身定制   并不确切,但再次有趣的是   想这样做,为什么。

     

旁注:

     

在讨论arrival rate based execution时,   已经讨论过了,目前的计划是可以支持的,但是   这项工作可能距离alpha / beta至少一个月的时间   能力,因为需要大量内部重构   支持我们想要添加的所有内容。你应该   可能会写出您的用例问题,以供考虑   以后考虑。