您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
JUnitPerf 使用手冊
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/1/18 14:31:06 ] 推薦標(biāo)簽:

long maxElapsedTime = 1000;

Test testCase = new ExampleTestCase("testOneSecondResponse");

Test timedTest = new TimedTest(testCase, maxElapsedTime);

同樣地,如果想要在執(zhí)行過程如果超出預(yù)期時間立即結(jié)束本次測試可以在TimedTest構(gòu)造函數(shù)中增加第三個參數(shù),舉例如下:

long maxElapsedTime = 1000;

Test testCase = new ExampleTestCase("testOneSecondResponse");

Test timedTest = new TimedTest(testCase, maxElapsedTime, false);

以下代碼創(chuàng)建了一個執(zhí)行時間的測試,用來測試被定義在單元測試ExampleTestCase.testOneSecondResponse()方法所代表的功能執(zhí)行的時間。

執(zhí)行效率測試舉例

import com.clarkware.junitperf.*;

import junit.framework.Test;

public class ExampleTimedTest {

    public static Test suite() {

        long maxElapsedTime = 1000;

        Test testCase = new ExampleTestCase("testOneSecondResponse");

        Test timedTest = new TimedTest(testCase, maxElapsedTime);

        return timedTest;

    }

    public static void main(String[] args) {

        junit.textui.TestRunner.run(suite());

    }

}

測試的粒度決定于JUnit的測試用例,并被JUnitPerf所使用,因此有一定的局限性。終獲得的執(zhí)行時間為測試用例中testXXX()方法的執(zhí)行時間,包括setUp(), testXXX(), 和tearDown()方法的執(zhí)行時間。執(zhí)行測試套件的時間包含測試套件中所有測試示例的setUp(), testXXX(), 和tearDown()方法的執(zhí)行時間。所以,預(yù)期的時間還應(yīng)該依照set-up和tear-down的執(zhí)行時間來制定(把這部分時間也考慮進(jìn)去)。

LoadTest

LoadTest用來仿效多個用戶并發(fā)執(zhí)行多次來進(jìn)行測試。

LoadTest簡單的構(gòu)造函數(shù)只有兩個參數(shù),測試用例和用戶數(shù),默認(rèn)情況下該測試只迭代一次。

例如,創(chuàng)建一個10用戶并發(fā)執(zhí)行一次ExampleTestCase.testOneSecondResponse()方法:

int users = 10;

Test testCase = new ExampleTestCase("testOneSecondResponse");

Test loadTest = new LoadTest(testCase, users);

負(fù)載測試過程也可以指定一個額外的計數(shù)器實例用來指定用戶并發(fā)執(zhí)行之間的延遲時間。ConstantTimer類構(gòu)造函數(shù)包含一個常量參數(shù),用來指定延遲時間,如果指定為0則表示所有的用戶同時開始。RandomTimer類可以構(gòu)造出隨機(jī)的延遲時間。

例如:創(chuàng)建一個負(fù)載測試,10個并發(fā)用戶各執(zhí)行一次ExampleTestCase.testOneSecondResponse()方法,各個用戶之間延遲1秒鐘執(zhí)行。

int users = 10;

Timer timer = new ConstantTimer(1000);

Test testCase = new ExampleTestCase("testOneSecondResponse");

Test loadTest = new LoadTest(testCase, users, timer);

為了仿效并發(fā)用戶以指定迭代次數(shù)執(zhí)行測試,LoadTest類構(gòu)造函數(shù)包含了RepeatedTest參數(shù)。這樣可以為每個測試用例指定迭代次數(shù)了。

例如:創(chuàng)建一個負(fù)載測試,10個并發(fā)用戶,每個用戶迭代執(zhí)行ExampleTestCase.testOneSecondResponse()方法20次,每個并發(fā)用戶之間延遲1秒。

int users = 10;

int iterations = 20;

Timer timer = new ConstantTimer(1000);

Test testCase = new ExampleTestCase("testOneSecondResponse");

Test repeatedTest = new RepeatedTest(testCase, iterations);

Test loadTest = new LoadTest(repeatedTest, users, timer);

或者這樣來寫:

int users = 10;

int iterations = 20;

Timer timer = new ConstantTimer(1000);

Test testCase = new ExampleTestCase("testOneSecondResponse");

Test loadTest = new LoadTest(testCase, users, iterations, timer);

如果負(fù)載測試要求測試在setUp()方法中包含特殊的測試狀態(tài),那么應(yīng)該使用TestFactory類來確保每個并發(fā)用戶線程使用一個本地線程測試實例。例如創(chuàng)建一個10用戶并發(fā)的測試,每個用戶運行ExampleStatefulTest類的一個本地線程,可這樣來寫:

int users = 10;

Test factory = new TestFactory(ExampleStatefulTest.class);

Test loadTest = new LoadTest(factory, users);

如果測試其中的某一個方法,可以這樣:

int users = 10;

Test factory = new TestMethodFactory(ExampleStatefulTest.class, "testSomething");

Test loadTest = new LoadTest(factory, users);

上一頁1234下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd