wps行距20磅怎么调
665
2025-03-31
前言
实现一个get 请求 demo
脚本编写
前言
实现一个get 请求 demo
脚本编写
前言
做性能测试脚本是一个实际下功夫的地方,工作中常见也就 是 key-value,json 方式比较多,那么 nGrinder 脚本咱们怎么编写以下简单介绍。
实现一个get 请求 demo
首先,通过 SpringBoot 编写一个工程实现增删改查,通过 Get 请求获取:
http://localhost:8888/findinfo?username=600128
该工程 controller 层中用最简单 Get 请求查询数据,该代码为:
@GetMapping("/findinfo") @ResponseBody public List
@ResponseBody 注解会自动转换 Json 显示到页面。该工程很简单,就不展示其他代码,大家在做练习的时候,可以找自己公司的项目或者自己写一个 demo 工程,进行练习。
接口层:
public interface UserService { List
实现层:
@Service public class UserServiceImpl implements UserService { @Override public List
数据库 Dao 层:
该层通过 Generator 插件生成
Generator 插件参考代码:
Pom.xml 配置:
maven 插件编写参考:
配置上面后点击:
运行即可就能生成数据库连接 SQL 语句。
脚本编写
打开上一节使用源码部署的工程,在介绍源码运行脚本地方新建一个脚本,参考如下代码修改成自己练习的脚本。
在 nGrinder 中新建的脚本编写如下代码:
import org.junit.FixMethodOrder 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 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 @RunWith(GrinderRunner) class PostGetDemo { public static GTest test // 定义 HTTPRequest 静态变量 request,用于发送 HTTP 请求 public static HTTPRequest request // 定义 NVPair 数组 headers ,用于存放通用的请求头数据 public static NVPair[] headers = [] // 定义 NVPair 数组 params ,用于存放请求参数数据 public static NVPair[] params = [] // 定义 Cookie 数组 cookies ,用于存放通用的 cookie 数据 public static Cookie[] cookies = [] @BeforeProcess public static void beforeProcess() { // 设置请求响应超时时间(ms) HTTPPluginControl.getConnectionDefaults().timeout = 6000 // 创建GTest对象,第一个参数1代表有多个请求/事务时的执行顺序ID,第二个参数是请求/事务的名称,会显示在summary结果中,有多个请求/事务时,要创建多个GTest对象 test = new GTest(1, "localhost:8888") //创建 HTTPRequest 对象,用于发起 HTTP 请求 request = new HTTPRequest() // Set header datas List
再次运行:
点击运行配置加上:
-javaagent:D:\maven\repository\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar
配置说明如下:
之后点击运行即可:
结果如下:
关键点需要注意这里:
List
注意头信息:
public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = []
查看源码就知道怎么传值,这里列举 cookie 源码传值说明:
通过源码查看得知如果传 cookie 需要 new cookie 实体通过构造方法进行传值入:
List
查看@BeforeThread注解下会执行 test.record方法源码如下:
/** * Instrument the supplied {@code target} object's method which has the given name. Subsequent * calls to {@code target}'s given method will be recorded against the statistics for this * {@code Test}. * 提供的具有给定名称的{@code target}对象方法。 后继的对{@code target}给定方法的调用将根据此方法的统计信息进行记录 * @param target Object to instrument. * @param methodName method name to instrument * @throws NonInstrumentableTypeException If {@code target} could not be instrumented. * @since 3.2.1 */ public final void record(Object target, String methodName) throws NonInstrumentableTypeException { if (StringUtils.isNotEmpty(context)) { record(target, new MethodNameFilter(methodName)); } }
解释:
target:指脚本对象,这里是 this;
methodNam:是需要统计的方法名,通常都是被 @Test 注释的方法。如果未配置,方法会正常执行,但是没有统计结果数据;
以下代码是可以复制出来修改的代码
import org.junit.FixMethodOrder 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 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 @RunWith(GrinderRunner) class PostGetDemo { public static GTest test // 定义 HTTPRequest 静态变量 request,用于发送 HTTP 请求 public static HTTPRequest request // 定义 NVPair 数组 headers ,用于存放通用的请求头数据 public static NVPair[] headers = [] // 定义 NVPair 数组 params ,用于存放请求参数数据 public static NVPair[] params = [] // 定义 Cookie 数组 cookies ,用于存放通用的 cookie 数据 public static Cookie[] cookies = [] @BeforeProcess public static void beforeProcess() { // 设置请求响应超时时间(ms) HTTPPluginControl.getConnectionDefaults().timeout = 6000 // 创建GTest对象,第一个参数1代表有多个请求/事务时的执行顺序ID,第二个参数是请求/事务的名称,会显示在summary结果中,有多个请求/事务时,要创建多个GTest对象 test = new GTest(1, "localhost:8888") //创建 HTTPRequest 对象,用于发起 HTTP 请求 request = new HTTPRequest() // Set header datas List
源码地址:
https://github.com/zuozewei/blog-example/blob/master/Performance-testing/01-test-tool/nGrinder/nGrinder-demo/script-sample/test-with-login/PostGetDemo.groovy
相关系列:
性能工具之 nGrinder 入门安装及使用
性能工具之 nGrinder 源码安装
性能工具之 nGrinder Get 请求脚本编写
性能工具之 nGrinder Post 请求脚本
性能工具之 nGrinder 关联脚本编写
性能工具之 nGrinder 参数化脚本编写
性能工具之 nGrinder 区域配置
数据库
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。