How to send 1k POST http requests per second with data using Python?

3.4k Views Asked by At

I need to do stress test for my API. I modified that example to send POST instead of HEAD, also I put some data to my request:

count = int(sys.argv[1])
url = "http://api.example.com/"
test_id = datetime.now().strftime("%H%M%S%f")
data = []
for i in range(count):
    req_data = {
        'test_id': test_id,
        'param1': i,
        'param2': 'aaa',
        'param3': 'bbb',
    }
    data.append(req_data)

start_time = time.time()
res = grequests.map(grequests.post(url, data=data[i]) for i in range(count))
sending_time = time.time() - start_time
print(sending_time)

After that performance dropped to 5 request per second! Also I tried all answers from that question and received the same result. Is it real to reached 1000 req/sec?

1

There are 1 best solutions below

3
On

Yes, you can do this.

You can achieve this by creating multiple processes and threads.

Ex. Using subprocess

Here, we are making 10 subprocesses and each will make 100 requests. So in a second, you will able to execute 1000 requests.

import subprocess

for i in range(10):
    subprocess.Popen(["python","pathToPythonScript/stressTesting.py","100"])