Some performance benchmarks for EJBS are necessary and helpful, and there are many methods and tools to test, but I recently discovered that Apache JMeter is an excellent tool for benchmarking. Unfortunately, JMeter does not provide a generic sampler (sampler) that can test arbitrary ejbs, but we can create one ourselves.
First, let's take a quick look at Apache JMeter, a 100% pure Java desktop application that can be used for stress testing and performance measurement. It was originally designed for Web application testing but later extended to other test areas.
In this article, I use the JBoss application server to run my EJB. The implementation process of using other containers should be very similar.
1. First create a factory (Factory) class for EJBs
The first thing we need to do is create a simple singleton factory class that creates an EJB client instance for your test. The reference code is as follows:
public class Myservicefactory {
Private static final Log log = Logfactory.getlog (Myservicefactory.class);
private static MyService service;
private static myservicefactory me;
Private Myservicefactory () {}
static {
myservicefactory.me = new Myservicefactory ();
}
public static Myservicefactory getinstance () {
return myservicefactory.me;
}
Public MyService GetService () {
if (Myservice.service = = null) {
try {
Log.info ("Loading the Service ...");
Context CTX = new InitialContext ();
Service = (MyService) ctx.lookup ("Myaction/remote");
if (service = = null) {
Log.error ("didn ' t get the service!");
}
} catch (Namingexception e) {
Log.error ("Error looking up the remote service", e);
return null;
}
}
return service;
}
}
2. Write test code
Next we need to write our own test code, in order to achieve this, we can extend the Abstractjavasamplerclient class in the Org.apache.jmeter.protocol.java.sampler package of JMeter. This abstract class has a runtest method that we need to override (override) and use this method to implement the actual test. In addition, we will override the Getdefaultparameters method so that it provides some reasonable default values that will be displayed in the JMeter graphical application interface.
Package us.mikedesjardins.demo.jmeter;
Import org.apache.jmeter.config.Arguments;
Import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
Import Org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
Import Org.apache.jmeter.samplers.SampleResult;
public class Digitalcontentserviceejbtestsampler extends Abstractjavasamplerclient {
Public Sampleresult Runtest (Javasamplercontext context) {
Sampleresult results = new Sampleresult ();
MyService service = Myservicefactory.getinstance (). GetService ();
Results.samplestart ();
Long param1 = Context.getlongparameter ("param_1");
String param2 = Context.getstringparameter ("param_2");
Myresult result = Service.mymethod (param1, param2);
if (result! = null) {
results.setsuclearcase/"target=" _blank ">ccessful (true);
Results.setresponsecodeok ();
Results.setresponsemessage ("' Myresult:" + myresult);
} else {
Results.setsuccessful (FALSE);
}
Results.sampleend ();
return results;
}
@Override
Public Arguments getdefaultparameters () {
Arguments args = new Arguments ();
Args.addargument ("Param_1", "4815162342");
Args.addargument ("Param_2", "Iculus");
return args;
}
}
3. Running JMeter
The extended LIB directory for JMETER is ${jmeter_install_lib}/lib/ext. You need to copy all the jar files required by the EJB client to this directory. If you are using JBoss, you need to copy the Jbossall-client.jar to this directory, and for other application servers, copy a similar client jar file to this directory.
When you start JMeter, your new Sampler (sampler) will appear in its sampler menu, so you can use it to test your EJB.
Use JMeter to test your EJB