2016-08-03 203 views
2

是否有可能獲得在JMeter中使用JSR223/groovy採樣器創建的API請求的實際響應時間?我有以下工作代碼,但是當我觀察我的聽衆時(響應內容實際上很好,有些json數據),沒有得到正確的響應時間。使用JSR223 + JMeter獲取響應時間

目標URL在採樣器的'參數'字段中給出(基於德米特里的例子)。此外,我在請求中添加了一個不記名標記,以便使用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 
+1

你的問題不清楚。你的意思是你希望得到'HttpResponse response = client.execute(get);'的響應時間而不是整個採樣器的響應時間?或者是其他東西? –

+0

是樣本的響應時間。我確實得到了一個有效的響應內容(json消息)。但我想知道請求花了多長時間,因此我需要知道響應時間。 對於我現在看到的,jsr223採樣器的響應時間大約是10-20ms左右,這是不正確的(對於此api請求應該在1秒以上)。 – DMC

+0

奇怪:我認爲採樣器會比單個操作更長。它可能與使用單獨的線程來運行代碼有關。基本上JMeter所做的主要假設是每個採樣器都是由單個線程執行的單個「操作」。而且你有多個線程並行處理多個URL到多個URL。所以它可能會變得怪異。我建議讓採樣器處理一件事,並控制線程組級 –

回答

3

你的樣品返回結束後, 的ThreadPoolExecutor的shutdown()方法啓動關閉,但不等待它。 嘗試用await Termination:pool.awaitTermination(60, TimeUnit.SECONDS)

+0

我的代碼如何看起來像?如果我替換pool.shutdown(); 'pool.awaitTermination(60,TimeUnit.SECONDS)'然後我得到一個響應消息:javax.script.ScriptException:groovy.lang.MissingPropertyException:沒有這樣的屬性:TimeUnit類:Script4 – DMC

+0

固定與導入java.util。 concurrent.TimeUnit,但是示例現在似乎永遠運行直到達到超時。然後顯示響應。響應/加載時間也是60秒 – DMC

+1

公頃對不起,我忘記了導入。你必須做關機然後等待。我不是很清楚,我的不好。 – ARA