0

How do I run a JMeter test case from Java code?

I have followed the example Here from Blazemeter.com

My code is as follows:

public class BasicSampler {

public static void main(String[] argv) throws Exception {
    // JMeter Engine
    StandardJMeterEngine jmeter = new StandardJMeterEngine();

    // Initialize Properties, logging, locale, etc.
    JMeterUtils.loadJMeterProperties("/home/stone/Workbench/automated-testing/apache-jmeter-2.11/bin/jmeter.properties");
    JMeterUtils.setJMeterHome("/home/stone/Workbench/automated-testing/apache-jmeter-2.11");
    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("/home/stone/Workbench/automated-testing/apache-jmeter-2.11/bin/examples/CSVSample.jmx");
    HashTree testPlanTree = SaveService.loadTree(in);
    in.close();

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

}

but I keep getting the following messages in the console and my test never executes.

INFO 2014-09-23 12:04:40.492 [jmeter.e] (): Listeners will be started after enabling running version INFO 2014-09-23 12:04:40.511 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false

I have also tried uncommented jmeterengine.startlistenerslater=false from jmeter.properties file

2 Answers 2

2
  1. How do you know that your "test never executes"?
  2. What is in jmeter.log file (it should be in the root of your project). Or alternatively comment JMeterUtils.initLogging() line to see the full output in STDOUT
  3. Have you changed relative path CSVSample_user.csv in "Get user details" CSV Data Set Config as it may resolve into a different location as it recommended in Using CSV DATA SET CONFIG
  4. Is CSVSample.jtl file generated anywhere (again it should be in the root of your project by default)? What is in it?

The code looks good and I'm pretty sure that the problem is with the path to CSVSample_user.csv file and you have something like java.io.FileNotFoundException in your log. Please double check that CSVSample.jmx file contains valid full path to CSVSample_user.csv.

UPDATE TO ANSWER QUESTIONS IN COMMENTS

  1. jmeter.log file should be under your Eclipse workspace folder by default
  2. Looking into CSVSample.jmx there is a View Resulst in Table listener which is configured to store results under ~/CSVSample.jtl
  3. If you want to see summarizer messages and "classic" .jtl reporting add next few lines before jmeter.configure(testPlanTree); stanza

    Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }
    
    String logFile = "/path/to/jtl/results/file.jtl";
    ResultCollector logger = new ResultCollector(summer);
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it was as you suspected,java.io.FileNotFoundException it was looking for CSV files in Eclipse's workspace, project folder. the problem is that the jmeter.log is not created, I was able to see the log once commented out JMeterUtils.initLogging() line.
How can I ensure that the jmeter.log is created? Where are the results stored? I cannot see a .jtl file
1

Try using library - https://github.com/abstracta/jmeter-java-dsl.

It supports implementing JMeter test as java code.

Below example shows how to implement and execute test for REST API. Same approach could be applied to other type of tests as well.

  @Test
  public void testPerformance() throws IOException {
    TestPlanStats stats = testPlan(
      threadGroup(2, 10,
        httpSampler("http://my.service")
          .post("{\"name\": \"test\"}", Type.APPLICATION_JSON)
      ),
      //this is just to log details of each request stats
      jtlWriter("test" + Instant.now().toString().replace(":", "-") + ".jtl")
    ).run();
    assertThat(stats.overall().elapsedTimePercentile99()).isLessThan(Duration.ofSeconds(5));
  }

1 Comment

Please add some explanation for better understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.