使用testNG进行并发性能测试

来源:互联网 发布:centos twisted 安装 编辑:程序博客网 时间:2024/05/01 12:20
Java代码  收藏代码
  1. import java.lang.reflect.Method;  
  2.   
  3. import org.testng.annotations.DataProvider;  
  4. import org.testng.annotations.Test;  
  5.   
  6. public class NewTest {  
  7.       
  8.     @DataProvider(name = "dp")    
  9.     public Object[][] createData(Method m) {    
  10.       System.out.println(m.getName());  // print test method name    
  11.       return new Object[][] { new Object[] { "Cedric" }};    
  12.   
  13.     }    
  14.   
  15.          
  16.   
  17.     @Test(dataProvider = "dp")    
  18.     public void test1(String s) {    
  19.         System.out.println(s);  
  20.         assert true;  
  21.     }    
  22.   
  23.          
  24.   
  25.     @Test(dataProvider = "dp")    
  26.     public void test2(String s) {    
  27.          System.out.println(s);  
  28.             assert true;  
  29.     }   
  30.   
  31.     @Test(invocationCount=1000,threadPoolSize=500)  
  32.     public void testMethod() throws Exception{  
  33.         int i = 0;  
  34.         while(i < 100){  
  35.             System.out.println(i++);  
  36.             Thread.sleep(100);  
  37.         }  
  38.     }  
  39.   
  40. }  

 

invocationCount设定的是这个方法的执行次数

threadPoolSize 这个属性表示的是开启线程数的多少,threadPoolSize的设定要依赖 invocationCount的设定,如果invocationCount的设定值小于threadPoolSize的设定值,多于的设定是无效的,举个极端的例子,如果你threadPoolSize设定是100,而invocationCount没有设定(默认为1次),那么系统只有开启一个线程来运行。反过invocationCount的设定不依赖threadPoolSize,testNG会以默认值1来运行。

 

开始时我将threadPoolSize设置去掉,然后执行,发现输出结果都是顺序的,而加上threadPoolSize设定后输出开始有些错乱,表明确实是多线程在执行。

我们能够使用这种方法进行并发测试和性能测试。



0 0
原创粉丝点击