菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow 使用 k6breadcrumb arrow 场景breadcrumb arrow 概念breadcrumb arrow 开放模型和封闭模型
开源

开放模型和封闭模型

不同的 k6 执行器有不同的调度 VU 的方式。一些执行器使用 封闭模型,而到达率执行器使用 开放模型

简而言之,在封闭模型中,VU 迭代只有在前一次迭代完成后才开始。另一方面,在开放模型中,VU 的到达与迭代完成无关。不同的模型适用于不同的测试目标。

封闭模型

在封闭模型中,每次迭代的执行时间决定了测试中执行的迭代次数。下一次迭代直到前一次迭代完成后才开始。

因此,在封闭模型中,新 VU 迭代的启动或到达率与迭代持续时间紧密耦合(即,VU 的 exec 函数从开始到结束的时间,默认为 export default function

JavaScript
import http from 'k6/http';

export const options = {
  scenarios: {
    closed_model: {
      executor: 'constant-vus',
      vus: 1,
      duration: '1m',
    },
  },
};

export default function () {
  // The following request will take roughly 6s to complete,
  // resulting in an iteration duration of 6s.

  // As a result, New VU iterations will start at a rate of 1 per 6s,
  // and we can expect to get 10 iterations completed in total for the
  // full 1m test duration.

  http.get('https://quickpizza.grafana.com/api/delay/6');
}

运行此脚本将产生类似如下结果

bash

running (1m01.5s), 0/1 VUs, 10 complete and 0 interrupted iterations
closed_model ✓ [======================================] 1 VUs  1m0s

使用封闭模型的缺点

当 VU 迭代的持续时间与新 VU 迭代的启动紧密耦合时,目标系统的响应时间会影响测试的吞吐量。响应时间越慢,迭代持续时间越长,新迭代的到达率越低,反之亦然。在某些测试文献中,此问题称为 协调遗漏。

换句话说,当目标系统承受压力并开始响应变慢时,封闭模型的负载测试会等待,导致迭代持续时间增加,并且新 VU 迭代的到达率逐渐降低。

当目标是模拟特定新 VU 到达率,或者更一般地模拟吞吐量(例如,每秒请求数)时,这种影响并不理想。

开放模型

与封闭模型相比,开放模型将 VU 迭代与迭代持续时间解耦。目标系统的响应时间不再影响目标系统的负载。

为了解决这种协调问题,您可以使用开放模型,它将新 VU 迭代的启动与迭代持续时间解耦。这减少了目标系统响应时间的影响。

Arrival rate closed/open models

k6 使用两种 到达率 执行器实现开放模型:恒定到达率递增到达率

JavaScript
import http from 'k6/http';

export const options = {
  scenarios: {
    open_model: {
      executor: 'constant-arrival-rate',
      rate: 1,
      timeUnit: '1s',
      duration: '1m',
      preAllocatedVUs: 20,
    },
  },
};

export default function () {
  // With the open model arrival rate executor config above,
  // new VU iterations will start at a rate of 1 every second,
  // and we can thus expect to get 60 iterations completed
  // for the full 1m test duration.
  http.get('https://quickpizza.grafana.com/api/delay/6');
}

运行此脚本将产生类似如下结果

bash
running (1m09.3s), 000/011 VUs, 60 complete and 0 interrupted iterations
open_model ✓ [======================================] 011/011 VUs  1m0s  1 iters/s