0

I know how to prepare a test plan and run it in JMeter using the Java API, there are quite a number of examples on how to do that. What's missing is a way to collect the results directly.

I know it's possible to save the results in a .jtl file but that would require me to open the file after saving it and parse it (depending on its format). I have seen the API provides quite a number of Result classes but I wasn't able to figure out how to use them. I have also tried debugging to try to figure out what classes were involved and try to understand an execution model.
Any help would be really appreciated

1 Answer 1

2

Right, I am not sure if that is the right answer, I think there's no right answer because it really depends on your needs. At least I managed to understand a bit more debugging a test execution.

Basically what I ended up doing was to extend the ResultCollector and add it to the instance of TestPlan. What the collector does is to store the events once received and print them at the end of the test (but at this point you can do whatever you want with it)

If you have better approaches please let me know (I guess a more generic approach would be to implement SampleListener and TestStateListener without using the specific implementation of the ResultCollector)

import java.util.LinkedList;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.samplers.SampleEvent;

public class RealtimeResultCollector extends ResultCollector{

    LinkedList<SampleEvent> collectedEvents = new LinkedList<>();

    /**
     * When a test result is received, store it internally
     *
     * @param event
     *            the sample event that was received
     */
    @Override
    public void sampleOccurred(SampleEvent event) {
        collectedEvents.add(event);
    }

    /**
     * When the test ends print the response code for all the events collected
     *
     * @param host
     *            the host where the test was running from
     */
    @Override
    public void testEnded(String host) {
        for(SampleEvent e: collectedEvents){
            System.out.println("TEST_RESULT: Response code = " + e.getResult().getResponseCode()); // or do whatever you want ...
         }
    }
}

And in the code of the main or wherever you have created your test plan

...
HashTree ht = new HashTree();

...

TestPlan tp = new TestPlan("MyPlan");

RealtimeResultCollector rrc = new RealtimeResultCollector();

// after a lot of confugration, before executing the test plan ...

ht.add(tp);
ht.add(ht.getArray()[0], rtc);

For the details about the code above you can find a number of examples on zgrepcode.com

Sign up to request clarification or add additional context in comments.

Comments

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.