關(guān)于junitperf的一點(diǎn)介紹
junitperf 是個(gè)很小巧的java性能測(cè)試框架,可以在http://sourceforge.net/project/showfiles.php?group_id=15278 上下載到。可以很容易的把它結(jié)合junit一起測(cè)試,比如在測(cè)試套件里面加這么幾行:
import com.clarkware.junitperf.TimedTest;
import com.clarkware.junitperf.LoadTest;
.......
int testTimes = 10;
int users = 5;
suite.addTest(new TestUserDAO("test1AddUser")); //基本功能測(cè)試,同時(shí)初始化環(huán)境
suite.addTest(new TimedTest(new TestUserDAO("test1AddUser"), 1000)); //基本性能測(cè)試:方法應(yīng)在1秒內(nèi)完成
suite.addTest(new LoadTest(new TestUserDAO("test1AddUser"), users)); // 并發(fā)測(cè)試
suite.addTest(new LoadTest(new TestUserDAO("test1AddUser"),users,testTimes)); //并發(fā)負(fù)載測(cè)試
suite.addTest(new TimedTest(new LoadTest(new TestUserDAO("test1AddUser"),
users,testTimes), 35000)); //并發(fā)性能測(cè)試
如果只需要反復(fù)做一個(gè)測(cè)試而不需要并發(fā)測(cè)試,可以
suite.addTest(new LoadTest(new TestUserDAO("test1AddUser"),1,testTimes));
當(dāng)然也可以不用junitperf,junit.extensions.RepeatedTest就是設(shè)計(jì)來干這個(gè)的:
suite.addTest(new RepeatedTest(new TestUserDAO("test1AddUser"), testTimes)); //重復(fù)測(cè)試
用junitperf做并發(fā)測(cè)試帶來的問題
做并發(fā)測(cè)試的時(shí)候junitperf有一個(gè)問題。注意看這一行: new LoadTest(new TestUserDAO("test1AddUser"), users)
我們只傳遞了一個(gè)TestUserDAO實(shí)例給LoadTest,卻要求它開啟users個(gè)線程來測(cè)試,這樣這users個(gè)線程就會(huì)只針對(duì)同一個(gè)TestUserDAO實(shí)例進(jìn)行測(cè)試。這個(gè)時(shí)候,我們?cè)赥estUserDAO里面就不能存放任何狀態(tài)數(shù)據(jù)了。比如以前我很喜歡這么做:
public class TestUserDAO extends TestCase{
private int lastId;
protected void setUp() throws Exception{
super.setUp();
//構(gòu)造一個(gè)測(cè)試用的數(shù)據(jù)
User user = new User("張三");
//向數(shù)據(jù)庫(kù)插入一條記錄
userDAO.addUser(user);
lastId = user.getId();//剛剛插入的記錄在數(shù)據(jù)庫(kù)中產(chǎn)生的ID;
}
public void testUpdateUser() throws DaoException{
//針對(duì)setup中插入的數(shù)據(jù)進(jìn)行update操作
user = userDAO.getUserById(lastId);
user.setName("李四");
userDAO.updateUser(user);
}
protected void tearDown() throws Exception{
//刪除測(cè)試數(shù)據(jù)
userDAO.deleteUserById(lastId);
super.tearDown();
}
這樣我是通過一個(gè)int變量lastId在各個(gè)方法之間傳遞被測(cè)試的數(shù)據(jù)的。如果用junitperf來測(cè)試,lastId變量就會(huì)被后來的線程覆蓋,導(dǎo)致測(cè)試失敗。
解決方法
在com.clarkware.junitperf.TestFactory的文檔中對(duì)這個(gè)問題做了說明:
This factory class should be used in cases when a stateful test is intended to be decorated by a <code>LoadTest</code>. A stateful test is defined as any test that defines test-specific state in its <code>setUp()</code> method.
TestFactory的使用方法是這樣:
import com.clarkware.junitperf.TestFactory;
......
suite.addTest(new LoadTest(new TestFactory(TestUserDAO.class), users,testTimes)); //并發(fā)負(fù)載測(cè)試
但是這樣只能觀察整個(gè)測(cè)試類的表現(xiàn)。如果我們要單個(gè)的測(cè)試測(cè)試類中的一個(gè)測(cè)試,那么可以考慮另一種方法。我們?cè)赥estFactory的文檔中看到:
This class is dependent on Java 2. For earlier platforms a local cache implementation should be changed to use, for example, a HashMap to track thread-local information.
這個(gè)方法同時(shí)也適用于我們需要處理的情況:
public class TestUserDAO extends TestCase{
private static final ThreadLocal threadLocal = new ThreadLocal();
protected void setUp() throws Exception{
super.setUp();
//構(gòu)造一個(gè)測(cè)試用的數(shù)據(jù)
User user = new User("張三");
//向數(shù)據(jù)庫(kù)插入一條記錄
userDAO.addUser(user);
//lastId = user.getId();//剛剛插入的記錄在數(shù)據(jù)庫(kù)中產(chǎn)生的ID;
threadLocal.set(new Integer(user.getId()));
}
public void testUpdateUser() throws DaoException{
//針對(duì)setup中插入的數(shù)據(jù)進(jìn)行update操作
//user = userDAO.getUserById(lastId);
user = userDAO.getUserById(((Integer)threadLocal.get()).intValue());
user.setName("李四");
userDAO.updateUser(user);
}
protected void tearDown() throws Exception{
//刪除測(cè)試數(shù)據(jù)
userDAO.deleteUserById(((Integer)threadLocal.get()).intValue());
super.tearDown();
}