is it possible to get the actual response time of API requests made using the JSR223 (Groovy) sampler in JMeter ? I have the following code, but do not get proper response time when i watch my listeners. The URL is given in the 'parameter' field in the sampler (based on Dmitri's example).
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.concurrent.ExecutorService
import java.util.concurrent.Executors
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 Ruable() { // 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);
get.addHeader("authorization","Bearer ${AccessToken}"); // Add the access token into the header
get.addHeader("coection","keep-alive"); // Add keep-alive in header
HttpResponse response = client.execute(get); // HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
log.info("Response Status Code: " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
SampleResult.setResponseData(EntityUtils.toByteArray(entity));
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
});
}
pool.shutdown(); // shut down thread pool
