ngrinder(二) 压力测试脚本groovy 实战
前言
groovy脚本
ngrinder 的进程与线程
? 预置函数
? groovy 脚本实例? 压测实例/* 这个脚本是对需要验签接口的压测 */ import static net.grinder.script.Grinder.grinder import static org.junit.Assert.* import static org.hamcrest.Matchers.* import net.grinder.plugin.http.HTTPRequest import net.grinder.plugin.http.HTTPPluginControl import net.grinder.script.GTest import net.grinder.script.Grinder import net.grinder.scriptengine.groovy.junit.GrinderRunner import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread // import static net.grinder.util.GrinderUtils.* // You can use this if you‘re using nGrinder after 3.2.3 import org.junit.Before import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith import java.util.Date import java.util.List import java.util.ArrayList import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Level; import HTTPClient.Cookie import HTTPClient.CookieModule import HTTPClient.HTTPResponse import HTTPClient.NVPair import java.text.SimpleDateFormat; import org.apache.commons.codec.binary.Base64; import java.security.spec.PKCS8EncodedKeySpec; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.Signature; import java.util.Arrays; import org.apache.commons.lang.StringUtils; import java.lang.StringBuilder import java.io.UnsupportedEncodingException; /** * A simple example using the HTTP plugin that shows the retrieval of a * single page via HTTP. * * This script is automatically generated by ngrinder. * * @author admin */ @RunWith(GrinderRunner) class TestRunner { public static GTest test public static HTTPRequest request // 定义全局变量 public static NVPair[] params = [] public static Cookie[] cookies = [] public static String private_key public static String[] contents = [] @BeforeProcess public static void beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 6000 test = new GTest(1,"Test1") request = new HTTPRequest() // 获取加密私钥内容 contents = new File("./resources/rsa_private_key_pkcs8.pem") as String[] StringBuilder private_str = new StringBuilder(); for(int i=0;i<contents.length;i++){ if (contents[i].charAt(0) != ‘-‘ && contents[i] != null) { private_str.append(contents[i]).append("n"); } } private_key = private_str.toString() //调试输出 grinder.logger.info("before process."); } @BeforeThread public void beforeThread() { test.record(this,"test") grinder.statistics.delayReports=true; grinder.logger.info("before thread."); } //自定义函数,修改http头数据 private NVPair[] headers(post) { if( post != null ){ post="&"+post }else{ post="" } def appid = "2222222222222222222211" def appcode = "22222" long currentTime = System.currentTimeMillis() def json = "appId="+appid+"&appCode="+appcode+"&"+""+"timestamp="+currentTime+post def sign = getsign(json) //grinder.logger.info(currentTime.toString()) return [ new NVPair("Content-type","application/json;charset=UTF-8"),new NVPair("appId","1013f9d4e97026cb07e3fdea1b560f2f"),new NVPair("sign",sign),new NVPair("timestamp",currentTime.toString()),new NVPair("appCode","22222") ]; } //自定义函数,生成YYYY-MM-dd HH:mm:ss 格式的当天日期串 def today() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); Calendar lastDate = Calendar.getInstance(); str = sdf.format(lastDate.getTime()); return str; } // 自定义函数加签 private getsign(post){ //加签算法 略 return sign } @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(){ // 参数初始化 def domain = "api.xxxx.com" def timestr=today() def json=‘{"eventCode":"2222211","eventTime":"‘+timestr+‘","channelCode":"2222211",eventTime":"‘+timestr+‘"}}‘; //获取签名后的头信息 def head = headers("bizContent="+json) //grinder.logger.info(head.toString()) HTTPResponse result = request.POST(‘http://‘+domain+‘/channel/v1/mot/receive‘,json.getBytes(),head) //HTTPResponse result = request.GET("http://106.14.8.248/phpapi.php",params) if(result.text.equals(new String(result.text.getBytes("iso8859-1"),"iso8859-1"))) { result.text=new String(result.text.getBytes("iso8859-1"),"utf-8"); } //调试输出头信息 grinder.logger.info("result="+request.getHeaders()) //调试输出结果数据 grinder.logger.info("result="+result.text) 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)); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |