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

以下的例子是測(cè)試單元測(cè)試ExampleTestCase.testOneSecondResponse()方法對(duì)應(yīng)的功能的一個(gè)負(fù)載測(cè)試,用來(lái)測(cè)試該功能的執(zhí)行效率。其中有10個(gè)并發(fā)用戶,無(wú)延遲,每個(gè)用戶只運(yùn)行一次。LoadTest本身使用了TimedTest來(lái)得到在負(fù)載情況下ExampleTestCase.testOneSecondResponse()方法的實(shí)際運(yùn)行能力。如果全部的執(zhí)行時(shí)間超過(guò)了1.5秒則視為不通過(guò)。10個(gè)并發(fā)處理在1.5秒通過(guò)才算通過(guò)。

負(fù)載下承受能力測(cè)試舉例

import com.clarkware.junitperf.*;

import junit.framework.Test;

public class ExampleThroughputUnderLoadTest {

    public static Test suite() {

        int maxUsers = 10;

        long maxElapsedTime = 1500;

        Test testCase = new ExampleTestCase("testOneSecondResponse");

        Test loadTest = new LoadTest(testCase, maxUsers);

        Test timedTest = new TimedTest(loadTest, maxElapsedTime);

        return timedTest;

    }

    public static void main(String[] args) {

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

    }

}

在下面的例子中,測(cè)試被顛倒過(guò)來(lái)了,TimedTest度量ExampleTestCase.testOneSecondResponse()方法的執(zhí)行時(shí)間。然后LoadTest中嵌套了TimedTest來(lái)仿效10個(gè)并發(fā)用戶執(zhí)行ExampleTestCase.testOneSecondResponse()方法。如果某個(gè)用戶的執(zhí)行時(shí)間超過(guò)了1秒則視為不通過(guò)。

負(fù)載下響應(yīng)時(shí)間測(cè)試舉例

import com.clarkware.junitperf.*;

import junit.framework.Test;

public class ExampleResponseTimeUnderLoadTest {

    public static Test suite() {

        int maxUsers = 10;

        long maxElapsedTime = 1000;

        Test testCase = new ExampleTestCase("testOneSecondResponse");

        Test timedTest = new TimedTest(testCase, maxElapsedTime);

        Test loadTest = new LoadTest(timedTest, maxUsers);

        return loadTest;

    }

    public static void main(String[] args) {

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

    }

}

性能測(cè)試套件

下面的測(cè)試用例例子中把ExampleTimedTest和ExampleLoadTest結(jié)合在一個(gè)測(cè)試套件中,這樣可以自動(dòng)地執(zhí)行所有相關(guān)的性能測(cè)試了:

Example Performance Test Suite

import junit.framework.Test;

import junit.framework.TestSuite;

public class ExamplePerfTestSuite {

    public static Test suite() {

        TestSuite suite = new TestSuite();

        suite.addTest(ExampleTimedTest.suite());

        suite.addTest(ExampleLoadTest.suite());

        return suite;

    }

    public static void main(String[] args) {

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

    }

}

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