Run JMeter test case by a Java Stand-Alone Applica

2020-07-10 19:49发布

问题:

I'm trying to execute the JMeter test case using the following command. Do I have another way of executing the test cases without having JMeter installed locally? Here I have to provide JMeter HOME path to JMeterUtils.

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

    // Initialize Properties, logging, locale, etc.
    JMeterUtils.loadJMeterProperties("/path/to/your/jmeter/bin/jmeter.properties");
    JMeterUtils.setJMeterHome("/path/to/your/jmeter");
    JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
    JMeterUtils.initLocale();

    // Initialize JMeter SaveService
    SaveService.loadProperties();

    // Load existing .jmx Test Plan
    FileInputStream in = new FileInputStream("/path/to/your/jmeter/extras/Test.jmx");
    HashTree testPlanTree = SaveService.loadTree(in);
    in.close();

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

resorce https://www.blazemeter.com/blog/5-ways-launch-jmeter-test-without-using-jmeter-gui

回答1:

You don't actually need to have a complete JMeter installation in this home directory you set. Just the property files are enough. In your JMeter home directory (or sub directories) you can put any additional property file like saveservice.properties, user.properties and so on. These are normally reference by relative paths inside the jmeter.property file. That is why you need a home directory...

So no local installation is needed for running a test plan from java. You just need the property files to be present. By setting JMeterHome you just tell JMeter where to look for additional property files specified in the jmeter.properties file.

For a working project i would suggest looking into this:

https://bitbucket.org/blazemeter/jmeter-from-code/

Hope this helps.



回答2:

It's better to include if multiple users will be running this app on their local systems.I used something like this

String JmeterPropertiesFilePath = System.getProperty("user.dir").toString()
        + "/folder/apache-jmeter-5.0/bin/jmeter.properties";
String JmeterHomePath = System.getProperty("user.dir").toString()
        + "/folder/apache-jmeter-5.0/";