emu in blogjava

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

          關于junitperf的一點介紹

          junitperf 是個很小巧的java性能測試框架,可以在http://sourceforge.net/project/showfiles.php?group_id=15278 上下載到。可以很容易的把它結(jié)合junit一起測試,比如在測試套件里面加這么幾行:

          import com.clarkware.junitperf.TimedTest;
          import com.clarkware.junitperf.LoadTest;
          .......

              int testTimes = 10;
              int users = 5;

              suite.addTest(new TestUserDAO("test1AddUser")); //基本功能測試,同時初始化環(huán)境
            
              suite.addTest(new TimedTest(new TestUserDAO("test1AddUser"), 1000)); //基本性能測試:方法應在1秒內(nèi)完成

              suite.addTest(new LoadTest(new TestUserDAO("test1AddUser"), users)); // 并發(fā)測試

            suite.addTest(new LoadTest(new TestUserDAO("test1AddUser"),users,testTimes)); //并發(fā)負載測試

            suite.addTest(new TimedTest(new LoadTest(new TestUserDAO("test1AddUser"),
                                                     users,testTimes), 35000)); //并發(fā)性能測試

          如果只需要反復做一個測試而不需要并發(fā)測試,可以

          suite.addTest(new LoadTest(new TestUserDAO("test1AddUser"),1,testTimes));

          當然也可以不用junitperf,junit.extensions.RepeatedTest就是設計來干這個的:

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


          用junitperf做并發(fā)測試帶來的問題


          做并發(fā)測試的時候junitperf有一個問題。注意看這一行:

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

          我們只傳遞了一個TestUserDAO實例給LoadTest,卻要求它開啟users個線程來測試,這樣這users個線程就會只針對同一個TestUserDAO實例進行測試。這個時候,我們在TestUserDAO里面就不能存放任何狀態(tài)數(shù)據(jù)了。比如以前我很喜歡這么做:

          public class TestUserDAO extends TestCase{
           private int lastId;
           protected void setUp() throws Exception{
            super.setUp();
            //構造一個測試用的數(shù)據(jù)
            User user = new User("張三");
            //向數(shù)據(jù)庫插入一條記錄
            userDAO.addUser(user);
            lastId = user.getId();//剛剛插入的記錄在數(shù)據(jù)庫中產(chǎn)生的ID;
           }

           public void testUpdateUser() throws DaoException{
            //針對setup中插入的數(shù)據(jù)進行update操作
            user = userDAO.getUserById(lastId);
            user.setName("李四");
            userDAO.updateUser(user);
           }
           protected void tearDown() throws Exception{
            //刪除測試數(shù)據(jù)
            userDAO.deleteUserById(lastId);
            super.tearDown();
           }
          這樣我是通過一個int變量lastId在各個方法之間傳遞被測試的數(shù)據(jù)的。如果用junitperf來測試,lastId變量就會被后來的線程覆蓋,導致測試失敗。


          解決方法


          在com.clarkware.junitperf.TestFactory的文檔中對這個問題做了說明:

          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ā)負載測試

          但是這樣只能觀察整個測試類的表現(xiàn)。如果我們要單個的測試測試類中的一個測試,那么可以考慮另一種方法。我們在TestFactory的文檔中看到:

           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.

          這個方法同時也適用于我們需要處理的情況:

          public class TestUserDAO extends TestCase{
           private static final ThreadLocal threadLocal = new ThreadLocal();
           protected void setUp() throws Exception{
            super.setUp();
            //構造一個測試用的數(shù)據(jù)
            User user = new User("張三");
            //向數(shù)據(jù)庫插入一條記錄
            userDAO.addUser(user);
            //lastId = user.getId();//剛剛插入的記錄在數(shù)據(jù)庫中產(chǎn)生的ID;
            threadLocal.set(new Integer(user.getId()));
           }

           public void testUpdateUser() throws DaoException{
            //針對setup中插入的數(shù)據(jù)進行update操作
            //user = userDAO.getUserById(lastId);
            user = userDAO.getUserById(((Integer)threadLocal.get()).intValue());
            user.setName("李四");
            userDAO.updateUser(user);
           }
           protected void tearDown() throws Exception{
            //刪除測試數(shù)據(jù)
            userDAO.deleteUserById(((Integer)threadLocal.get()).intValue());
            super.tearDown();
           }

          posted on 2005-05-18 14:13 emu 閱讀(584) 評論(0)  編輯  收藏 所屬分類: java技術測試技術
          主站蜘蛛池模板: 吴桥县| 政和县| 宜州市| 巢湖市| 越西县| 胶州市| 鄂托克旗| 石景山区| 青河县| 拜泉县| 资中县| 全南县| 祁门县| 丰顺县| 镇原县| 永宁县| 南投市| 西乌| 汕尾市| 郧西县| 阳泉市| 枣强县| 陵水| 方城县| 平乡县| 安岳县| 安阳县| 屯门区| 钟山县| 和田市| 滁州市| 华坪县| 晋州市| 高安市| 灵山县| 灌云县| 随州市| 大新县| 吉木乃县| 藁城市| 花莲市|