使用 k6 只执行 X 个请求

Use k6 to just execute X number of requests

有没有办法执行 X 次请求?

我尝试过迭代和虚拟用户,但当我不想设置持续时间时,它们似乎需要设置持续时间,只需为每个用户执行 X 次请求。我不想增加或尝试最大化吞吐量,只是触发请求。

谢谢

我想你可以使用简单的 JavaScript 循环。

示例场景 test.js

import http from 'k6/http';

const N = 10;

export default function () {
    for (let i = 0; i < N; ++i) {
        http.get('http://test.k6.io');
    }
}

k6 run test.js 输出,如您所见 - 确切的 10 个请求:

[12:06]d.vinokurov@MBP-DVinokurov[lptl]$ k6 run test.js

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: test.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)


running (00m01.6s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m01.6s/10m0s  1/1 iters, 1 per VU

     data_received..................: 113 kB 71 kB/s
     data_sent......................: 760 B  476 B/s
     http_req_blocked...............: avg=14.56ms  min=2µs      med=2.5µs    max=145.61ms p(90)=14.56ms  p(95)=80.09ms
     http_req_connecting............: avg=14.38ms  min=0s       med=0s       max=143.8ms  p(90)=14.38ms  p(95)=79.09ms
     http_req_duration..............: avg=144.89ms min=143.37ms med=144.2ms  max=151.88ms p(90)=145.74ms p(95)=148.81ms
       { expected_response:true }...: avg=144.89ms min=143.37ms med=144.2ms  max=151.88ms p(90)=145.74ms p(95)=148.81ms
     http_req_failed................: 0.00%  ✓ 0   ✗ 10
     http_req_receiving.............: avg=1.52ms   min=286µs    med=821µs    max=7.62ms   p(90)=2.65ms   p(95)=5.13ms
     http_req_sending...............: avg=30.69µs  min=15µs     med=18µs     max=147µs    p(90)=34.49µs  p(95)=90.74µs
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s
     http_req_waiting...............: avg=143.33ms min=142.69ms med=143.17ms max=144.24ms p(90)=144.02ms p(95)=144.13ms
     http_reqs......................: 10     6.264016/s
     iteration_duration.............: avg=1.59s    min=1.59s    med=1.59s    max=1.59s    p(90)=1.59s    p(95)=1.59s
     iterations.....................: 1      0.626402/s
     vus............................: 1      min=1 max=1
     vus_max........................: 1      min=1 max=1

您可以在您的选项中配置 iterations,它在没有 JS for 循环的情况下也能正常工作。

示例loop.js

import http from 'k6/http';
const N = 10;

export let options = {
  vus: N,
  iterations: N,
};

export default function () {
  http.get('http://test.k6.io');
}

K6输出:

➜ k6 run loop.js

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: loop.js
     output: -

  scenarios: (100.00%) 1 scenario, 10 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 10 iterations shared among 10 VUs (maxDuration: 10m0s, gracefulStop: 30s)

running (00m00.8s), 00/10 VUs, 10 complete and 0 interrupted iterations
default ✓ [======================================] 10 VUs  00m00.7s/10m0s  10/10 shared iters

     data_received..................: 113 kB 151 kB/s
     data_sent......................: 760 B  1.0 kB/s
     http_req_blocked...............: avg=250.28ms min=241.54ms med=250.71ms max=255.63ms p(90)=251.58ms p(95)=253.61ms
     http_req_connecting............: avg=248.8ms  min=240.55ms med=249.06ms max=254.19ms p(90)=250.05ms p(95)=252.12ms
     http_req_duration..............: avg=302.49ms min=244.32ms med=256.28ms max=495.71ms p(90)=495.62ms p(95)=495.67ms
       { expected_response:true }...: avg=302.49ms min=244.32ms med=256.28ms max=495.71ms p(90)=495.62ms p(95)=495.67ms
     http_req_failed................: 0.00%  ✓ 0         ✗ 10
     http_req_receiving.............: avg=49.71ms  min=67µs     med=753.5µs  max=245.41ms p(90)=244.72ms p(95)=245.06ms
     http_req_sending...............: avg=174.6µs  min=43µs     med=160.5µs  max=538µs    p(90)=264.39µs p(95)=401.19µs
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=252.61ms min=243.14ms med=253.33ms max=258.95ms p(90)=256.57ms p(95)=257.76ms
     http_reqs......................: 10     13.329459/s
     iteration_duration.............: avg=553.36ms min=486.05ms med=507.9ms  max=746.35ms p(90)=746.27ms p(95)=746.31ms
     iterations.....................: 10     13.329459/s