2017-09-07 28 views
0

我必須從包含10000多條記錄的csv文件讀取數據。 我想在JMeter中使用這些數據來創建一個Web服務。如何訪問我的Java請求採樣器中的CSV數據集配置

我已經用硬編碼值寫了我的代碼。但我想讓它變得動態。

如何在我的自定義Java請求採樣器中訪問CSV數據集config ...? 如何訪問我的java請求採樣器中的CSV數據集配置中聲明的變量..?

這裏是我的全碼:

@Override 
public SampleResult runTest(JavaSamplerContext arg0) 
{ 
    SampleResult result = new SampleResult(); 
    boolean success = true; 

    byte arr[] = new byte[] {1,49,45,1,2,(byte)214,1,1,98,0,6,0,0,9,24,0,0,0,0,0,0,0,0,0,0,0,0,0,127,(byte)255,0,21,0,16,0,75,1,0,0,58,32,2,7,0,0,4,4,0,85,81,98,0,5,14,(byte)158,0,2,0,0,0,0,0,88,82,50,69,49,83,49,86,48,67,48,0,0,1,97,75,0,84,30,12,7,17,5,7,50,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,4,6,0,0,48,49,48,48,49,48,51,48,0,0,0,0,0,0,0,0,0,0,70,48,10,29,22,85,0,1,(byte)134,(byte)160,(byte)255,(byte)255,(byte)158,(byte)170,0,0,0,67,0,0,0,0,2,0,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,12,0,13,0,12,0,13,0,13,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,12,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,13,0,13,0,12,0,13,0,13,2,(byte)158,2,(byte)159,2,(byte)241,2,(byte)234,5,48,5,68,8,90,7,(byte)193,6,15,4,10,3,100,4,(byte)224,7,47,6,72,4,(byte)170,4,4,4,7,5,16,6,107,6,114,5,(byte)195,4,(byte)179,2,(byte)198,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,13,0,101,0,99,0,(byte)129,0,(byte)129,2,81,2,(byte)224,1,(byte)153,0,(byte)30,0,31,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,21,0,86,0,98,0,51,0,72,0,104,0,(byte)144,0,(byte)175,0,(byte)174,0,(byte)174,2,20,4,(byte)132,4,103,5,96,0,126,0,14,0,14,0,14,0,14,0,14,0,15,0,14,0,14,0,14,0,14,0,14,0,85,1,41,1,104,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,(byte)226,0,(byte)223,0,(byte)223,0,15,0,14,0,13,0,115,0,(byte) 223,(byte)162,40,38,85,115,101,114,78,97,109,101,61,101,82,101,103,38,85,115,101,114,80,97,115,115,119,111,114,100,61,97,98,99,49,50,51}; 

    try 
    { 
     URL obj = new URL(POST_URL); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
     con.setRequestMethod("POST"); 

     con.setDoOutput(true); 
     result.sampleStart(); 
     OutputStream os = con.getOutputStream(); 
     os.write(arr); 
     os.flush(); 
     os.close(); 
     result.sampleEnd(); 
     int responseCode = con.getResponseCode(); 
     System.out.println("POST Response Code :: " + responseCode); 

     if (responseCode == HttpURLConnection.HTTP_OK) 
     { //success 
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
      String inputLine; 
      StringBuffer response = new StringBuffer(); 
      while ((inputLine = in.readLine()) != null) 
      { 
       response.append(inputLine); 
      } 
      in.close(); 
      // print result 
      System.out.println(response.toString().getBytes()); 
     } 
     else 
     { 
      System.out.println("POST request not worked"); 
     } 

    } 
    catch(Exception E) 
    { 
    } 

    // 


    result.setSuccessful(success); 
    return result; 

} 

@Override 
public Arguments getDefaultParameters() 
{ 
    Arguments dp=new Arguments(); 
    return dp; 

} 

@Override 
public void setupTest(JavaSamplerContext context) {} 
@Override 
public void teardownTest(JavaSamplerContext context) { 
} 
+0

你可以添加你當前的代碼? – user7294900

+0

在http採樣器中,我們直接讀取在csv數據集配置中聲明的變量。但是,如何在使用自定義java請求採樣器時做到這一點 – Fazaikh

+0

字節數組中的十六進制數據字符串就是我想從文件發送的內容。 – Fazaikh

回答

1

通常情況下,你應該能夠訪問JMeter的變量,例如:

String myVar = JMeterContextService.getContext().getVariables().get("your_variable_name_here"); 

但是,如果你不想把它硬編碼你可能考慮將配置移動到Java請求採樣器GUI,如:

String valueFromCsv = ""; 
String defaultValue = "insert_jmeter_variable_here"; 

@Override 
public Arguments getDefaultParameters() { 
    Arguments dp = new Arguments(); 
    dp.addArgument("hexData", "insert_jmeter_variable_here"); 
    return dp; 
} 

@Override 
public void setupTest(JavaSamplerContext context) {   
    valueFromCsv = context.getParameter("hexData", defaultValue);   
} 

這樣你會abl e直接從JMeter GUI控制參數值。

參考文獻:

+0

setupTest()方法做什麼...? – Fazaikh

+0

dp(「hexData」,「insert_jmeter_variable_here」); 這不是一種方法,我想這是不可能的 – Fazaikh

+0

道歉,當然它應該是'dp.addArgument' –

相關問題