如何在不编辑 jmx 文件的情况下通过 java 代码运行 jmeter 时传递自定义属性 [英] How to pass custom property when running jmeter via java code without editing the jmx file

查看:10
本文介绍了如何在不编辑 jmx 文件的情况下通过 java 代码运行 jmeter 时传递自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽力了,但我没有找到解决方案的运气.基本上,我需要在通过 java 代码运行 Jmeter 时传递自定义属性,而无需在 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

  • 通过 Java 代码调用 System.setProperty() 方法

    System.setProperty("foo", "bar");
    

  • 在所有情况下,您都应该能够访问像 ${__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)

    更多信息:

    这篇关于如何在不编辑 jmx 文件的情况下通过 java 代码运行 jmeter 时传递自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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