您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源性能測(cè)試工具 >
認(rèn)識(shí) p-unit : 一款開源的性能測(cè)試工具
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2012/12/6 15:41:27 ] 推薦標(biāo)簽:

這是做為普通的測(cè)試案例,但是注意到這僅僅是一個(gè)測(cè)試案例,不包含其他任何邏輯,這也是 p-unit 追求的業(yè)務(wù)邏輯和測(cè)試運(yùn)行環(huán)境分離的一個(gè)理念。同一個(gè)測(cè)試案例,用戶可以選擇不同的測(cè)試環(huán)境去運(yùn)行,而不是綁定在某一個(gè)特定的測(cè)試軟件工具上,F(xiàn)在我們來看 p-unit 是如何運(yùn)行這個(gè)測(cè)試案例的。你只需要在 main 函數(shù)中寫一行代碼來運(yùn)行它:


清單 2. 單線程運(yùn)行測(cè)試案例

 
public static void main(String[] args) {
 new PUnitSoloRunner().run(SimpleTestClass.class);
}
 

 

清單 3. 單線程運(yùn)行測(cè)試案例結(jié)果

[solo] Started running samples.SimpleTestClass
samples.SimpleTestClass
testA
testA() - [287.0ms]
testB() - [27.0ms]
testC() - [213.0ms]
total: 3, failures:0 (GREEN) - 2025.0ms
 


是否和想象中的一樣?下面我們來看如何多線程執(zhí)行這個(gè)測(cè)試案例。或許從上面的例子你已經(jīng)猜到了,在 main 函數(shù)還是只需一句代碼,只用把 PUnitSoloRunner 換成 PUnitConcurrentRunner 即可!


清單 4. 多線程運(yùn)行測(cè)試案例

public static void main(String[] args) {
 new PUnitConcurrentRunner().run(SimpleTestClass.class);
}
 

 

清單 5. 多線程運(yùn)行測(cè)試案例結(jié)果

[concurrent] Started running samples.SimpleTestClass
samples.SimpleTestClass
testA
testA
testA
testA
testA
testA
testA
testA
testA
testA
testA() - [405.0ms]
testB() - [469.0ms]
testC() - [503.0ms]
total: 3, failures:0 (GREEN) - 1447.0ms
 


是否和想象中的一樣?默認(rèn)情況 p-unit 啟動(dòng) 10 個(gè)線程來執(zhí)行,要指定不同的線程數(shù),只需將線程數(shù)做為參數(shù)傳入 PUnitConcurrentRunner 即可。p-unit 甚至支持不同的測(cè)試案例有不同的線程數(shù),這要求測(cè)試案例實(shí)現(xiàn) p-unit 中定義的 Concurrent 接口,該接口的定義為:


清單 6. p-unit Concurrent 接口

public interface Concurrent {
 public int concurrentCount();
}
 


該接口的意思,相信無需再多做解釋了,返回該測(cè)試案例需要的線程數(shù)。

參數(shù)化測(cè)試案例

性能測(cè)試,不同于單元測(cè)試,經(jīng)常要求測(cè)試不同數(shù)量級(jí)在同一個(gè)測(cè)試場(chǎng)景中的表現(xiàn),JUnit 是一款非常的單元測(cè)試工具,但沒覆蓋到這個(gè)方面。比如我們比較類庫(kù) Foo1 的方法 bar() 和類庫(kù) Foo2 的方法 bar() 哪個(gè)更符合自己的應(yīng)用程序,我們需要測(cè)試該函數(shù)在應(yīng)用程序可能的數(shù)量級(jí)的范圍內(nèi)的表現(xiàn)。有經(jīng)驗(yàn)的開發(fā)者知道經(jīng)常碰到在小數(shù)量級(jí) A 更好大數(shù)量級(jí) B 更好的局面,因此全面的測(cè)試對(duì)于代碼的性能理解非常重要,能幫助開發(fā)者做出正確的決定。p-unit 支持將參數(shù)傳給測(cè)試方法,測(cè)試案例需要實(shí)現(xiàn) p-unit 的 parameterizable 接口,該接口的主要方法是返回一組參數(shù)列表,這組列表的參數(shù)將會(huì)一一傳給測(cè)試方法。

清單 7. p-unit 參數(shù)化測(cè)試案例

public class ParamTestClass implements Parameterizable {

 public static void main(String[] args) {
  new PUnitSoloRunner().run(ParamTestClass.class);
 }
 
 public Parameter[] parameters() {
  return new Parameter[] { new ParameterImpl(10), new ParameterImpl(20) };
 }

 public void testA(ParameterImpl param) {
  SampleUtil.doSomething();
 }
 
 public void testB(ParameterImpl param) {
  SampleUtil.doSomething();
 }
 
 public void testC(ParameterImpl param) {
  SampleUtil.doSomething();
 }
 
 public void setUpAfterWatchers(Parameter param) throws Exception {

 }

 public void setUpBeforeWatchers(Parameter param) throws Exception {

 }

 public void tearDownAfterWatchers(Parameter param) throws Exception {

 }

 public void tearDownBeforeWatchers(Parameter param) throws Exception {

 }

 static class ParameterImpl implements Parameter {
  private int _count;

  ParameterImpl(int count) {
   _count = count;
  }

  public int count() {
   return _count;
  }
  
  public String toString() {
   return String.valueOf(_count);
  }
 }
}

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