Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StandardJMeterEngine run method not triggering the HTTPSampler

Tags:

java

jmeter

I am trying to create a Java class that can make a call to a REST API using JMeter. I am doing this as I will be needing this to create a utility to do some kind of load testing.

I installed JMeter and using the GUI mode I was able to create and run the test that is able to hit my API. Then I took the sample code suggested on many websites and tried to run the same.

The code is mentioned below:

public class JMeterTestRunner {

public static void main(String[] argv) throws Exception {

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

    //JMeter initialization (properties, log levels, locale, etc)
    JMeterUtils.loadJMeterProperties("/Users/something/apache-jmeter-5.2.1/bin/jmeter.properties");
    JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
    JMeterUtils.initLocale();

    // JMeter Test Plan, basic all u JOrphan HashTree
    HashTree testPlanTree = new HashTree();

    // HTTP Sampler
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("localhost");
    httpSampler.setPort(8405);
    httpSampler.setPath("/some_path");
    httpSampler.setMethod("GET");

    // Loop Controller
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.addTestElement(httpSampler);
    loopController.setFirst(true);
    loopController.initialize();

    // Thread Group
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(loopController);

    // Test Plan
    TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");

    // Construct Test Plan from previously initialized elements
    testPlanTree.add("testPlan", testPlan);
    testPlanTree.add("loopController", loopController);
    testPlanTree.add("threadGroup", threadGroup);
    testPlanTree.add("httpSampler", httpSampler);

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

}

Now what happens is this runs fine, but the API endpoint doesn't get hit. I have debugged this and the configuration of the jmeter looks perfectly fine. The output that I get is mentioned below:

2020-04-10 01:11:36 [main] WARN  JMeterUtils:732 - Exception 'null' occurred when fetching boolean property:'server.exitaftertest', defaulting to: false
2020-04-10 01:11:36 [main] WARN  JMeterUtils:732 - Exception 'null' occurred when fetching boolean property:'jmeterengine.remote.system.exit', defaulting to: false
2020-04-10 01:11:36 [main] WARN  JMeterUtils:732 - Exception 'null' occurred when fetching boolean property:'jmeterengine.stopfail.system.exit', defaulting to: true
2020-04-10 01:11:36 [main] WARN  JMeterUtils:732 - Exception 'null' occurred when fetching boolean property:'jmeterengine.force.system.exit', defaulting to: false
2020-04-10 01:11:36 [main] INFO  JMeterUtils:365 - Setting Locale to en_IN
2020-04-10 01:11:36 [main] INFO  HTTPSamplerBase:1465 - Parser for text/html is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser
2020-04-10 01:11:36 [main] INFO  HTTPSamplerBase:1465 - Parser for application/xhtml+xml is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser
2020-04-10 01:11:36 [main] INFO  HTTPSamplerBase:1465 - Parser for application/xml is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser
2020-04-10 01:11:36 [main] INFO  HTTPSamplerBase:1465 - Parser for text/xml is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser
2020-04-10 01:11:36 [main] INFO  HTTPSamplerBase:1465 - Parser for text/vnd.wap.wml is org.apache.jmeter.protocol.http.parser.RegexpHTMLParser
2020-04-10 01:11:36 [main] INFO  HTTPSamplerBase:1465 - Parser for text/css is org.apache.jmeter.protocol.http.parser.CssParser
2020-04-10 01:11:36 [main] INFO  HTTPJavaImpl:71 - Maximum connection retries = 0
2020-04-10 01:11:36 [main] INFO  StandardJMeterEngine:351 - Running the test!
2020-04-10 01:11:36 [main] INFO  SampleEvent:64 - List of sample_variables: []
2020-04-10 01:11:36 [main] INFO  SampleEvent:64 - List of sample_variables: []
2020-04-10 01:11:36 [main] INFO  CompoundVariable:66 - Note: Function class names must contain the string: '.functions.'
2020-04-10 01:11:36 [main] INFO  CompoundVariable:69 - Note: Function class names must not contain the string: '.gui.'
2020-04-10 01:11:36 [main] WARN  CompoundVariable:83 - Did not find any functions
2020-04-10 01:11:36 [main] INFO  StandardJMeterEngine:450 - Starting ThreadGroup: 1 : 
2020-04-10 01:11:36 [main] INFO  StandardJMeterEngine:510 - Starting 1 threads for group .
2020-04-10 01:11:36 [main] INFO  StandardJMeterEngine:520 - Thread will continue on error
2020-04-10 01:11:36 [main] INFO  ThreadGroup:220 - Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2020-04-10 01:11:36 [main] INFO  ThreadGroup:246 - Started thread group number 1
2020-04-10 01:11:36 [main] INFO  StandardJMeterEngine:461 - All thread groups have been started
2020-04-10 01:11:36 [ 1-1] INFO  JMeterThread:711 - Thread started:  1-1
2020-04-10 01:11:36 [ 1-1] INFO  JMeterThread:299 - Thread is done:  1-1
2020-04-10 01:11:36 [ 1-1] INFO  JMeterThread:328 - Thread finished:  1-1
2020-04-10 01:11:36 [main] INFO  StandardJMeterEngine:214 - Notifying test listeners of end of test
2020-04-10 01:11:36 [main] INFO  FileServer:87 - Default base='/Users/somepath'

I have done my share of googling and tried many suggestions mentioned on various links such as : JMeter API Code to run HttpSampler doesnt work

But this doesn't seem to address/resolve my issue.

I am new to this Jmeter thing so any help will be most welcome. Thanks in advance.

like image 472
Vikas Kumar Avatar asked Oct 17 '25 11:10

Vikas Kumar


1 Answers

