ngrinder混合场景压测例子e

原理简单就是通过随机算法进行分配

1.进行50% 50%请求。根据具体的请求进行混合。这里作为例子进行

具体按照比例可以根据实际情况获取。比例。或者是根据请求的次数进行比例换算

2.通过定义2个Gtest。进行50% 接口的互相混合;

要求 命中率
test1 50%
test2

50%

3.想出多线的报表,需要配置间隔

这里配置一下;

....... 

@RunWith(GrinderRunner)

class TestRunner {
public static GTest test1
    public static GTest test2
    public static GTest test3
public static HTTPRequest request
public static NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []
public static String[] basic
Random random = new Random() 


@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
//test = new GTest(2, "****.com")
test1 = new GTest(1, "test1")  // 参数为 ID、显示名
                test2 = new GTest(1, "test2")
                test3 = new GTest(1, "test3")
request = new HTTPRequest()
basic = new File("./resources/cityandversion.csv") as String[]
grinder.logger.info("before process.");
}


@BeforeThread 
public void beforeThread() {
//test.record(this, "test")
test1.record(this, "test1")  
        test2.record(this, "test2")
        test3.record(this, "test3")
grinder.statistics.delayReports=true
grinder.logger.info("before thread.");
}


@Before
public void before() {
request.setHeaders(headers)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before thread. init headers and cookies");
}
 

     //核心代码

@Test
public void test(){
int n =(int)new java.util.Random().nextInt(100)
grinder.logger.info(String.valueOf(n))
                /***/
switch (n) {  
 
case 0 .. 50:          // n为90-97之间时  
this.test1() 
break;  
case 51 .. 100:          // n为70-89之间时  
this.test2()
break;  

}  



}

    
    
public void test1(){
HTTPResponse result = request.GET("https://xxx.com/api/nearby/v3/nearbyBikeInfo?longitude=116.46723372&latitude=39.94815657&platform=1&bikenum=100&biketype=4", params)


if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
} else {
assertThat(result.statusCode, is(200));
}

}




public void test2(){


HTTPResponse result = request.POST("https://xxx.com/api/v2/api/config/v1.do?version=010&citycode=010", params)


if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
} else {
assertThat(result.statusCode, is(200));
}
}

public void test3(){

HTTPResponse result = request.GET("https://xxx.com/api/v2/rentmgr/getriding.do", params)


if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
} else {
assertThat(result.statusCode, is(200));
}


}
}

猜你喜欢

转载自blog.csdn.net/keny88888/article/details/80679565