性能测试工具操作数据库-Loadrunner与Mysql

1、库文件下载地址:http://files.cnblogs.com/files/xiaoxitest/MySQL_LoadRunner_libraries.zip

分别库文件和代码添加到Loadrunner bin目录和include目录下

2、vuser_init文件添加代码:copy

#include "Ptt_Mysql.h"

#include "mysql.h"

#include "mysql_com.h"

#include "mysql_time.h"

#include "mysql_version.h"

#include "typelib.h"

#include "my_list.h"

#include "my_alloc.h"

#define MYSQLSERVER "172.16.1.220"

#define MYSQLUSERNAME "dbuser"

#define MYSQLPASSWORD "1qaz@wsx"

#define MYSQLDB "sroa_lyt"

#define MYSQLPORT "3309"

charchQuery[124];

intstatu=1;

intstatus=;

MYSQL*Mconn;

charsql_query[256], sql_sub_query[5];

inti=, res=, conn_fail=, conn_iter=;

charresponse[5];// to go

vuser_init()

{

lr_load_dll ("libmysql.dll");

lr_start_transaction("mysql_conn");//设置一个连接数据库的事务

/*------------------------------------------------------------------------*/

/* Initialise MySQL */

if(!(Mconn = mysql_init(NULL)))

{

lr_message("Error -1: Cannot initialize MySQL - %s", mysql_error(Mconn));

//return -1;

statu =;

lr_end_transaction("mysql_conn", LR_FAIL);//初始化数据库失败判定事务失败

}

/*------------------------------------------------------------------------*/

do

{

/* Connect to database */

if(!mysql_real_connect(Mconn, MYSQLSERVER, MYSQLUSERNAME, MYSQLPASSWORD, MYSQLDB, atoi(MYSQLPORT),NULL,))

{

conn_fail = -2;

conn_iter++;

sleep(1);

}

else

conn_fail =;

}

while(conn_fail

if(conn_fail

{

lr_message("Error -2: %s", mysql_error(Mconn));

//mysql_close(Mconn);

//return -2;

statu =;

lr_end_transaction("mysql_conn", LR_FAIL);//连接数据库失败判定事务失败

}

else

{

//lr_message("MySql - Good Connection");

//mysql_close(Mconn);

statu =1;

lr_end_transaction("mysql_conn", LR_PASS);//连接数据库成功,事务通过。

return;

}

3、Action文件添加代码copy

Action()

{

if(statu){//成功连接数据库后才进行执行sql的操作

lr_start_transaction("mysql_select");

sprintf(chQuery,"SELECT id, lb, title, nr, attachment_id, attachment_name, pic_name, fbsj, fbbm, fbr, vcount FROM sroa_lyt.oa_news;");//查询论坛用户

//sprintf(chQuery,"UPDATE lr_test SET password = "%s" WHERE username='tom'",lr_eval_string (""));

/*

sprintf(chQuery,"update pre_ucenter_members set username='dd' where username='qq' ");//更改用户名qq为dd

sprintf(chQuery,"delete from pre_ucenter_members ORDER BY uid desc LIMIT 1");//删除最后添加的一条数据

*/

status = lr_mysql_query(Mconn, chQuery);

if(status!=){//判断sql是否执行成功

lr_end_transaction("mysql_select",LR_FAIL);

}else{

lr_end_transaction("mysql_select",LR_PASS);

}

lr_start_transaction("mysql_insert");

sprintf(chQuery,"insert into oa_news(id, lb, title, nr, attachment_id, attachment_name, pic_name, fbsj, fbbm, fbr, vcount) values(9,9,'qq@qq.com','test','','','teste','',6,'test',0)");//插入数据

status = lr_mysql_query(Mconn, chQuery);

if(status!=){//判断sql是否执行成功

lr_end_transaction("mysql_insert",LR_FAIL);

}else{

lr_end_transaction("mysql_insert",LR_PASS);

}

}

return;

}

4、vuser_end文件添加代码:copy

vuser_end()

{

lr_mysql_disconnect(Mconn);//关闭数据库连接

return;

}

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180112B0MQFZ00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券