web services协议
在LR的web services协议中提供了如下函数进行数据库的连接和执行sql语句,以实现对数据库的性能测试,也可以实现向数据库中自动生成批量数据。
lr_db_disconnect用于数据库的连接,可以通过插入该函数,通过LR的向导配置数据库连接参数并测试数据库连接是否成功,如下:
或:
连接oracle数据库并执行sql查询的测试脚本:
Action()
{ int NumRows=;
int i=; //OLEDB方式连接数据库
lr_db_connect("StepName=OracleConnect",
"ConnectionName=db1",
"ConnectionString=Provider=OraOLEDB.Oracle.1;Data Source=netservicename_testbook;Password=Testbook_1;User ID=system",
"ConnectionType=OLEDB",
LAST); //或 lr_db_connect("StepName=OracleConnect",
// "ConnectionName=db1",
// "ConnectionString=Provider=OraOLEDB.Oracle.1;Data Source=localhost:1521/testbook;Password=Testbook_1;User ID=system",
// "ConnectionType=OLEDB",
// LAST); lr_start_transaction("PerformQuery"); //执行查询,并保存查询出的总记录条数。lr_db_executeSQLStatement中还能执行增删该、存储过程等其他sql语句。
NumRows =
lr_db_executeSQLStatement("StepName=PerformQuery",
"ConnectionName=db1",
"SQLStatement=select * from department",
"DatasetName=Mydataset",
LAST);
lr_output_message("Notify:NumRows:%d",NumRows); //提取查询出来的数据
while ( i < )
{
lr_db_getvalue("StepName=GetValue",
"DatasetName=Mydataset",
"Column=department_name",
"Row=next",
"OutParam=MyOutputParam",
LAST );
lr_output_message("The value of MyOutputParam is: %s", lr_eval_string("{MyOutputParam}") );
i++;
} //在日志中打印内存中的数据集,默认只打印100条。lr_db_dataset_action中还能执行其他action操作
lr_db_dataset_action("StepName=PrintDataset",
"DatasetName=Mydataset",
"Action=PRINT",
LAST ); //释放数据库连接
lr_db_disconnect("StepName=Disconnect",
"ConnectionName=db1",
LAST); lr_end_transaction("PerformQuery", LR_AUTO); return ;
}
lr_db_executeSQLStatement函数执行存储过程实例:
lr_db_executeSQLStatement("StepName=StartDeal",
"ConnectionName=XXXOrderDB",
"SQLStatement=exec XXXorderdb..sp1_XXX_firstdealorder"
"@orderid='{ID}',"
"@operator='mazj',"
"@ProcessType = 'NOR',"
"@CorpConfirmType = '',"
"@CorporationID = NULL,"
"@BOS=NULL",
"DatasetName=testDataset",
LAST);
Oracle(2-Tier)协议
Oracle(2-Tier)协议用于C/S结构的两层架构进行数据库的测试,可以录制PL/SQL Developer或sqlplus工具连接数据库执行数据库操作脚本,各种变量的定义置于vdf.h文件中,,数据库的登陆退出分别置于vuser_init和vuser_end中,数据库的操作置于Action中。
连接oracle数据库并执行查询的实例脚本:
Action()
{
//***vdf.h***
#include "lrd.h" //位于LR安装目录下的include目录中的头文件 //相关变量定义
static LRD_INIT_INFO lnitlnfo = {LRD_INIT_INFO_EYECAT};
static LRD_DEFAULT_DB_VERSION DBTypeVersion[] = {{LRD_DBTYPE_NONE,LRD_DBVERSION_NONE}};
static LRD_VAR_DESC OBJECT_NAME_D1;
static LRD_VAR_DESC NUM ={LRD_VAR_DESC_EYECAT, , , LRD_DBTYPE_ORACLE, {, , },DT_LONG_VARCHAR};
static void FAR * OraEnv1;
static void FAR * OraSvc1;
static void FAR * OraSrv1;
static void FAR * OraSes1;
static void FAR * OraStm1;
static void FAR * OraDef1;
static unsigned long uliFetchedRows;
unsigned long rownum; //***vuser_init*** //Initializes the LRD environment
lrd_init(&lnitlnfo, DBTypeVersion); //Initializes the database process environment
lrd_initialize_db(LRD_DBTYPE_ORACLE,,); //Allocates and initializes an LRDDBI handle
lrd_env_init(LRD_DBTYPE_ORACLE, &OraEnv1,,); //Explicitly allocates and initializes an LRDDBI Handle
lrd_ora8_handle_alloc(OraEnv1,SVCCTX,&OraSvc1,); lrd_ora8_handle_alloc(OraEnv1,SERVER,&OraSrv1,); lrd_ora8_handle_alloc(OraEnv1,SESSION,&OraSes1,); //Creates an access path to a data source for database operations
lrd_server_attach(OraSrv1,"netservicename_testbook",-,,); //或 lrd_server_attach(OraSrv1,"localhost:1521/testbook",-1,0,0); //Sets an attribute for an LRDDBI handle
lrd_ora8_attr_set_from_handle(OraSvc1,SERVER,OraSrv1,,); //Sets an attribute for an LRDDBI handle
lrd_ora8_attr_set(OraSes1,USERNAME,"system",-,); lrd_ora8_attr_set(OraSes1,PASSWORD,"Testbook_1",-,); lrd_ora8_attr_set_from_handle(OraSvc1,SESSION,OraSes1,,); lr_think_time(); //Creates and begins a user session for a server
lrd_session_begin(OraSvc1,OraSes1,,,); //***Action*** lrd_ora8_handle_alloc(OraEnv1,STMT,&OraStm1,); //事务开始
lr_start_transaction("sqlstart"); //Prepares a null-terminated SQL statement for execution
lrd_ora8_stmt(OraStm1, "select department_name from department", , , ); //Executes an SQL statement in Oracle 8.x
lrd_ora8_exec(OraSvc1, OraStm1, , ,&rownum, , , , , ); //Binds a host variable to a column
lrd_ora8_bind_col(OraStm1,&OraDef1,,&NUM,,); //Saves the value of a table cell to a parameter
lrd_ora8_save_col(OraStm1, , , , "resu1"); lrd_ora8_save_col(OraStm1, , , , "resu2"); //Fetches the next row in the result set
lrd_ora8_fetch(OraStm1, -, , &rownum, , , , ); lr_output_message("Notify:The final message is:%s", lr_eval_string("{resu1}"));
lr_output_message("Notify:The final message is:%s", lr_eval_string("{resu2}")); //***vuser_end*** //Frees an LRDDBI handle
lrd_handle_free(&OraStm1, ); //Ends a user session for a server
lrd_session_end(OraSvc1, OraSes1, , ); //Deletes an access path to a data source
lrd_server_detach(OraSrv1, , ); lrd_handle_free(&OraEnv1, ); lr_end_transaction("sqlstart", LR_AUTO); return ;
}
以上脚本执行的前提:执行机器上安装了oracle客户端程序并你能正常连接到oracle数据库。当然如果脚本执行机器上安装有oracle服务器程序,就不再需要安装oracle客户端程序了。一般可以先安装个PL/SQL Developer,然后确保PL/SQL Developer能正常连接到oracle数据库,脚本就可以正常连接数据库并执行了,也可以利用LR数据库连接函数lr_db_connect对话框向导中的连接测试来测试与数据库服务器的连通性,需要确保连接测试成功。