How parse a jmeter jmx element to a object

Viewed 236

I want to parse this String to a Jmeter ResultCollector object:

          <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
            <boolProp name="ResultCollector.error_logging">false</boolProp>
            <objProp>
              <name>saveConfig</name>
              <value class="SampleSaveConfiguration">
                <time>true</time>
                <latency>true</latency>
                <timestamp>true</timestamp>
                <success>true</success>
                <label>true</label>
                <code>true</code>
                <message>true</message>
                <threadName>true</threadName>
                <dataType>true</dataType>
                <encoding>false</encoding>
                <assertions>true</assertions>
                <subresults>true</subresults>
                <responseData>false</responseData>
                <samplerData>false</samplerData>
                <xml>false</xml>
                <fieldNames>true</fieldNames>
                <responseHeaders>false</responseHeaders>
                <requestHeaders>false</requestHeaders>
                <responseDataOnError>false</responseDataOnError>
                <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
                <assertionsResultsToSave>0</assertionsResultsToSave>
                <bytes>true</bytes>
                <sentBytes>true</sentBytes>
                <url>true</url>
                <threadCounts>true</threadCounts>
                <idleTime>true</idleTime>
                <connectTime>true</connectTime>
              </value>
            </objProp>
            <stringProp name="filename"></stringProp>
          </ResultCollector>

I do not want to new a ResultCollector, and set many fields, because it takes too much time. Is therer any esasy way to pass the element string, and get according java object.

1 Answers

The easiest way is to ask people in the Internet to do your job for you.

Take a look at SaveService.loadTree() function which reads JMeter's .jmx test plan into a HashTree

Once you load it you should be able to find your ResultsCollector instance from the HashTree

Example code:

package com.example;

import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;

public class JMeterTest {

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

        JMeterUtils.loadJMeterProperties("c:/apps/jmeter/bin/jmeter.properties");
        JMeterUtils.setJMeterHome("c:/apps/jmeter");

        HashTree testplan = SaveService.loadTree(new File("c:/apps/jmeter/bin/test.jmx"));
        SaveService.loadProperties();

        SearchByClass<ResultCollector> listenerSearch = new SearchByClass<>(ResultCollector.class);
        testplan.traverse(listenerSearch);
        Collection<ResultCollector> listeners = listenerSearch.getSearchResults();
        ResultCollector resultCollector = listeners.stream().findFirst().get();

        FileOutputStream resultCollectorOut = new FileOutputStream("resultcollector.dump");
        ObjectOutputStream objectOut = new ObjectOutputStream(resultCollectorOut);
        objectOut.writeObject(resultCollector);
        objectOut.flush();
        objectOut.close();
        
    }
}

Once you execute the code you will have resultcollector.dump file created in the folder where the project lives.

So the instance of a Results Collector can be created as simple as:

FileInputStream resultCollectorIn = new FileInputStream(new File("resultcollector.dump"));
ObjectInputStream objectIn = new ObjectInputStream(resultCollectorIn);

ResultCollector resultCollector = (ResultCollector)objectIn.readObject();
objectIn.close();

More information:

Related