ngrinder groovy 参数化登录账号和密码(以mysql数据库为例)
2023-09-14 09:01:11 时间
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()) } } }
相关文章
- MySQL如何利用索引优化ORDER BY排序语句
- MySQL忘记root密码解决办法
- Mysql初始化root密码和允许远程访问
- mysql日志详细解析
- mysql误删root用户或者忘记root密码解决方法
- mysql常用语句
- 详解MYSQL数据库密码的加密方式及破解方法
- windows下mysql忘记root密码的解决方法
- MySql修改root密码
- EasyDSS流媒体服务器软件对数据库Sqlite3和MySQL的同时支持说明
- Java连接MySQL数据库
- MySQL时间函数from_unixtime()date_format()unix_timestamp()now()使用说明
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- [MySQL]修改root密码的4种方法(以windows为例)
- MySQL使用小技巧(二)——MySQL忘记密码怎么办
- MYsql 数据库密码忘记(Window)
- 【项目实战】MySQL中LIMIT用法
- mysql事务操作——transaction
- mysql忘记密码的三种解决方案
- MySQL----MySQL数据库忘记root用户登录密码的解决办法
- 【Deepin 20系统】解决“phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接。您应该检查配置文件中的主机、用户名和密码,并确认这些信息与 MySQL 服务器管理员所给出的信”
- MySQL安装后第一次修改密码
- MySQL 表锁
- MySQL数据库管理系统安装部署——Linux
- mysql root 修改密码 远程登陆