LoadRunner脚本开发:操作数据库(六)

1|0一. 步骤


1. 下载MySQL loadrunner libraries

http://files.cnblogs.com/files/xiaoxitest/MySQL_LoadRunner_libraries.zip

2. 解压zip包,把其中bin、include文件夹下的文件拷贝到loadrunner的安装路径对应的文件夹中

C:\Program Files(x86)\HP\LoadRunner\bin

C:\Program Files(x86)\HP\LoadRunner\include

3. 准备mysql数据库信息

新建数据库demo,字符集必须是utf8mb4,排序规则可以随意填

使用sql语句建表 lr_user

DROP TABLE IF EXISTS `lr_user`; CREATE TABLE `lr_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `mobile` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `pwd` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `dtime` datetime DEFAULT NULL, PRIMARY KEY(`id`) ) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

4. 启动loadrunner,选择web-http/html协议,创建脚本

5. 编写vuser_init的脚本

引入头文件:#include "Ptt_Mysql.h",当然也可以在global.h文件中引入

定义宏(也就是全局变量):#define 大写的变量名 变量值,当然也可以在global.h文件中定义。注意不要以分号结束,要放在action外

#include "Ptt_Mysql.h" //引用,也可以写在global.h中 #define MYSQLSERVER "192.168.0.105" //宏定义,定义一个全局变量 #define MYSQLPORT "3306" #define MYSQLUSERNAME "root" #define MYSQLPASSWORD "pertest" #define MYSQLDB "demo" MYSQL *Mconn; //定义数据库接收类型 vuser_init() { lr_load_dll("libmysql.dll"); //加载文件 Mconn = lr_mysql_connect(MYSQLSERVER, MYSQLUSERNAME, MYSQLPASSWORD, MYSQLDB, atoi(MYSQLPORT)); //创建数据库连接 return 0; }

6. 编写vuser_end的脚本

断开数据库连接

vuser_end() { lr_mysql_disconnect(Mconn); return 0; }

 

7. 编写action的脚本

(1) 查询脚本

Action() { char sqlQuery[1024]; //1024是字符长度 int count; //查询脚本:SQL select脚本,把脚本字符串存储到sqlQuery中 sprintf(sqlQuery, "select * from lr_user;"); count = lr_mysql_query(Mconn, sqlQuery); //count=0,说明执行成功了。不管表里有没有数据 if(count == 0) { lr_output_message("============脚本执行成功==========="); lr_output_message("========count=%d", count); } else { lr_output_message("============脚本执行失败==========="); } return 0; }

运行结果:

Virtual User Script started at : 2020/4/3 22:30:14 Starting action vuser_init. Web Turbo Replay of LoadRunner 12.0.0 for Windows 2008 R2; build 2739 (Nov 30 2014 23:13:05) [MsgId: MMSG-27143] Run mode: HTML [MsgId: MMSG-26993] Run-Time Settings file: "C:\Users\Administrator\Documents\VuGen\Scripts\WebHttpHtml6\\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Maximum number of concurrent connections per server: 6 [MsgId: MMSG-26989] Starting action Action. Action.c(36): ============脚本执行成功=========== Action.c(37): ========count=0 Ending action Action. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.

 

(2) 查询有多少条记录

注意这里的返回结果类似于表格一行一列,里面的值为数据库的总记录条数:2,因此使用row[0][0].cell获取第一行第一列的值

  第一列
第一行 2

 

 

 

 

Action() { char sqlQuery[1024]; //1024是字符长度 int count; //查询有多少个数据 sprintf(sqlQuery, "select count(0) from lr_user;"); count = lr_mysql_query(Mconn, sqlQuery); //row[列号][行号] lr_save_string(row[0][0].cell, "value"); lr_output_message(lr_eval_string("{value}")); //count=0,说明执行成功了。不管表里有没有数据 if(count == 0) { lr_output_message("============脚本执行成功==========="); lr_output_message("========count=%d", count); } else { lr_output_message("============脚本执行失败==========="); } return 0; }

返回结果

Starting action Action. Action.c(30): Notify: Saving Parameter "value = 2". Action.c(31): Notify: Parameter Substitution: parameter "value" = "2" Action.c(31): 2 Action.c(37): ============脚本执行成功=========== Action.c(38): ========count=0 Ending action Action.

(3) 查询某个固定的值

现在数据库已经有两条数据了,如果想要查询第一行第二列的值,可以这样写。如果没有拿到值,将会返回空字符串

Action() { char sqlQuery[1024]; //1024是字符长度 int count; //查询脚本:SQL select脚本,把脚本字符串存储到sqlQuery中 sprintf(sqlQuery, "select * from lr_user;"); count = lr_mysql_query(Mconn, sqlQuery); //row[列号][行号] lr_save_string(row[1][0].cell, "value"); //第一个[]里为0时表示id,为1时表示手机号 lr_output_message(lr_eval_string("{value}")); //count=0,说明执行成功了。不管表里有没有数据 if(count == 0) { lr_output_message("============脚本执行成功==========="); lr_output_message("========count=%d", count); } else { lr_output_message("============脚本执行失败==========="); } return 0; }

运行结果

Starting action Action. Action.c(30): Notify: Saving Parameter "value = 13524012256". Action.c(31): Notify: Parameter Substitution: parameter "value" = "13524012256" Action.c(31): 13524012256 Action.c(37): ============脚本执行成功=========== Action.c(38): ========count=0 Ending action Action.

(4) 插入一条数据

Action() { char sqlQuery[1024]; //1024是字符长度 int count; //插入数据 sprintf(sqlQuery, "INSERT INTO lr_user(`mobile`, `pwd`, `dtime`) VALUES ('13500023456', '123456', NOW());"); count = lr_mysql_query(Mconn, sqlQuery); //count=0,说明执行成功了。不管表里有没有数据 if(count == 0) { lr_output_message("============脚本执行成功==========="); lr_output_message("========count=%d", count); } else { lr_output_message("============脚本执行失败==========="); } return 0; }

 

(5) 参数化插入数据

Action() { char sqlQuery[1024]; //1024是字符长度 int count; //参数化,插入脚本的手机号 sprintf(sqlQuery, "INSERT INTO lr_user(`mobile`, `pwd`, `dtime`) VALUES ('%s', '123456', NOW());", lr_eval_string("135{phone}")); count = lr_mysql_query(Mconn, sqlQuery); //count=0,说明执行成功了。不管表里有没有数据 if(count == 0) { lr_output_message("============脚本执行成功==========="); lr_output_message("========count=%d", count); } else { lr_output_message("============脚本执行失败==========="); } return 0; }

 

(6) 加密密码

Action() { char sqlQuery[1024]; //1024是字符长度 int count; //加密密码 lr_save_string(Change_to_Md5("123456"), "pwd"); sprintf(sqlQuery, "INSERT INTO lr_user(`mobile`, `pwd`, `dtime`) VALUES ('%s', '%s', NOW());", lr_eval_string("135{phone}"), lr_eval_string("{pwd}")); count = lr_mysql_query(Mconn, sqlQuery); //count=0,说明执行成功了。不管表里有没有数据 if(count == 0) { lr_output_message("============脚本执行成功==========="); lr_output_message("========count=%d", count); } else { lr_output_message("============脚本执行失败==========="); } return 0; }

 

(7) 批量造数据

有两种办法,一种是按F4设置迭代次数,另一种是使用Controller设置虚拟用户数

打开Controller的Design,分别设置Start Vusers,Duration和Stop Vusers。注意批量造数据脚本用的是上面加密密码的脚本

运行后查看数据库的数据总数

在Controller的导航栏Results下点击Analyze Results,可以看到结果分析

如果想要添加图标,还可以点击Summary Report ==》Add New Item ==》Add New Graph

 

(8) 删除数据 

Action() { char sqlQuery[1024]; //1024是字符长度 int count; //删除数据 sprintf(sqlQuery, "DELETE FROM lr_user WHERE mobile = %s;", "13508518632"); count = lr_mysql_query(Mconn, sqlQuery); //count=0,说明执行成功了。不管表里有没有数据 if(count == 0) { lr_output_message("============脚本执行成功==========="); lr_output_message("========count=%d", count); } else { lr_output_message("============脚本执行失败==========="); } return 0; }

 


__EOF__

本文作者cnhkzyy
本文链接https://www.cnblogs.com/my_captain/p/12629609.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   cnhkzyy  阅读(499)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示