drpogue
drpogue

Reputation: 133

How to send parallel POST requests Java

I am currently trying to load test a server. Due to issues outside of my control, I cannot use jmeter for my testing. What would be the best way to create a set of threads and have each one send a post at the same moment, or as close as I can get to it? My current implementation creates a number of threads with a for loop before running the method which sends to POST to the server. But this isn't concurrent at all.

Upvotes: 0

Views: 2065

Answers (3)

searchengine27
searchengine27

Reputation: 1471

Id say you would want to do this in a cluster, and not in multiple threads on the same machine unless you have a machine with several network interface cards (NICs). Your bottleneck isnt the cores, its your NIC. Your NIC will still empty its buffer sequentially.

What I would recommend is having a cluster of as many machines as you can. On each machine, have a couple threads (just to ensure the NIC's buffer stays full). Don't bother synchronizing the cluster to send requests at the same time, because the round trip time (RTT) will take too long and it will void your load testing. Just have each Thread hammer away...statistically speaking, you're bound to get a couple POST requests at the same time, provided your cluster is sufficiently large.

Upvotes: 3

Jose Martinez
Jose Martinez

Reputation: 11992

Assuming yhis question is just about synchronizing your threads, and not creating/running threads, then I recommend a CountDownLatch. The example in the link is great.

Upvotes: 1

MadConan
MadConan

Reputation: 3767

I would definitely recommend using some load testing application framework. There are many to choose from.

With that said, you could use something like this to start with. This would be where I would start from. You would need to implement the performRequest method. This is not very robust, to say the least. There is no logging, no reporting no error handling -- all of which you get "for free" (for certain definitions of "free") with a load testing application.

public class HttpClient {
    enum RequestType{
        POST,GET
    }

    private RequestType type;
    private String url;

    public HttpClient(RequestType rt, String url){
        type = rt;
        this.url = url;
    }

    public void performRequest(){
       // your code here.
    }

    public static void main(String[] args){
        List<HttpClient> clients = new ArrayList<>();
        int clientCount = Integer.parseInt(args[0]);
        String url = args[1];
        for(int i=0; i<clientCount; i++){
            clients.add(new HttpClient(RequestType.POST,url));
        }
        clients.parallelStream().forEach(HttpClient::performRequest);
    }
}

Some notable improvements you might want to make

  • The default thread pool is very small. It's equal to the number of CPU cores on the system. Creating your own ForkJoinPool instance will give you greater control.
  • Moving to a separate class for launching and parsing args would be much better.

Upvotes: 0

Related Questions