0
votes

I'm trying to run JMeter programmatically with my own sampler. I have been able to successfully run it with the HTTPSamplerProxy so I'm pretty sure that the process works.
If I simply replace

HTTPSamplerProxy javaSampler = new HTTPSamplerProxy();

With

CustomJavaSampler javaSampler = new CustomJavaSampler();

where CustomJavaSampler is

public class CustomJavaSampler extends AbstractJavaSamplerClient implements Serializable, Interruptible{

I get

summary = 0 in 0s = ******/s Avg: 0 Min: 9223372036854775807 Max: -9223372036854775808 Err: 0 (0.00%)

as compared to

summary = 23 in 23.3s = 1.0/s Avg: 21001 Min: 20993 Max: 21008 Err: 23 (100.00%)

A more complete listing of the code that worked follows:

    private StandardJMeterEngine doStandardJMeterSetup() {

    File jmeterProperties = new File(jmeterHome.getPath() + File.separator + "bin" + File.separator + "jmeter.properties");

    StandardJMeterEngine jmeter = new StandardJMeterEngine();

    //JMeter initialization (properties, log levels, locale, etc)
    JMeterUtils.setJMeterHome(jmeterHome.getPath());
    JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
    JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
    JMeterUtils.initLocale();

    return jmeter;
}


private void doJMeterStuff(int concurrentCount, int port, List<String> testData) {
    StandardJMeterEngine jmeter = doStandardJMeterSetup();

    // JMeter Test Plan, basically JOrphan HashTree
    HashTree testPlanTree = new HashTree();

    // Second HTTP Sampler - open jon's machine
    HTTPSamplerProxy javaSampler = new HTTPSamplerProxy();
    System.out.println("The domain is: " + testData.get(0) + " " + testData.get(0).getClass().getName());
    javaSampler.setDomain(testData.get(0));
    javaSampler.setPort(Integer.parseInt(testData.get(1)));
    javaSampler.setPath(testData.get(2));
    javaSampler.setMethod("GET");
    javaSampler.setName("Open implementation at " + port);
    javaSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
    javaSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
    Arguments jargs = javaSampler.getArguments();
    jargs.addArgument(new HTTPArgument(testData.get(3), testData.get(4), testData.get(5), true));
    jargs.getArgument(0).setProperty("HTTPArgument.always_encode", false);

    // Loop Controller
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    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.setName("Example Thread Group");
    threadGroup.setNumThreads(concurrentCount);
    threadGroup.setRampUp(2);
    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());

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


    //add Summarizer output to get test progress in stdout like:
    // summary =      2 in   1.3s =    1.5/s Avg:   631 Min:   290 Max:   973 Err:     0 (0.00%)
    Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }

    // Store execution results into a .jtl file
    String tmpDir = System.getProperty("java.io.tmpdir");
    String logFile = tmpDir + File.separator + "example"+port+".jtl";
    CustomResultCollector logger = new CustomResultCollector(summer);
    logger.setFilename(logFile);
    testPlanTree.add(testPlanTree.getArray()[0], logger);

    String logFile2 = tmpDir + File.separator + "example_results_"+port+".jtl";

    ResultCollector r = new ResultCollector();
    r.setFilename(logFile2);
    SampleSaveConfiguration ssc = new SampleSaveConfiguration();
    ssc.setAsXml(true);
    ssc.setResponseData(true);
    ssc.setResponseHeaders(true);
    r.setSaveConfig(ssc);

    ResultSaver rs = new ResultSaver();
    rs.setEnabled(true);
    rs.setName("Responses");

    javaSampler.addTestElement(rs);

    testPlanTree.add(testPlanTree.getArray()[0], r);
    testPlanTree.add(testPlanTree.getArray()[0], rs);

    // save generated test plan to JMeter's .jmx file format
    try {
        SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + File.separator + "example"+port+".jmx"));
    } catch (IOException e) {
        e.printStackTrace();
    }

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


}
1

1 Answers

0
votes

In your CustomJavaSampler, you are probably already overriding sample function, so SampleResult it's supposed to return is what will contain data for the statistics. For example the following will ensure sample result will have a start and end time, and will be set to status "success":

@Override
public SampleResult sample(Entry e) 
{    
    SampleResult sampleResult = new SampleResult();
    sampleResult.setSampleLabel( "My label" );
    sampleResult.sampleStart();

    // do something useful

    sampleResult.sampleEnd();
    sampleResult.setSuccessful( true );
    sampleResult.setResponseMessageOK();
    return sampleResult;
}

Check SampleResult reference