Some performance benchmarks for EJBS are necessary and helpful, with a lot of testing methods and tools, but I recently discovered that Apache JMeter is an excellent tool for benchmarking. Unfortunately, JMeter does not provide a universal 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 of other containers should also be very similar.
1. First create a factory (Factory) class for EJB
The first thing we need to do is create a simple singleton factory class to create 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;
}
}