loadrunner-mysql脚本(odbc协议)

来源:互联网 发布:多核优化好的网游 编辑:程序博客网 时间:2024/05/17 09:31

借鉴了网上的很多攻略。。感谢前人指路。

使用如下:

loadunner做mysql的性能测试脚本有两种方式,1、使用c协议做,在dll文件中封装mysql的连接方法,通过脚本调用dll文件实现。2、使用ODBC协议,通过ODBC连接mysql实现。

本帖将使用ODBC协议实现对musql的压测过程。

1、下载并安装ODBCf  for mysql的工具。

可以百度:mysql-connector-odbc-win32.zip或者到地址:http://download.csdn.net/detail/shen1936/8066789

2、在安装过odbc工具后,会在控制面板中显示ODBC的图标(可惜我的没有),通过在桌面建立快捷方式的方法来建立。

到桌面,新建一个快捷方式,目标位置:%SystemRoot%\system32\odbcad32.exe。 

              

3、设置ODBC的连接:

打开odbc,选择添加

然后选择mysql的连接



设置好连接后,就可以使用loadrunner脚本进行脚本开发。

脚本打开后,直接在Action中书写脚本就可以了。

脚本如下:

#include "lrd.h"Action(){static LRD_INIT_INFO InitInfo = {LRD_INIT_INFO_EYECAT};static LRD_DEFAULT_DB_VERSION DBTypeVersion[] ={  {LRD_DBTYPE_ODBC, LRD_DBVERSION_ODBC_30},  {LRD_DBTYPE_NONE, LRD_DBVERSION_NONE}};static LRD_CONTEXT FAR * Ctx1;static LRD_CONNECTION FAR * Con1;static LRD_CURSOR FAR *     Csr1;//上面的定义的代码如果录制脚本,在vdf.h中就有定义,同时还有一些其他文件//如果手写脚本,则需要手工添加,主要是定义各种变量//查询行数unsigned long count=0;//初始lrd_init(&InitInfo, DBTypeVersion);//打开上下文lrd_open_context(&Ctx1, LRD_DBTYPE_ODBC, 0, 0, 0);//申请连接的内存lrd_alloc_connection(&Con1, LRD_DBTYPE_ODBC, Ctx1, 0 /*Unused*/, 0);//打开连接,注意DRIVER就是上面安装的lrd_open_connection(&Con1, LRD_DBTYPE_ODBC, "", "", "test", "", Ctx1, 0, 0);//打开游标lrd_open_cursor(&Csr1, Con1, 0);//Sql语句,注意1代表的意思是,立马执行lr_rendezvous("1111111");  //集合点lr_start_transaction("查询");//统计事务    lrd_stmt(Csr1, "select * from tb_refund", -1, 1, 0 /*None*/, 0);//被测语句lr_end_transaction("查询", LR_AUTO);//统计行数到count变量中lrd_row_count(Csr1, &count, 0); //打印消息lr_message("count= %d",count);//先关闭游标lrd_close_cursor(&Csr1, 0);//再关闭连接lrd_close_connection(&Con1, 0, 0);//释放连接,和alloc相呼应,否则有内存泄露lrd_free_connection(&Con1, 0 /*Unused*/, 0);//再关闭上下文lrd_close_context(&Ctx1, 0, 0);//完毕,返回0return 0;}

特别说明:

lrd_open_connection(&Con1, LRD_DBTYPE_ODBC, "", "", "test", "", Ctx1, 0, 0);

本语句中的test为在odbc中设置的ODBC连接的名字 


压测结果如下:

Virtual User Script started at : 2014-10-22 10:33:13
Starting action vuser_init.
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(25): lrd_open_connection: User="", Server="test"
Action.c(30): Rendezvous 1111111
Action.c(32): Notify: Transaction "查询" started.
Action.c(34): lrd_stmt: select * from tb_refund
Action.c(36): Notify: Transaction "查询" ended with "Pass" status (Duration: 0.0391).
count= 5197
Action.c(45): lrd_close_connection: User="", Server="test"
Ending action Action.
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.


0 0