如何通过使用程序(Java代码)运行Jmeter测试来在Java代码中设置CSV文件? [英] How to set CSV file in java code by running the Jmeter test using a program (Java Code)?

查看:310
本文介绍了如何通过使用程序(Java代码)运行Jmeter测试来在Java代码中设置CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过一个问题,即使用Java程序运行jmeter测试后如何记录结果,然后我自己获得了这个信息,因此我将与答案共享该问题的链接,以供将来参考.因此,这里是链接... 使用程序(JAVA代码)运行Jmeter测试后,如何保存结果集?

I have previously ask the question that how to log the results after running the jmeter test using a java program and then I have got this by myself so I am going to share the link of that question with answer for future references. So here is the link... How can I save a result set after running the Jmeter Test using a program (JAVA CODE)?

但是现在我对此还有另一个问题,如何设置CSV文件 在这里有多次登录,我已经尝试了一些代码,但是没有 发生了我想要的.有人可以帮我这个忙吗?和 请参阅以下我尝试过的代码.

But now I have another question for this, How can I set a CSV file here with a multiple logins, I have tried some code but it not happened as I want. Can anyone please help me out from this ? And please see the following code that I have tried.

package com.solitera.automation.controller;

import org.apache.jmeter.engine.StandardJMeterEngine;  
import org.apache.jmeter.reporters.ResultCollector;  
import org.apache.jmeter.reporters.Summariser;  
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;  
import org.apache.jorphan.collections.HashTree;  

import java.io.File;  
import java.io.FileInputStream;  

public class JMeterFromExistingJMX {  

  public static void main(String[] argv) throws Exception {  

    //Set jmeter home for the jmeter utils to load  
    String jmeterHomelocation = "D:/apache-jmeter-5.1.1";  
    String jmeterPropertieslocation = jmeterHomelocation + "/bin/jmeter.properties";  

    // JMeter Engine  
    StandardJMeterEngine jmeter = new StandardJMeterEngine();  


    // Initialize Properties, logging, locale, etc.  
    JMeterUtils.loadJMeterProperties(new File(jmeterPropertieslocation).getPath());  
    JMeterUtils.setJMeterHome(new File(jmeterHomelocation).getPath());  
    // you can comment this line out to see extra log messages of i.e. DEBUG level  
    JMeterUtils.initLogging();  
    JMeterUtils.initLocale();  

    // Initialize JMeter SaveService  
    SaveService.loadProperties();  

    HashTree testPlanTree = SaveService.loadTree(new File("D:/apache-jmeter-5.1.1/extras/slt_auto_test_java_blaze_script.jmx"));

    Summariser summer = null;  
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");  

    if (summariserName.length() > 0) {  
      summer = new Summariser(summariserName);  
    }  


    CSVDataSet csvDataSet = new CSVDataSet();
    csvDataSet.setName("CSV Data Set Config");
    csvDataSet.setProperty("delimiter",",password,submitLogin,userName");
    csvDataSet.setProperty("filename", "D:/apache-jmeter-5.1.1/extras/CSVData.csv");
    csvDataSet.setProperty("ignoreFirstLine", false);
    csvDataSet.setProperty("quotedData", false);
    csvDataSet.setProperty("recycle", true);
    csvDataSet.setProperty("shareMode", "shareMode.all");
    csvDataSet.setProperty("stopThread", false);
    csvDataSet.setProperty("variableNames", "foo");
    csvDataSet.setProperty(TestElement.TEST_CLASS, csvDataSet.getClass().getName());
    csvDataSet.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

    String logFile = "D:/apache-jmeter-5.1.1/extras/resultss.xml";
    ResultCollector logger = new ResultCollector(summer);  
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);  

    // Run JMeter Test  
    jmeter.configure(testPlanTree);  
    jmeter.run();  
  }  
}

下面是我使用Blazemeter录制脚本并将其添加到Jmeter GUI的图像,请参阅相同的图片以了解我的TestPlan实际外观的更多信息.

CSVData.csv文件:

CSVData.csv file :

注意:我要尝试通过上面共享的Java代码运行整个脚本,如果我仅使用一个用户登录名就没有CSV文件运行,并且将线程数设置为3然后脚本运行正常.

NOTE : This whole script I am trying to run through the Java code that I have shared above, If I run without CSV file with only one user login and set the No. of threads = 3 then the script runs fine.

推荐答案

只需从CSV数据集配置的变量名称"部分中删除所有内容:

Just Remove everything from the "Variable Names" section of the CSV Data Set Config:

,您的设置应会按预期开始工作.假设您将忽略第一行"设置为False,并且CSV文件的第一行是标头,而不是您不需要在此处设置任何变量名的数据,那么JMeter将自动执行此操作.

and your setup should start working as expected. Given you have "Ignore first line" set to False and the first line of your CSV file is the header, not the data you don't need to set any variable names there, JMeter will do it automatically.

您也不需要此 CSVDataSet 代码中的声明/配置,因为:

You also don't need this CSVDataSet declaration/configuration in the code because:

  1. 配置不正确
  2. 它没有添加任何值,因为您没有将其添加到测试计划

更多信息:使用CSV数据集配置

这篇关于如何通过使用程序(Java代码)运行Jmeter测试来在Java代码中设置CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