如何先等待 post 问题并在 k6 负载测试脚本中使用 while 循环?

How to wait first post issue and use while loop in k6 load test scripts?

我有两个 post 请求。此 post 请求应该 运行 直到响应为“createdIsCompleted” == false。我正在从第二个 post 问题中获取 createdIsCompleted 响应。那么我如何在 while 循环中 运行 两个请求。顺便说一句,在第二个 post 问题应该是 运行 之前,我必须先等待 post 问题...我知道 k6 中没有 await 运算符。但我想学习替代方法。这个 while 循环无法正常工作。响应仍然 returns "createdIsCompleted" == true

let createdISCompleted;
 describe('place products', (t) => {
            while (createdIsCompleted == false) {
              

               http.post(requestUrlAPI + 'PickingProcess.checkCell', JSON.stringify({
                    cellLabel: `${createdCellLabel}`,
                    pickingReferenceNumber: `${createdpickingProcessReferenceNumber}`,
                    allocatedItemId: `${createdAllocatedItemId}`,
                }), generateTokenHeader)

                let placeProductRes = http.post(requestUrlAPI + 'PickingProcess.placeProduct', JSON.stringify({
                    cellLabel: `${createdCellLabel}`,
                    pickingReferenceNumber: `${createdpickingProcessReferenceNumber}`,
                    pickingToteLabel: `${createdPickingToteLabel}`,
                    productLabel: `${createdProductLabel}`,
                    allocatedItemId: `${createdAllocatedItemId}`,
                }), generateTokenHeader) 
                createdIsCompleted = placeProductRes.json().isCompleted;
               
                break;
            }
        });

By the way, I have to wait first post issue before the second post issue should be run...I know there is no await operator in k6

K6 目前只有阻塞调用,因此每个 post 将在下一个开始之前完全完成。

关于循环题你有两(三)个问题:

  • createdISCompleted 是单元化的,所以 while 循环永远不会是 运行 因为它不是 false.
  • 你在声明中有大 S 但你在 while 循环中有小 s
  • 你有 break 在循环的末尾,这意味着它总是会在第一次迭代后退出。