I want to stress test my service endpoint, making http POST requests.
I want the solution to be able to either:
- Make a certain number of requests, like 100K
- Keep running for x number of minutes
So far I created my solution using an executorservice like:
int MAX_THREADS = 20;
int MAX_REQUESTS = 1000;
ExecutorService es = Executors.newFixedThreadPool(MAX_THREADS);
for(int x = 0; x < MAX_REQUESTS; x++) {
es.execute(new MyTask());
}
es.shutdown();
try {
es.awaitTermination(..);
}
catch(...) {
}
Where MyTask implemented Runnable, and it makes the HttpPost using HttpClient (apache).
So this creates a set pool of threads, and then calls the task x number of times.
How can I modify this to also be able to run for a specified number of minutes?
For me to get start/end time statistics, I’m going to have to use futures so each thread and return back how long it took to run correct?
Also, in my runnable task, I am creating a new instance of the HttpClient object, and loading a file that I send with the post. I’m guessing this is going to slow down my benchmark, is there a way to re-use the httpclient with everything setup except for the actual call to make the request?
Before anyone mentions it, I know there are tools like jmeter etc. that does this already, but I want to both learn how this works and I will be doing some custom things later on that I want full control over.
Why make your own if you can use Apache JMeter? You can make a simple plugin to suit your applications needs and then have all the load generating possibilities of JMeter at your fingertips. Including running with intervals and such.