使用 JSR223 + JMeter 获取响应时间

getting response times using JSR223 + JMeter

是否可以获取使用 JMeter 中的 JSR223/groovy 采样器发出的 API 请求的实际响应时间?我有以下工作代码,但是当我观看我的听众时没有得到正确的响应时间(响应内容实际上很好,一些 json 数据)。

目标 URL 在采样器的 'parameter' 字段中给出(基于 Dmitri 的示例)。此外,我在 header 中添加了一个承载令牌,以便在执行请求时使用 OAuth 访问令牌。

我确实尝试使用事务控制器并在其中包含 JSR223 采样器,但这在获取响应时间方面不起作用(即使在创建 parent 示例时也是如此)。

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

您的示例 returns 在执行您的请求的线程完成之前, ThreadPoolExecutor 的 shutdown() 方法启动关闭但不等待它。 尝试使用 awaitTermination:pool.awaitTermination(60, TimeUnit.SECONDS)