emu in blogjava

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評(píng)論 :: 2 Trackbacks

          用junitperf做并發(fā)測(cè)試帶來(lái)的問(wèn)題

          關(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ì)來(lái)干這個(gè)的:

          suite.addTest(new RepeatedTest(new TestUserDAO("test1AddUser"), testTimes)); //重復(fù)測(cè)試

          用junitperf做并發(fā)測(cè)試帶來(lái)的問(wèn)題

          做并發(fā)測(cè)試的時(shí)候junitperf有一個(gè)問(wèn)題。注意看這一行:

          new LoadTest(new TestUserDAO("test1AddUser"), users)

          我們只傳遞了一個(gè)TestUserDAO實(shí)例給LoadTest,卻要求它開啟users個(gè)線程來(lái)測(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();
           }
          這樣我是通過(guò)一個(gè)int變量lastId在各個(gè)方法之間傳遞被測(cè)試的數(shù)據(jù)的。如果用junitperf來(lái)測(cè)試,lastId變量就會(huì)被后來(lái)的線程覆蓋,導(dǎo)致測(cè)試失敗。

          解決方法

          在com.clarkware.junitperf.TestFactory的文檔中對(duì)這個(gè)問(wèn)題做了說(shuō)明:

          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è)方法同事也適用于我們需要處理的情況:

          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();
           }

          posted on 2005-05-16 18:52 emu 閱讀(686) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 乡宁县| 巴塘县| 九寨沟县| 冕宁县| 南乐县| 民县| 丰镇市| 大同市| 海晏县| 铁岭县| 南部县| 奉化市| 饶平县| 潮州市| 嘉鱼县| 闸北区| 崇文区| 南投市| 乳源| 新疆| 永昌县| 仁化县| 岢岚县| 尉犁县| 栾城县| 大荔县| 宜黄县| 武威市| 蒙城县| 石泉县| 七台河市| 凤山市| 鸡东县| 梧州市| 岫岩| 沿河| 灌云县| 泗洪县| 祁门县| 枝江市| 大埔县|