原文作者:李文
背景:
官方网站为:http://naver.github.io/ngrinder/
在实际压测工作中psot请求分为两种最常见传参情况,以下分别介绍这两种脚本编写:
- 第一种是通过key-->value方式传参数,head参数为:Content-Type:application/x-www-form-urlencoded
- 第二种是通过json方式传参数,head参数为:Content-Type:application/json
咱们开启post脚本之旅
前置条件是大家源码部署成功的,这样方面咱们直接在源码的脚本位置添加咱们调试的脚本,下面咱们使用两种方式做例子分别介绍:
在模拟请求的服务端的springboot工程的controller层添加如下代码:
@PostMapping("/findinfo")
@ResponseBody
public List<UserTable> findUserPost(UserTable userInfo) {
List<UserTable> UserInfo = userService.findinfo(userInfo);
return UserInfo;
}
如果上面代码调用看不明白请参考:
性能工具之Ngrinder之Get请求脚本编写
1、post请求方式
参考代码
1. import HTTPClient.Cookie
2. import HTTPClient.CookieModule
3. import HTTPClient.HTTPResponse
4. import HTTPClient.NVPair
5. import net.grinder.plugin.http.HTTPPluginControl
6. import net.grinder.plugin.http.HTTPRequest
7. import net.grinder.script.GTest
8. import net.grinder.scriptengine.groovy.junit.GrinderRunner
9. import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
10. import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
11. import org.junit.Before
12. import org.junit.Test
13. import org.junit.runner.RunWith
14.
15. import static net.grinder.script.Grinder.grinder
16. import static org.hamcrest.Matchers.is
17.
18. // import static net.grinder.util.GrinderUtils.
19. * // You can use this if you're using nGrinder after 3.2.3
20.
21. import static org.junit.Assert.assertThat
22.
23. /**
24. * @Title: PostDemo
25. * @Description: post请求
26. * @author liwen
27. * @date 2019/10/24 / 12:22
28. */
29. @RunWith(GrinderRunner)
30. class PostDemo {
31.
32. public static GTest test
33. public static HTTPRequest request
34. public static NVPair[] headers = []
35. public static NVPair[] params = []
36. public static Cookie[] cookies = []
37.
38.
39. @BeforeProcess
40. public static void beforeProcess() {
41. HTTPPluginControl.getConnectionDefaults().timeout = 6000
42. test = new GTest(1, "localhost:8888")
43. request = new HTTPRequest()
44. // Set header datas
45. List<NVPair> headerList = new ArrayList<NVPair>()
46. //POST key/value格式的params
47. headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
48. headers = headerList.toArray()
49. // Set param datas
50. List<NVPair> paramList = new ArrayList<NVPair>()
51. paramList.add(new NVPair("username", "600128"))
52. params = paramList.toArray()
53. grinder.logger.info("before process.");
54.
55. }
56.
57. @BeforeThread
58. public void beforeThread() {
59. test.record(this, "test")
60. grinder.statistics.delayReports = true;
61. grinder.logger.info("before thread.");
62. }
63.
64. @Before
65. public void before() {
66. request.setHeaders(headers)
67. cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
68. grinder.logger.info("before thread. init headers and cookies");
69.
70. }
71.
72. @Test
73. public void test() {
74. HTTPResponse result = request.GET("http://localhost:8888/findinfo", params)
75. def text = result.getText()
76.
77. grinder.logger.info(text)
78.
79. assertThat(result.statusCode, is(200))
80.
81.
82. }
83.
84. }
注意别忘记修改请求参数:
-javaagent:D:\maven\repository\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar
点击运行
结果为:
2、json请求方式
在测试前,先模拟可以发送json请求的服务端,在Controler层中增加一个方法并且使用可以解析json方法的注解为:@RequestBody具体代码为:
/**
* json请求
* @param userInfo
* @return
*/
@PostMapping("/findinfoJson")
@ResponseBody
public List<UserTable> findUserPostJson(@RequestBody UserTable userInfo) {
List<UserTable> UserInfo = userService.findinfo(userInfo);
return UserInfo;
}
通过上面请求调整即可接受json格式的请求,以下通过源码编写json类进行编写脚本;
参考代码
1. import HTTPClient.Cookie
2. import HTTPClient.CookieModule
3. import HTTPClient.HTTPResponse
4. import HTTPClient.NVPair
5. import net.grinder.plugin.http.HTTPPluginControl
6. import net.grinder.plugin.http.HTTPRequest
7. import net.grinder.script.GTest
8. import net.grinder.scriptengine.groovy.junit.GrinderRunner
9. import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
10. import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
11. import org.junit.Before
12. import org.junit.Test
13. import org.junit.runner.RunWith
14.
15. import static net.grinder.script.Grinder.grinder
16. import static org.hamcrest.Matchers.is
17.
18. // import static net.grinder.util.GrinderUtils.*
19. // You can use this if you're using nGrinder after 3.2.3
20.
21. import static org.junit.Assert.assertThat
22.
23. /**
24. * @Title: PostDemo
25. * @Description: json请求
26. * @author liwen
27. * @date 2019/10/24 / 12:22
28. */
29. @RunWith(GrinderRunner)
30. class PostDemo {
31.
32. public static GTest test
33. public static HTTPRequest request
34. public static NVPair[] headers = []
35. public static NVPair[] params = []
36. public static Cookie[] cookies = []
37.
38. @BeforeProcess
39. public static void beforeProcess() {
40. HTTPPluginControl.getConnectionDefaults().timeout = 6000
41. test = new GTest(1, "localhost:8888")
42. request = new HTTPRequest()
43. // Set header datas
44. List<NVPair> headerList = new ArrayList<NVPair>()
45. headerList.add(new NVPair("Content-Type", "application/json"))
46. headers = headerList.toArray()
47. grinder.logger.info("before process.");
48.
49. }
50.
51. @BeforeThread
52. public void beforeThread() {
53. test.record(this, "test")
54. grinder.statistics.delayReports = true;
55. grinder.logger.info("before thread.");
56. }
57.
58. @Before
59. public void before() {
60. request.setHeaders(headers)
61. cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
62. grinder.logger.info("before thread. init headers and cookies");
63.
64. }
65.
66. @Test
67. public void test() {
68. String url = "http://localhost:8888/findinfoJson"
69. String parambody = "{\"username\":\"600128\"}"
70. HTTPResponse result = request.POST(url, parambody.getBytes())
71. //显示结果
72. def text = result.getText()
73. grinder.logger.info(text)
74. assertThat(result.statusCode, is(200))
75. }
76. }
点击请求参考结果:
显示结果:
源码分析说明
HTTPResponse result = request.POST(url, parambody.getBytes())
通过点击方法的post请求可以看出源码中支持那些方式传参,:
通过源码就知道post请求怎么参数化,感兴趣的朋友可以打开源码玩一玩;
下次分享通过外部文件获取参数与脚本之间怎么关联,为最后做实战做基础工作。
总结:注意k-v与josn在传参的时候注意Content-Type的值就行;
送大家一句:
当我们遇到别人,问我们问题的时候,我们可以回答,“不知道”,但是“不知道”,可以是解决问题的开始,也可以是问题的,结束心态,智者不是全知,而是选择开始。