2
votes

Is it possible to get the actual response time of API requests made using the JSR223/groovy sampler in JMeter ? I have the following working code, but do not get proper response time when i watch my listeners (response content is actually fine, some json data).

The target URL is given in the 'parameter' field in the sampler (based on Dmitri's example). Furthermore i added a bearer token into header to use the OAuth access token when doing requests.

I did try to use a Transaction controller and include the JSR223 sampler within it, but this did not work in getting the response time (even not when creating a parent sample).

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 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);                      

                get.addHeader("authorization","Bearer ${AccessToken}");     // Add the access token into the header 
                get.addHeader("connection","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
1
your question is unclear. Do you mean you'd like to get the response time of HttpResponse response = client.execute(get); instead of response time of the entire sampler? or something else? - Kiril S.
yes the response time of the sample. i do get a valid response content (json message) for this api request. but i want to know how long the request took, therefore i need to know the response time. for what i see now, the response time of the jsr223 sampler is about 10-20ms or so, which is not correct (should be above 1> sec for this api request). - DMC
Strange: I would think sampler would be longer than single operation within it. It might have something to do with using separate threads to run your code. Basically main assumption JMeter makes is that each sampler is a single "operation" performed by a single thread. And you have multiple threads doing multiple things to several URLs on parallel. So it may get weird. I'd suggest getting sampler to process ONE thing, and controlling processing of all URLs on thread group level - Kiril S.
can you help me get the code modified to just 1 url, and not using array of urls? - DMC
Can you link to Dmitri's example? Are you trying to fetch several xhr requests in parallel? - RaGe

1 Answers

3
votes

Your sample returns before the thread executing your request is finished, the shutdown() method of ThreadPoolExecutor initiates the shutdown but does not wait for it . Try with awaitTermination: pool.awaitTermination(60, TimeUnit.SECONDS)