服务器抓取请求
- 登录线上服务器,到日志目录执行 cat 2022-02-16-app-rcd-recall*.info.log|grep -m 2000 “S_Information_B7”> /home/app/req.txt (抓取2000条)
- 利用把请求数据文件下载到本地
脚本介绍
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
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 org.ngrinder.http.HTTPRequest
import org.ngrinder.http.HTTPRequestControl
import org.ngrinder.http.HTTPResponse
import org.ngrinder.http.cookie.Cookie
import org.ngrinder.http.cookie.CookieManager
/**
* 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 Map<String, String> headers = [:]
public static Map<String, Object> params = [:]
public static List<Cookie> cookies = []
public static String body
public static Integer maxrow, counterun, linesnum // 定义最大行数,运行的次数,取文档中行数
// 读取文件
public static List<String> readfile = new File("./resources/req.txt").readLines("utf8")
@BeforeProcess
public static void beforeProcess() {
HTTPRequestControl.setConnectionTimeout(300000)
test = new GTest(1, "10.24.17.121")
request = new HTTPRequest()
// Set header data
headers.put("Content-Type", "application/json")
maxrow = readfile.size() // 获取文件 最大行数
// Set header data
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)
CookieManager.addCookies(cookies)
counterun = grinder.runNumber // 获取运行次数
grinder.logger.info("=======counterun is : {}", counterun)
// 逻辑判断,取值
if (counterun < maxrow) {
linesnum = counterun
}else {
linesnum = counterun % maxrow
}
grinder.logger.info("=======linesnum is : {}", linesnum)
body = readfile[linesnum].substring(readfile[linesnum].indexOf("req:") + 4, readfile[linesnum].indexOf("itemCnt") - 1)
grinder.logger.info("=======body:{}", body);
grinder.logger.info("before. init headers and cookies");
}
@Test
public void test() {
HTTPResponse response = request.POST("http://10.24.17.121:8081/recommend/api", body.getBytes())
grinder.logger.info("============test:{}", response.statusCode)
if (response.statusCode == 301 || response.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", response.statusCode)
} else {
assertThat(response.statusCode, is(200))
}
}
}
1.
需要把抓取的线上请求文件,上传到resources目录,请求文件名为req.txt
2.
如果每条请求数据需要解析,请向上面标红处解析
3.
标红处为请求连接,可修改
上传请求参数文件
1.
2.
选择请求日志文件上传
执行发送请求
1.
2.
转载请注明:学时网 » 利用rc-ngrinder模拟发送线上请求