2
votes

Ok so I have looked and could not find first, why I am getting this error and second, I couldn't find the answer through search.

So basically I am trying to run a test plan using JMeter. I essentially copy/paste the template for both running an existing .JMX file and creating a new Test Plan, all from using purely Java code.

The first code is from the web when running an existing .jmx file

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



    // Initialize Properties, logging, locale, etc.
    JMeterUtils.loadJMeterProperties("/Users/jmoore/Documents/apache-jmeter-2.11/bin/jmeter.properties");
    JMeterUtils.setJMeterHome("/Users/jmoore/Documents/apache-jmeter-2.11/bin/jmeter.sh");
    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("/Users/jmoore/dev/commerce/selenium/jmeter/src/test/testplans/test1.jmx");
    HashTree testPlanTree = SaveService.loadTree(in);
    in.close();

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

The second (below), is from trying to create my own Test Plan (sample from the web)

@Test
public void example() {

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

    //JMeter initialization (properties, log levels, locale, etc)

    JMeterUtils.loadJMeterProperties("/Users/jmoore/Documents/apache-jmeter-2.11/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("https:/www.google.com/");
    httpSampler.setPort(80);
    httpSampler.setPath("/");
    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();
}

First Problem (first code): For trying to run an existing .JMX file, I keep getting the error: "JMeter requires the saveservice properties file to continue"

But the funny thing is in my /bin, there is clearly a saveservice.properties file

Second Problem (second code): It runs fine, the only problem is I do not even see a standard output. When I did some research, (google and through here), it states to uncomment the section regarding the summariser which is located in the "jmeter.properties" file - Great and I did that. I uncommented the following:

summariser.out=true

So my question is why isn't there anything at least being printed out to the screen if it has been uncommented and set to 'true?' I prefer the second way rather than the first because I can customize it more to my needs.

Thank you

2

2 Answers

4
votes
  1. To work around JMeter requires the saveservice properties file to continue add the next line between StandardJMeterEngine initialization and JMeterUtils.loadJMeterProperties

    JMeterUtils.setJMeterHome("/Users/jmoore/Documents/apache-jmeter-2.11");
    
  2. To see summarizer output during the test add the next lines before jmeter.configure(testPlanTree); stanza

    Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");//$NON-NLS-1$
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }
    
    String logFile = "/path/to/results/file.jtl";
    ResultCollector logger = new ResultCollector(summer);
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);
    

See 5 Ways To Launch a JMeter Test without Using the JMeter GUI for more details on how to run existing .jmx files from Java code and creating a new JMeter test using JMeter Java API.

-1
votes

I faced this error because I have set the wrong jmeter Home as follows

JMeterUtils.setJMeterHome("C:\\apache-jmeter-2.12\\bin\\");

But it should be

JMeterUtils.setJMeterHome("C:\\apache-jmeter-2.12\\");