使用JSR223采样器(Jmeter)的并行请求

时间:2016-08-01 10:17:11

标签: ajax api parallel-processing jmeter

我正在尝试在JMeter 3.0中使用以下代码(由Dmitri提供),但是,我没有成功运行它。我的目标是模拟并行API调用

知道它失败的原因吗?代码:

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.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; // necessary imports


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);
     HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
           } catch (Exception ex) {
               ex.printStackTrace();
           }

       }
   });
}
pool.shutdown(); // shut down thread pool

引发的错误是(我在参数字段jsr223 sampler中使用了2个示例主机):

java.lang.IllegalStateException: Target host is null
        at org.apache.http.util.Asserts.notNull(Asserts.java:52)
        at org.apache.http.impl.conn.DefaultHttpRoutePlanner.determineRoute(DefaultHttpRoutePlanner.java:99)
        at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:763)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:382)
        at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
        at org.apache.http.client.HttpClient$execute.call(Unknown Source)
        at Script3$1.run(Script3.groovy:26)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

代码看起来不错

  1. 确保在&#34;语言&#34;中选择groovy下拉
  2. 确保在&#34;参数&#34;中提供至少1个网址部分(包括协议,httphttps)。如果你使用&gt; 1个网址 - 用空格分隔,即

    http://url1/path http://url2/path2
    
  3. 演示:

    JSR223 AJAX Demo

    来源:How to Load Test AJAX/XHR Enabled Sites With JMeter

答案 1 :(得分:0)

@DMC尝试添加:

pool.awaitTermination(60, TimeUnit.SECONDS); after pool.shurdown();

它会给你正确的响应时间