开发者

Can only send 10 HTTP POST requests/second?

开发者 https://www.devze.com 2023-04-13 01:11 出处:网络
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.

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 sens开发者_如何学Pythone.


Use the Threads, Luke!

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.


Why not execute each post in a separate thread? the instruction:

httpClient.executeMethod(method);

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号