是否可以获得使用 JMeter 中的 JSR223/groovy 采样器发出的 API 请求的实际响应时间?我有以下工作代码,但是当我观看我的听众时没有得到适当的响应时间(响应内容实际上很好,一些 json 数据)。
目标 URL 在采样器的“参数”字段中给出(基于 Dmitri 的示例)。此外,我在标头中添加了一个不记名令牌,以便在执行请求时使用 OAuth 访问令牌。
我确实尝试使用事务控制器并在其中包含 JSR223 采样器,但这在获取响应时间方面不起作用(即使在创建父示例时也不起作用)。
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