curl 如何同时并行运行500个api请求

f0ofjuux  于 2022-11-13  发布在  其他
关注(0)|答案(3)|浏览(188)

需要对一个API进行压力测试,并且要求在一秒钟内有500个API请求。有测试这个的命令吗?
正在使用xargs,但响应时间戳并不完全相同,大约每10个请求更改一次。

dtcbnfnu

dtcbnfnu1#

一个强大而简单的选项是使用k6,来自https://k6.io/docs/getting-started/running-k6/的示例,模拟500 VUsvirtual-users):
简单的. js

import http from 'k6/http';
import { sleep } from 'k6';

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

输出

$ k6 run --vus 500 --duration 1s script.js 

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

  execution: local
     script: script.js
     output: -

  scenarios: (100.00%) 1 scenario, 500 max VUs, 31s max duration (incl. graceful stop):
           * default: 500 looping VUs for 1s (gracefulStop: 30s)

running (17.7s), 000/500 VUs, 500 complete and 0 interrupted iterations
default ✓ [======================================] 500 VUs  1s

     data_received..................: 8.5 MB 481 kB/s
     data_sent......................: 219 kB 12 kB/s
     http_req_blocked...............: avg=2.65s    min=681.15ms med=2.46s    max=13.29s   p(90)=4.62s    p(95)=5.36s   
     http_req_connecting............: avg=339.8ms  min=315.61ms med=327.35ms max=1.32s    p(90)=346.11ms p(95)=349.95ms
     http_req_duration..............: avg=1.51s    min=318ms    med=1.08s    max=10.42s   p(90)=2.84s    p(95)=3.68s   
       { expected_response:true }...: avg=1.51s    min=318ms    med=1.08s    max=10.42s   p(90)=2.84s    p(95)=3.68s   
     http_req_failed................: 0.00%  ✓ 0         ✗ 500  
     http_req_receiving.............: avg=214.48ms min=27.91µs  med=82.99µs  max=4.04s    p(90)=699.7ms  p(95)=1.3s    
     http_req_sending...............: avg=36.37µs  min=12.5µs   med=34.49µs  max=106.12µs p(90)=53.32µs  p(95)=60.42µs 
     http_req_tls_handshaking.......: avg=2.28s    min=329.4ms  med=2.08s    max=12.96s   p(90)=4.29s    p(95)=4.98s   
     http_req_waiting...............: avg=1.3s     min=315.16ms med=997.61ms max=10.1s    p(90)=2.57s    p(95)=3.4s    
     http_reqs......................: 500    28.311116/s
     iteration_duration.............: avg=5.17s    min=2.03s    med=4.83s    max=17.56s   p(90)=7.98s    p(95)=8.84s   
     iterations.....................: 500    28.311116/s
     vus............................: 1      min=1       max=500
     vus_max........................: 500    min=500     max=500
j13ufse2

j13ufse22#

您可以使用ab命令。

ab -n 500 https://apihost/api/action
ab -n 500 -c 10 https://apihost/api/action
bkhjykvo

bkhjykvo3#

如果您真的想继续使用curl -请看一下GNU Parallel工具
然而,我建议使用专门的负载测试工具,原因如下:
1.每秒运行500个请求并不能告诉您全部情况,建议的方法是逐渐增加负载,以便您能够将增加的负载与响应时间、每秒命中数和其他指标关联起来
1.您将获得基本的通过/失败标准,即开箱即用的响应状态代码检查
1.您将获得开箱即用的统计表和图表
1.如果您以后需要扩展脚本(即从响应中提取一些内容以构建下一个请求或应用自定义通过/失败标准)
最先进的免费和开放源代码负载测试工具包括:

有关上述工具(包括功能比较)的更多信息:Open Source Load Testing Tools: Which One Should You Use?

相关问题