How to execute JMeter test case from Java code

前提是你 提交于 2021-01-29 22:27:26

问题


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


回答1:


  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);
    



回答2:


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));
  }


来源:https://stackoverflow.com/questions/25993210/how-to-execute-jmeter-test-case-from-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!