2017-04-04 83 views
0

我是Spring和JMeter的新手,我試圖用JMeter測試我的服務的性能。 站點在服務器上運行,它接受兩個參數(用戶名和值)。我想用CSV文件中的數據填充這兩個參數。如何使用CSV文件將參數填充到JMeter中的HttpSampler?

我有下面的類:

public class JMeter { 

    public static Logger logger = LoggerFactory.getLogger(JMeter.class); 

    public static void main(String[] argv) throws Exception { 
    //JMeter Engine 
    StandardJMeterEngine standardJMeterEngine = new StandardJMeterEngine(); 

    //JMeter initialization (properties, log levels, locale, etc) 
    JMeterUtils.loadJMeterProperties("target/jmeter/bin/jmeter.properties"); 
    JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level 
    JMeterUtils.initLocale(); 

    // JMeter Test Plan, basic all u JOrphan HashTree 
    HashTree hashTree = new HashTree(); 

    String csvFilePath = "src/test/jmeter/resources/userdata.csv"; 
    CSVDataSet csvDataSet = createAndConfigureCsvDataSet(csvFilePath); 
    HTTPSampler httpSampler = createAndConfigureHttpSampler(); 
    LoopController loopController = createAndConfigureLoopController(httpSampler); 
    ThreadGroup threadGroup = createAndConfigureThreadGroup(loopController); 
    TestPlan testPlan = new TestPlan("HttpTest"); 

    // Construct Test Plan from previously initialized elements 
    hashTree.add("testPlan", testPlan); 
    hashTree.add("loopController", loopController); 
    hashTree.add("threadGroup", threadGroup); 
    hashTree.add("httpSampler", httpSampler); 
    hashTree.add("csvDataSet", csvDataSet); 

    // Run Test Plan 
    standardJMeterEngine.configure(hashTree); 
    standardJMeterEngine.run(); 
    } 

    private static CSVDataSet createAndConfigureCsvDataSet(String csvFilePath) { 
    // CSV Data Set 
    File file = new File(csvFilePath); 
    if (file.exists()) { 
     logger.debug("CSV-File found: {}", file.getAbsoluteFile()); 
    } else { 
     logger.debug("CSV-File NOT found: {}", file.getAbsoluteFile()); 
    } 
    CSVDataSet csvDataSet = new CSVDataSet(); 
    csvDataSet.setFilename(csvFilePath); 
    csvDataSet.setVariableNames("USERNAME,VALUE"); 
    csvDataSet.setDelimiter(";"); 
    csvDataSet.setQuotedData(false); 
    csvDataSet.setRecycle(true); 
    csvDataSet.setStopThread(false); 
    return csvDataSet; 
    } 

    private static LoopController createAndConfigureLoopController(HTTPSampler httpSampler) { 
    LoopController loopController = new LoopController(); 
    loopController.setLoops(1); 
    loopController.addTestElement(httpSampler); 
    loopController.setFirst(true); 
    loopController.initialize(); 
    return loopController; 
    } 

    private static HTTPSampler createAndConfigureHttpSampler() { 
    HTTPSampler httpSampler = new HTTPSampler(); 
    httpSampler.setDomain("localhost"); 
    httpSampler.setPort(8080); 
    httpSampler.setPath("/userinfo"); 
    httpSampler.setMethod("GET"); 
    httpSampler.setContentEncoding("UTF-8"); 
    httpSampler.setFollowRedirects(true); 
    httpSampler.setUseKeepAlive(true); 
    httpSampler.addArgument("username", "USERNAME"); 
    httpSampler.addArgument("value", "VALUE"); 
    return httpSampler; 
    } 

    private static ThreadGroup createAndConfigureThreadGroup(LoopController loopController) { 
    ThreadGroup threadGroup = new ThreadGroup(); 
    threadGroup.setNumThreads(20); 
    threadGroup.setRampUp(1); 
    threadGroup.setSamplerController(loopController); 
    threadGroup.initialize(); 
    return threadGroup; 
    } 
} 

但我無法弄清楚如何把數據從我csvDataSet到我httpSampler。

回答

1

要將變量CSV數據集捕獲值轉換爲,請使用${variable_name}模式。

因此,在你的情況下,"${USERNAME}""${VALUE}"

相關問題