简体   繁体   English

如何使用Python每秒发送1k POST http请求?

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

I need to do stress test for my API. 我需要为我的API做压力测试。 I modified that example to send POST instead of HEAD, also I put some data to my request: 我修改了这个例子来发送POST而不是HEAD,我也把一些数据放到我的请求中:

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! 之后,性能下降到每秒5个请求! Also I tried all answers from that question and received the same result. 此外,我尝试了该问题的所有答案并收到了相同的结果。 Is it real to reached 1000 req/sec? 是真的达到1000 req / sec?

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. 在这里,我们正在制作10个子流程,每个子流程将生成100个请求。 So in a second, you will able to execute 1000 requests. 所以在一秒钟内,您将能够执行1000个请求。

import subprocess

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM