每秒只能发送 10 个 HTTP POST 请求?
这似乎是一个奇怪的问题,但我试图在几秒的时间间隔内发送尽可能多的 HTTP POST 请求。
我想知道是否有人有任何建议,因为我目前每秒只能完成大约 6-9 个请求,这似乎相当低。我的代码如下 - 我正在使用 Apache Commons HTTP 库:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
long start = System.currentTimeMillis();
long end = start + 4000;
int count = 0;
while (System.currentTimeMillis() < end)
{
count++;
httpClient.executeMethod(method);
}
System.out.println((double)count/4 + " reqs / sec");
post 方法预先创建一次:
String body= getBodyString();
PostMethod method = new PostMethod(Url);
method.setRequestEntity( new StringRequestEntity(body));
method.setRequestHeader(...etc)
我猜测 httpclient 正在等待响应,存在某种固有的顺序行为?就我而言,我不关心响应,因此想必有一种方法可以提高呼叫率。也许我可以在发送请求的同时准备下一个请求等。还有一个更高效、更快速的库吗?
我对这种类型的代码很陌生,所以如果这个问题没有多大意义,我深表歉意。
This might seem like a strange problem, but I am trying to send as many HTTP POST requests as possible within a several second interval.
I am wondering if anyone has any advice as I am currently only achieving around 6-9 requests a second which seems fairly low. My code is as follows - I am using the Apache Commons HTTP library:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
long start = System.currentTimeMillis();
long end = start + 4000;
int count = 0;
while (System.currentTimeMillis() < end)
{
count++;
httpClient.executeMethod(method);
}
System.out.println((double)count/4 + " reqs / sec");
The post method is created once beforehand:
String body= getBodyString();
PostMethod method = new PostMethod(Url);
method.setRequestEntity( new StringRequestEntity(body));
method.setRequestHeader(...etc)
I am guessing that there is some sort of inherent sequential behavior whereby the httpclient is waiting for the response? In my case, I do not care about the response so presumably there is a way to improve the call rate. Perhaps I can be preparing the next request whilst one is being sent etc. Also could there be a more efficient and speedy library?
I'm new to this type of code so I apologize if this question doesn't make much sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的客户端受到服务器平均响应时间的限制。如果服务器在 100 毫秒内响应,那么每秒的请求数永远不会超过 10 个。
但是,如果您同时发送 10 个请求,并且服务器能够以相同的响应时间同时处理这些请求,那么您会立即达到 100 个请求/秒。
如果您打算对应用程序进行压力测试,可以使用一些不错的免费工具,例如 JMeter 或 ab。如果您确实在向其他人的网站发送垃圾邮件:您真可耻。他们的网络基础设施很可能很快就会切断您的联系。
Your client is limited by the average response time of the server. If the server responds withing 100 milliseconds you will never exceed 10 requests per second.
However if you send 10 requests at the same time and the server is capable of handling them simultaneously with the same response time, you've immediately reached 100 request/second.
If you aim to stress-test your application, there are some nice free tools like JMeter or ab. If you are literally spamming someone else's site: shame on you. And most likely their network infrastructure will cut you off soon.
为什么不在单独的线程中执行每个帖子?指令:
需要时间,如果您正在寻找增加 POST 的数量,我会使用一组线程来请求(线程池)
Why not execute each post in a separate thread? the instruction:
takes its time, and if you are looking for increasing the number of POST, I'd use a set of threads for requests (a pool of threads)