  1. Your way of adding the Test Elements to the Test plan is not correct, you need to change it to the following:

    testPlanTree.add(testPlan);
    HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
    threadGroupHashTree.add(httpSampler);
    
  2. You don't seem to store your results anywhere, it would be good to save them into .jtl results file for later analysis like:

    Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }
    
    String logFile = "/Users/something/apache-jmeter-5.2.1/bin/result.jtl";
    ResultCollector logger = new ResultCollector(summer);
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);
    
  3. Having the application under test and the JMeter on the same machine is not the best idea as you won't get reliable test results, consider running JMeter from another host. It would be also a good idea to monitor the essential OS health metrics like CPU, RAM, Network and Disk IO, etc. on both JMeter and system under test sides, it can be done using JMeter PerfMon Plugin

Full code just in case:

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

    //JMeter initialization (properties, log levels, locale, etc)
    JMeterUtils.loadJMeterProperties("/Users/something/apache-jmeter-5.2.1/bin/jmeter.properties");
    JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
    JMeterUtils.initLocale();

    // JMeter Test Plan, basic all u JOrphan HashTree
    HashTree testPlanTree = new HashTree();

    // HTTP Sampler
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("localhost");
    httpSampler.setPort(8405);
    httpSampler.setPath("/some_path");
    httpSampler.setMethod("GET");
    httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
    httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

    // Loop Controller
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.addTestElement(httpSampler);
    loopController.setFirst(true);
    loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
    loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
    loopController.initialize();


    // Thread Group
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(loopController);
    threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
    threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

    // Test Plan
    TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
    testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
    testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
    testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

    SaveService.saveTree(testPlanTree, new FileOutputStream("/Users/something/apache-jmeter-5.2.1/bin/test.jmx"));

    Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }

    String logFile = "/Users/something/apache-jmeter-5.2.1/bin/result.jtl";
    ResultCollector logger = new ResultCollector(summer);
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);

    // Construct Test Plan from previously initialized elements
    testPlanTree.add(testPlan);
    HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
    threadGroupHashTree.add(httpSampler);

    // Run Test Plan
    jmeter.configure(testPlanTree);
    jmeter.run();
}
like image 107
Dmitri T Avatar answered Oct 20 '25 03:10

Dmitri T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!