一个测试类,跟java方式相似
groovy脚本,以下展示了主要代码,其他代码根据自己情况调整。
package service
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import model.AssetFreezeReq
import model.AssetInfo
import model.MemberGrowthAddRequest
import model.ShipperInfo
import net.grinder.plugin.http.HTTPRequest
import util.UserManager
/**
* 成长值服务调用
*/
class MemberGrowthService {
private static final DEV_BASE_URL = "https://dev.ymm56.com"
private static final QA_BASE_URL = "http://qa.ymm56.com"
private static final PROD_BASE_URL = "https://www.ymm56.com"
/**
* 发放入口
*/
static void sendGrowth() {
HTTPRequest request = new HTTPRequest()
List<NVPair> headerList = new ArrayList<>()
ShipperInfo shipperInfo = UserManager.getRandomShipper();
headerList.add(new NVPair("Content-Type", "application/json"));
headerList.add(new NVPair("Authorization", "" + shipperInfo.getAuthHeader() + ""));
headerList.add(new NVPair("Pressure-measurement", "LoadTest"));
headerList.add(new NVPair("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"));
NVPair[] headers = headerList.toArray() as NVPair[]
request.setHeaders(headers)
String bizId = UUID.randomUUID().toString();
String bizType = "pressure-test";
MemberGrowthAddRequest growthAddRequest = new MemberGrowthAddRequest();
growthAddRequest.setBizType(bizType);
growthAddRequest.setRequestId(bizId);
growthAddRequest.setAmount(100L);
// 短途100 平台10
growthAddRequest.setBusinessCode("100");
// 成长会员1 测试999
growthAddRequest.setIdentityType("1");
growthAddRequest.setChangeReason(bizType);
growthAddRequest.setGrowthType("BASE");
// 用户id 本地随机取
growthAddRequest.setUserId(Long.valueOf(shipperInfo.getUid()))
//资产发放
send(growthAddRequest, request);
}
/**
* 资产发放
* @param assetInfo
*/
private static void send(MemberGrowthAddRequest growthAddRequest, HTTPRequest request) {
def body = " {\n" +
"\"amount\":" + growthAddRequest.getAmount() + ",\n" +
"\"requestId\":\""+ growthAddRequest.getRequestId() +"\",\n" +
"\"growthType\":\""+ growthAddRequest.getGrowthType() +"\",\n" +
"\"bizType\":\""+ growthAddRequest.getBizType() + "\",\n" +
"\"userId\":"+ growthAddRequest.getUserId()+ ",\n" +
"\"businessCode\":"+ growthAddRequest.getBusinessCode() +",\n" +
"\"identityType\":"+growthAddRequest.getIdentityType()+"\n" +
"}"
println body
// 沿用网关
HTTPResponse result = request.POST(String.format("%s/ymm-charge-app/memberGradeGrowthService_1.0.0/memberGrowthSend", DEV_BASE_URL), body.getBytes())
// HTTPResponse result = request.POST(String.format("%s/ymm-charge-app/memberGradeGrowthService_1.0.0/memberGrowthSend", DEV_BASE_URL), body.getBytes())
// HTTPResponse result = request.POST(String.format("%s/ymm-charge-app/memberGradeGrowthService_1.0.0/memberGrowthSend", QA_BASE_URL), body.getBytes())
def text = result.getText()
println text
}
}
跟写junit test一样
入口代码:
import model.LoadTestMethod
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import service.MemberAccountService
import service.MemberAssetService
import service.MemberGrowthService
import util.CommonUtil
import util.LoggerUtil
import util.MethodManager
import util.UserManager
import static net.grinder.script.Grinder.grinder
@RunWith(GrinderRunner)
class TestRunner {
public static Map<String, GTest> monitors
public static HTTPRequest request
// public static Cookie[] cookies = []
public static List<LoadTestMethod> loadTestMethodList = new ArrayList<>(64)
/**
* 定义在进程被调用之前应执行的行为
*/
@BeforeProcess
public static void beforeProcess() {
try {
HTTPPluginControl.getConnectionDefaults().timeout = 5000
UserManager.initUserInfo()
MethodManager.initMethodConfig()
MethodManager.loadTestMethodMap.each {
k, v -> loadTestMethodList.add(v)
}
monitors = CommonUtil.initMonitor()
request = new HTTPRequest();
LoggerUtil.setLogLevel("info");
} catch (Exception e) {
LoggerUtil.error("process init error:", e);
System.exit(0);
}
}
/**
* 定义在每个线程被调用之前应执行的行为, 初始化用户, 日志级别设置
*/
@BeforeThread
public void beforeThread() {
grinder.statistics.delayReports = true;
monitors.each {
key, value -> value.record(this,"test")
}
}
/**
* 定义每个被 @Test 注解的方法被执行前应执行的行为
*/
@Before
public void before() {
//cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
}
/**
* 测试类
*/
@Test
public void test() {
int randomValue
int length = loadTestMethodList.size()
for (int i=0; i<length; i++) {
randomValue = CommonUtil.getRandom(MethodManager.percentAll)
int rightRangeValue = 0
LoadTestMethod method = null
// 找到要运行的方法
for (int j=0; j<length; j++) {
rightRangeValue += loadTestMethodList.get(j).getFlowRatioDefine()
if (randomValue <= rightRangeValue) {
method = loadTestMethodList.get(j)
break
}
}
if (Objects.nonNull(method) && method.getInvokeSwitch()) {
switch (method.getMethodName()) {
case "getMemberAccount":
MemberAccountService.getMemberAccount()
break
case "assetHandle":
MemberAssetService.assetHandle();
break;
case "sendGrowth":
MemberGrowthService.sendGrowth();
break;
}
}
}
}
}
本地运行jvm配置:
-javaagent:/Users/peter/mvn/net/sf/grinder/grinder-dcr-agent/3.9.1/grinder-dcr-agent-3.9.1.jar
ngrinder平台压测
这里面有三块 脚本 模本和测试任务,脚本配置完,每次及时更新代码就好,模板配置完一般不动,测试任务每次新建,压测完成会生产报告
配置脚本git地址和分支 配置模板 模板会关联脚本和一些压测用户 创建测试任务:
其他 压测机申请 压测机用完记得释放!!!!
|