如何使用程序(Java 代码)通过 运行 Jmeter 测试在 java 代码中设置 CSV 文件?
How to set CSV file in java code by running the Jmeter test using a program (Java Code)?
我之前曾问过如何在使用 java 程序 运行 进行 jmeter 测试后记录结果,然后我自己得到了这个问题,所以我将分享link 该问题的答案以供将来参考。所以这里是 link...
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 file :
注意: 我正在尝试通过上面分享的 Java 代码 运行 整个脚本,如果我 运行没有 CSV 文件,只有一个用户登录并设置线程数 = 3 然后脚本 运行 没问题。
只需从 CSV 数据集配置的 "Variable Names" 部分删除所有内容:
您的设置应该会按预期开始工作。鉴于您已将 "Ignore first line" 设置为 False
并且 CSV 文件的第一行是 header,而不是您不需要在那里设置任何变量名称的数据,JMeter 会这样做自动地。
您也不需要在代码中使用此 CSVDataSet declaration/configuration,因为:
- 配置不正确
- 它不会添加任何值,因为您没有将它添加到 Test Plan
我之前曾问过如何在使用 java 程序 运行 进行 jmeter 测试后记录结果,然后我自己得到了这个问题,所以我将分享link 该问题的答案以供将来参考。所以这里是 link...
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 file :
注意: 我正在尝试通过上面分享的 Java 代码 运行 整个脚本,如果我 运行没有 CSV 文件,只有一个用户登录并设置线程数 = 3 然后脚本 运行 没问题。
只需从 CSV 数据集配置的 "Variable Names" 部分删除所有内容:
您的设置应该会按预期开始工作。鉴于您已将 "Ignore first line" 设置为 False
并且 CSV 文件的第一行是 header,而不是您不需要在那里设置任何变量名称的数据,JMeter 会这样做自动地。
您也不需要在代码中使用此 CSVDataSet declaration/configuration,因为:
- 配置不正确
- 它不会添加任何值,因为您没有将它添加到 Test Plan