Monday, December 17, 2007

Load testing stand alone classes with Apache JMeter

We want to collect timing information for some of your class methods. The kind of question we are interested in is, how long method A or method B takes. We would also like to subject our methods to multi thread load and analyze the performance under load. We also want pretty graphs of performance tests plus wish to save all the individual sample in some file.

Instead of doing this task by writing our own instrumentation and system.currentTimeMillis(), we decided to take a different path. Since we are already using apache JMeter for our web application load testing , we started looking for ways to do load testing of our class methods using apache JMeter. Finally, the task turned out to be much simpler than anticipated.

First up, you would like to create some custom samplers that apache JMeter can use. Below we present one such custom sampler.



public class JPACreateTest implements JavaSamplerClient {

public Arguments getDefaultParameters() {
    Arguments params = new Arguments();
    return params;
}

public SampleResult runTest(JavaSamplerContext context) {
    SampleResult results = new SampleResult();
    String subject = null ;
    String message = null ;
    String userName = " jpa console" ;

    try {
        Date now = new Date();
        String t = DateFormat.getDateTimeInstance().format(now);
        subject = " JPA create test @ " + t;
        message = " JPA create message @ " + t ;

        results.sampleStart();
        // call your methods
        MessageService messageService = new MessageServiceImpl();
        messageService.createNewMessage(userName, subject , message);
        results.sampleEnd();

        results.setSuccessful(true);

    } catch(Exception ex) {
        results.setSuccessful(false);
        ex.printStackTrace();
    }
    return results ;


}

public void setupTest(JavaSamplerContext arg0) {
// no initialization required

}

public void teardownTest(JavaSamplerContext arg0) {
// no cleanup required

}



Implement Java Sample Client interface and return a Sample Result. Now to use this Sampler in JMeter please follow these steps:

  • Put your sampler implementation in JMeter classpath. Better thing is to package the application class files inside a jar and drop the jar in JMeter/lib/ext folder.
  • Adjust the value of property user.classpath to include all the jars that are used by your application jar file. JMeter.properties file
Now you have created a sampler, put the jar file containing sampler inside apache JMeter lib/ext folder and all the dependencies of sampler jar is known to JMeter via user.classpath property. Now re-start JMeter UI. To add your sampler,


  • Right click context Menu
  • Add Sampler - Java Request
  • Select your Sampler from the drop down
Two Links have been especially useful

© Life of a third world developer
Maira Gall