Home > Back-end >  How to run 500 api requests in parallel at the same time
How to run 500 api requests in parallel at the same time

Time:08-17

Need to stress test an api and have a requirement for 500 API requests within a second. Is there command to test this?

Was using xargs but the responses timestamps were not all the same, changing every 10 requests or so.

CodePudding user response:

Full disclosure, I originally got most of this code from some tutorial on calculating QPS for model serving. Once I find the original source I can link you to that, but I can't seem to find it anymore. This should allow you to test 500 connections, and you can toggle the number pretty easily. Also, because it's asynchronous I don't think it will eat up all of your cores/memory.

import asyncio
import aiohttp
import time

async def fetch_page(session, url):
    async with session.get(url) as response:
        assert response.status == 200
        return await response.read()

async def main():
    num_connections = 500
    url = "YOUR URL HERE"
    results = await asyncio.gather(*[run_benchmark(url) for _ in range(num_connections)])
    print("Queries per second:", sum(results)) # Sum qps over all replicas.

async def run_benchmark(url):
    start = time.time()
    count = 0
    async with aiohttp.ClientSession(loop=loop) as session:
        while time.time() - start < 60: # Run test for one minute.
            count  = 1
            print(await fetch_page(session, url))
        return count / (time.time() - start) # Compute queries per second.

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

CodePudding user response:

A powerful yet simple option could be using k6, from the example at https://k6.io/docs/getting-started/running-k6/, simulating 500 VUs (virtual-users):

simple.js

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

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

output

$ 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

CodePudding user response:

you can use ab command.

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