emu in blogjava

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

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

          posted on 2005-05-18 14:13 emu 閱讀(582) 評(píng)論(0)  編輯  收藏 所屬分類: java技術(shù)測(cè)試技術(shù)
          主站蜘蛛池模板: 万州区| 罗城| 镶黄旗| 汨罗市| 东乌珠穆沁旗| 漠河县| 惠东县| 郁南县| 沈阳市| 安阳县| 礼泉县| 台湾省| 天长市| 岱山县| 额尔古纳市| 黔江区| 岳阳市| 鹤山市| 随州市| 白朗县| 竹溪县| 淮滨县| 佳木斯市| 武夷山市| 化州市| 远安县| 唐河县| 绍兴市| 宁阳县| 旅游| 马尔康县| 海城市| 沙田区| 南澳县| 霍林郭勒市| 巴东县| 金沙县| 桐乡市| 伊春市| 诸暨市| 金寨县|