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