2011-11-08 26 views
1

我正在測試具有「加載文檔」功能的服務。我需要爲每個請求發送一個獨特的文檔。在HTTP請求採樣器配置菜單中,我發現我可以發送一個文檔以及我的請求。但是,我不知道如何隨每個請求發送不同的文檔。有沒有辦法讓JMeter稍微修改一個文檔,生成一個指定的文檔,或者甚至可能選擇一系列外部生成的文檔來提交請求?使用JMeter測試加載文檔功能

回答

3

您可以使用While控制器下的CSV數據集配置在循環中讀取和發送預先創建的測試文檔名稱。

這將是這樣的:

  1. 創建不同的測試文件與您發送請求的集合;
    可選:將創建文檔的路徑存儲爲jmeter變量 - 用於腳本;
  2. 創建測試文檔列表;
    您可以在BeanShell Sampler中使用代碼來執行此操作,如下所示;
  3. add控制器發送週期中的測試文檔;
    CSV數據集配置爲控制器作爲子項時 - 從列表中讀取測試文檔名稱。

詳細:

BeanShell Sampler

${__javaScript("${testFile}"!="<EOF>",)} - 耕種文件 While Controller

CSV Data Set Config

HTTP Request

結束閱讀列表

BeanShell取樣器代碼生成測試文件列表:

import java.text.*; 
import java.io.*; 
import java.util.*; 

String [] params = Parameters.split(","); 

String contentList = params[0]; 
String testDataDir = params[1]; 

File dir = new File(System.getProperty("user.dir") + File.separator + testDataDir); 
BufferedWriter out = null; 

try { 

    if (!dir.exists()) { 
     throw new Exception ("Directory " + dir.getName() + " not found."); 
    } 

    File contentFile = new File(System.getProperty("user.dir") + File.separator + contentList); 

    if (contentFile.exists()) { 
     contentFile.delete(); 
    } 

    FileWriter fw = new FileWriter(contentFile, true); 
    out = new BufferedWriter(fw); 

    System.out.println("\n--------------------------------------------------------------------------------------"); 
    System.out.println("CONTENT LIST:\n"); 

    if ((dir.exists()) && (dir.listFiles() != null) && (out != null)) { 
     for (File f : dir.listFiles()) { 
      if (contentFile.length() == 0) { 
       out.write(f.getName()); 
      } else { 
       out.write("\n" + f.getName()); 
      } 

      out.flush(); 

      System.out.println("Content " + f.getName() + " added to " + contentFile.getName() + "."); 
     } 
    } 

    System.out.println("--------------------------------------------------------------------------------------\n"); 
} 
catch (Exception ex) { 
    IsSuccess = false; 
    log.error(ex.getMessage()); 
    System.err.println(ex.getMessage()); 
} 
catch (Throwable thex) { 
    System.err.println(thex.getMessage()); 
} 
finally { 
    out.close(); 
}