通过Java代码运行jmeter时如何在不编辑jmx文件的情况下传递自定义属性 [英] How to pass custom property when running jmeter via java code without editing the jmx file
问题描述
我已尽力而为,但我没有运气寻找解决方案.基本上,我需要通过java代码运行Jmeter时传递 custom property 而不在jmx文件中添加任何内容(就像通过命令行运行时一样).我有一些尝试过的jmeter属性函数(StandardJmeterEngine setProperty,JmeterUtils setProperty),但仍然无济于事.我已经看到了一些解决方案,例如将属性文件传递给代码,但是似乎未读取属性文件.
I have tried my best but I had no luck in looking for a solution. Basically I needed to pass custom property when running Jmeter via java code without adding anything in the jmx file (just like when running via command line). I have some tried jmeter property functions (StandardJmeterEngine setProperty, JmeterUtils setProperty) but still no avail. I have seen some solutions like passing the property file to the code but it seems that the property file is not being read.
顺便说一句,最简单的方法是通过Java代码与属性一起创建jmx文件,但是运行现有jmx文件时需要传递该属性.
By the way, the easiest way to do this is to create the jmx file along with the properties via java code, but I am required to pass the property when running an existing jmx file.
public class TestRunJmxJava {
@Test
public static void executeScript() throws IOException, BiffException, JMeterEngineException {
String slash = System.getProperty("file.separator");
StandardJMeterEngine jmeter = new StandardJMeterEngine();
String jmeterPath = "C:"+slash+"jmeter"+slash+"bin"+slash+"jmeter.properties";
String uPath = "C:"+slash+"jmeter"+slash+"bin"+slash+"jd.properties";
System.out.println(jmeterPath);
JMeterUtils.setJMeterHome("C:\\jmeter");
JMeterUtils.loadJMeterProperties(jmeterPath);
JMeterUtils.loadProperties(uPath);
JMeterUtils.getSearchPaths();
JMeterUtils.initLogging();
JMeterUtils.initLocale();
SaveService.loadProperties();
File in = new File(System.getProperty("user.dir")+slash+"jmxfolder"+slash+"TestJmeterRun.jmx");
HashTree testPlanTree = SaveService.loadTree(in);
jmeter.configure(testPlanTree);
FileServer.getFileServer().setBasedir(System.getProperty("user.dir")+slash+"jmxfolder");
System.out.println(FileServer.getFileServer().getBaseDir());
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");//$NON-NLS-1$
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
String logFile = "C:\\Users\\JD\\Desktop\\sample2.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
jmeter.run();
jmeter.exit();
}
}
推荐答案
您可以使用普通" Java系统属性为此,可以通过不同的方式进行设置:
You can use "normal" Java System Properties for this, they can be set in different ways:
- 通过 system.properties 文件(位于JMeter安装的"bin"文件夹中
-
通过 -D命令行参数传递给Java可执行文件
- Via system.properties file (lives in the "bin" folder of your JMeter installation
Via -D command-line argument passed to Java executable
java -Dfoo=bar -jar /path/to/your/test.jar
在所有情况下,您都应该能够访问${__P(foo,)}
之类的属性值(确保在项目类路径中包含ApacheJMeter_functions.jar
)
In all cases you should be able to access the property value like ${__P(foo,)}
(make sure you include ApacheJMeter_functions.jar
in your project classpath)
更多信息:
- Configuring JMeter
- Apache JMeter Properties Customization Guide
这篇关于通过Java代码运行jmeter时如何在不编辑jmx文件的情况下传递自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!