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 HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
//导入数据库连接依赖的包
import groovy.sql.Sql
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []
// 定义两个字符串列表,接收 账号和密码
public static String[] sql_nameList, sql_pwdList
// 定义sql连接数据库的对象
public static Sql sql = Sql.newInstance("jdbc:mysql://IP:port/databaseName",
"user",
"password",
"com.mysql.jdbc.Driver")
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "10.248.12.10")
request = new HTTPRequest()
// 设置请求头header
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("Content-Type", "application/json"))
headers = headerList.toArray()
// 定义查询语句的执行结果, LineList 是一个map
def LineList = sql.rows("SELECT user_name,password FROM `user` where user_name like 'test%';")
grinder.logger.info("sqlresult={}",LineList)
// 获取map中 name 和 pwd 的值
sql_nameList = LineList.user_name
sql_pwdList = LineList.password
grinder.logger.info("sql_nameList={},sql_pwdList={}",sql_nameList,sql_pwdList);
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
test.record(this, "test")
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 totalAgents=grinder.getProperties().getInt("grinder.agents",1); //总代理数
int total_processes=grinder.getProperties().getInt("grinder.processes",1); //总进程数
int total_threads=grinder.getProperties().getInt("grinder.threads",1); //总线程数
int Listindex=grinder.agentNumber*total_processes*total_threads + grinder.processNumber*total_threads + grinder.threadNumber + totalAgents * total_processes * total_threads * grinder.runNumber
grinder.logger.info("runNumber={},processNumber={},threadNumber={},name={},pwd={}",grinder.runNumber,grinder.processNumber,grinder.threadNumber,sql_nameList[Listindex],sql_pwdList[Listindex])
// 重新生成body
String body = "{\"userName\":\"${sql_nameList[Listindex]}\",\"password\":\"${sql_pwdList[Listindex]}\"}"
grinder.logger.info("body={}",body)
HTTPResponse result = request.POST("http://10.248.12.10:3462/login", body.getBytes())//发起请求
grinder.logger.info("响应报文={}",result.getText())
//设置断言
if(assertThat(result.getText(), containsString("\"code\":200"))){
grinder.logger.info("响应失败,响应报文={}",result.getText())
}
}
}