DMC DMC - 4 months ago 62
Ajax Question

parallel request using JSR223 Sampler (Jmeter)

I'm trying to use the following code (by Dmitri) in

JMeter 3.0
, however, I am unsuccessful in running it. my goal is to simulate parallel API calls.

Any idea why its failing? code:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; // necessary imports


List<String> urls = new ArrayList<String>(); // initialize array of URLs
Collections.addAll(urls,args); // read URLs from "Parameters" input and add them to array
ExecutorService pool = Executors.newFixedThreadPool(urls.size()); // initialize pool of Future Tasks with number of threads equal to size of URLs provided
for (String url : urls) { // for each URL from list
final String currentURL = url;
pool.submit(new Runnable() { // Sumbit a new thread which will execute GET request

@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient(); // Use Apache Commons HTTPClient to perform GET request
HttpGet get = new HttpGet(currentURL);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} catch (Exception ex) {
ex.printStackTrace();
}

}
});
}
pool.shutdown(); // shut down thread pool


Error thrown is (i did use 2 example hosts in parameter field jsr223 sampler):

java.lang.IllegalStateException: Target host is null
at org.apache.http.util.Asserts.notNull(Asserts.java:52)
at org.apache.http.impl.conn.DefaultHttpRoutePlanner.determineRoute(DefaultHttpRoutePlanner.java:99)
at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:763)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:382)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.apache.http.client.HttpClient$execute.call(Unknown Source)
at Script3$1.run(Script3.groovy:26)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Answer

The code looks ok

  1. Make sure you have groovy selected in the "Language" dropdown
  2. Make sure you provide at least 1 URL in "Parameters" section (including protocol, http or https). If you use > 1 url - separate them by spaces, i.e.

    http://url1/path http://url2/path2
    

Demo:

JSR223 AJAX Demo

Source: How to Load Test AJAX/XHR Enabled Sites With JMeter

Comments