我正在尝试在EJB 2.1 bean上生成多个线程来测试连接池上的负载。有什么方法可以做到这一点,或者我可以使用开源工具吗?
将对ejb的调用封装在Servlet中。有各种语言的各种工具可以模拟web应用程序的加载。
我使用Grinder对OpenEJB进行负载测试。真是太棒了。
grinder.py文件的示例。这实际上是客户端:
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from javax.naming import Context,InitialContext
from java.util import Properties
# A shorter alias for the grinder.logger.output() method.
log = grinder.logger.output
tests = {
"ping" : Test(1, "ping"),
"add" : Test(2, "add"),
"sum" : Test(3, "sum"),
}
# Wrap the log() method with our Test and call the result logWrapper.
# Calls to logWrapper() will be recorded and forwarded on to the real
# log() method.
#logWrapper = test1.wrap(log)
# Initial context lookup for EJB home.
p = Properties()
p[Context.INITIAL_CONTEXT_FACTORY] = "org.apache.openejb.client.RemoteInitialContextFactory"
p[Context.PROVIDER_URL] = "ejbd://127.0.0.1:4201";
loadBean = InitialContext(p).lookup("LoadBeanRemote")
pingBean = tests["ping"].wrap(loadBean)
addBean = tests["add"].wrap(loadBean)
sumBean = tests["sum"].wrap(loadBean)
# A TestRunner instance is created for each thread. It can be used to
# store thread-specific data.
class TestRunner:
# This method is called for every run.
def __call__(self):
pingBean.ping()
addBean.add(3, 4)
sumBean.sum([3, 4, 5, 6])
注意grinder.py文件是用jython编写的,因此您可以连接任何java客户机jar。
这是一个示例grinder。属性文件:
grinder.script grinder.py
grinder.processes 2
grinder.threads 20
grinder.runs 0
grinder.jvm.classpath=/Users/dblevins/work/grinder/openejb-3.1.4/lib/openejb-client-3.1.4.jar:
/Users/dblevins/work/grinder/openejb-3.1.4/lib/javaee-api-5.0-3.jar:
/Users/dblevins/work/grinder/openejb-3.1.4/lib/ejb31-experimental-api-3.1.4.jar:
/Users/dblevins/work/grinder/load-beans/target/load-beans-1.0.jar
grinder.logDirectory logs
grinder.numberOfOldLogs 0
然后LoadBean看起来像这样(你想要加载测试的应用程序):
package org.superbiz.load;
import javax.ejb.*;
import java.lang.reflect.Method;
@Local
@Remote
@Singleton
@Lock(LockType.READ)
public class LoadBean implements Load {
public void ping() {
// do nothing
}
public int add(int a, int b) {
return a + b;
}
public int sum(int... items) {
int i = 0;
for (int item : items) {
i += item;
}
return i;
}
}