DMC DMC - 1 year ago 333
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

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();
} catch (Exception ex) {

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(
at org.apache.http.impl.conn.DefaultHttpRoutePlanner.determineRoute(
at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(
at org.apache.http.impl.client.DefaultRequestDirector.execute(
at org.apache.http.impl.client.AbstractHttpClient.doExecute(
at org.apache.http.impl.client.CloseableHttpClient.execute(
at org.apache.http.impl.client.CloseableHttpClient.execute(
at org.apache.http.impl.client.CloseableHttpClient.execute(
at org.apache.http.client.HttpClient$ Source)
at Script3$
at java.util.concurrent.Executors$ Source)
at Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$ Source)
at Source)

Answer Source

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


JSR223 AJAX Demo

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download