Monday, 30 April 2012

Jmeter Sampler Client

Download  latest Jmeter binaries  and install as for instruction given.

You can use some Java development tools like eclipse for developing client program.
i am just creating simple sample program using eclipse because it provide lot of utility like auto compile, create jar etc.

Lets start with creating simple java class JmeterTestClient.java inside jmeter.sampler either by implementing JavaSamplerClient(org.apache.jmeter.protocol.java.sampler.JavaSamplerClient) or extending AbstractJavaSamplerClient(org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient)

you need ApacheJMeter_java.jar ApacheJMeter_core.jar for compilation only. You can find both jars at <<Installation_dir>>\jakarta-jmeter-2.4\lib\ext

When possible, Java tests should extend AbstractJavaSamplerClient rather than implementing JavaSamplerClient directly. This should protect your tests from future changes to the interface. While it may be necessary to make changes to the JavaSamplerClient interface from time to time (therefore requiring changes to any implementations of this interface), we intend to make this abstract class provide reasonable default implementations of any new methods so that subclasses do not necessarily need to be updated for new versions. Implementing JavaSamplerClient directly will continue to be supported for cases where extending this class is not possible (for example, when the client class is already a subclass of some other class).

This interface defines the interactions between the JavaSampler and external Java programs which can be executed by JMeter. Any Java class which wants to be executed as a JMeter test must implement this interface (either directly or indirectly through AbstractJavaSamplerClient).
JMeter will create one instance of a JavaSamplerClient implementation for each user/thread in the test. Additional instances may be created for internal use by JMeter (for example, to find out what parameters are supported by the client).
When the test is started, setupTest() will be called on each thread's JavaSamplerClient instance to initialize the client. Then runTest() will be called for each iteration of the test. Finally, teardownTest() will be called to allow the client to do any necessary clean-up

Compile the above java file and export as jar and put inside <<Installation_dir>>\jakarta-jmeter-2.4\lib\ext   
start jmeter , select testplan creat new thread group and select thread group add java request sampler . select JmeterTestClient  from classname drop down . save your test plan and run.
in my example i printed some text are you can see in console ,You can set loop count in thread group.
The sample code is.



package jmeter.sampler;


import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;


public class JmeterTestClient implements JavaSamplerClient {
int count;


@Override
public Arguments getDefaultParameters() {
count = 10;
System.out.println("inside default");
return null;
}


@Override
public SampleResult runTest(JavaSamplerContext arg0) {
System.out.println("Inside Run test:" + count++);
return null;
}


@Override
public void setupTest(JavaSamplerContext arg0) {
count++;
System.out.println("inside Setup");
}


@Override
public void teardownTest(JavaSamplerContext arg0) {
System.out.println("inside tear Down:" + count);
}


}

No comments:

Post a Comment