Search code examples
javamavenjmeterbeanshell

When Running Jmeter test from java its not using the properties


I have a test in JMeter, and with GUI it runs perfectly, but when I run it using my Java code (like from here: http://blazemeter.com/blog/5-ways-launch-jmeter-test-without-using-jmeter-gui), I can't retrieve the params from the properties. If I print them in the beanshell log, I can see them. But, when I'm trying to use this params in thread Group as number of users it is not working.

More info:

  1. I have a setUp thread that counts the rows of the csv file and inserting a value to a property so that in the next thread I will use this property to set the amount of users. (this logic is happening twice during the test).
  2. I'm using Maven dependencies of: ApacheJMeter_http V2.11 (2.13 creates a conflict with common-pools2 and d-haven)
  3. eclipse (if it matters)
  4. The goal is that the only parameter I will pass to the test run is the test file.
  5. The prop statement in the test looks like this ${__P(paramName)} also tried $(__property{paramName}).
  6. I have also created a property in the file itself instead of using in beanshell JMeterUtils.setProperty("paramName","5");

Adding the bean shell snippet:

import org.apache.jmeter.util.JMeterUtils;
import java.io.*;

BufferedReader br = new BufferedReader(new FileReader("C:\\res\\movieResultData.csv"));
String line;

int counter = 0;
while ((line = br.readLine()) != null) {
   counter++;
}
br.close();
JMeterUtils.setProperty("statsThreadNum",Integer.toString(counter-1));

My java Code:

    @Test
public void ttt() throws Exception {
    // JMeter Engine
    StandardJMeterEngine jmeter = new StandardJMeterEngine();

    // Initialize Properties, logging, locale, etc.
    JMeterUtils.loadJMeterProperties("C:\\apache-jmeter-2.12\\bin\\jmeter.properties");
    JMeterUtils.setJMeterHome("C:\\apache-jmeter-2.12");
    //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("C:\\apache-jmeter-2.12\\bin\\statsTests2.jmx");
    HashTree testPlanTree = SaveService.loadTree(in);
    in.close();

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

Solution

  • The correct syntax should be:

    ${__P(prop_name)}
    

    If you are running it through a java only implementation, be sure you pass the property file to your JVM as it starts up or use the following code snippit:

      JMeterUtils.loadJMeterProperties("/path/to/your/jmeter/bin/jmeter.properties");
    

    Are you using Apache Exec to start JMeter from java as an external process? If so, be sure you build your CMD line statement using the following options:

    -p, --propfile {argument}
                the jmeter property file to use
    -G, --globalproperty (argument)[=(value)]
                Define Global properties (sent to servers)
    

    It might be useful to show us a snippit of your Beanshell that you use to set the properties in SetUp.

    Edit: I believe the issue may be with how your Beanshell is accessing properties. I've never had to access the JMeterUtils class to manipulate variables or properties within a JMX.

    Beanshell Syntax for assigning a property:

    props.put("test_prop_name","prop_value");
    

    Beanshell syntax for getting a property:

    props.get("test_prop_name");
    

    Beanshell syntax for assigning a property to a variable:

    vars.put("test_var_name",props.get("test_prop"));
    

    This variable can then be referenced in a sampler like this:

    ${test_var_name}
    

    Or you should be able to accesss the property directly in a sampler like this:

    ${__P(test_prop_name)}
    

    Also, use the following POM if you wish to fix the Maven issues with JMeter 2.13:

    <dependencies>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_config</artifactId>
            <version>2.13</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>commons-math3</groupId>
                    <artifactId>commons-math3</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-pool2</groupId>
                    <artifactId>commons-pool2</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>2.13</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>commons-math3</groupId>
                    <artifactId>commons-math3</artifactId>
                </exclusion>
    
                <exclusion>
                    <groupId>commons-pool2</groupId>
                    <artifactId>commons-pool2</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.4.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.3</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>