web services协议
在LR的web services协议中提供了如下函数进行数据库的连接和执行sql语句,以实现对数据库的性能测试,也可以实现向数据库中自动生成批量数据。
lr_db_disconnect用于数据库的连接,可以通过插入该函数,通过LR的向导配置数据库连接参数并测试数据库连接是否成功,如下:
或:
连接oracle数据库并执行sql查询的测试脚本:
1 Action() 2 { 3 4 int NumRows=0; 5 int i=0; 6 7 //OLEDB方式连接数据库 8 lr_db_connect("StepName=OracleConnect", 9 "ConnectionName=db1", 10 "ConnectionString=Provider=OraOLEDB.Oracle.1;Data Source=netservicename_testbook;Password=Testbook_1;User ID=system", 11 "ConnectionType=OLEDB", 12 LAST); 13 14 //或 lr_db_connect("StepName=OracleConnect", 15 // "ConnectionName=db1", 16 // "ConnectionString=Provider=OraOLEDB.Oracle.1;Data Source=localhost:1521/testbook;Password=Testbook_1;User ID=system", 17 // "ConnectionType=OLEDB", 18 // LAST); 19 20 lr_start_transaction("PerformQuery"); 21 22 //执行查询,并保存查询出的总记录条数。lr_db_executeSQLStatement中还能执行增删该、存储过程等其他sql语句。 23 NumRows = 24 lr_db_executeSQLStatement("StepName=PerformQuery", 25 "ConnectionName=db1", 26 "SQLStatement=select * from department", 27 "DatasetName=Mydataset", 28 LAST); 29 lr_output_message("Notify:NumRows:%d",NumRows); 30 31 //提取查询出来的数据 32 while ( i < 7 ) 33 { 34 lr_db_getvalue("StepName=GetValue", 35 "DatasetName=Mydataset", 36 "Column=department_name", 37 "Row=next", 38 "OutParam=MyOutputParam", 39 LAST ); 40 lr_output_message("The value of MyOutputParam is: %s", lr_eval_string("{MyOutputParam}") ); 41 i++; 42 } 43 44 //在日志中打印内存中的数据集,默认只打印100条。lr_db_dataset_action中还能执行其他action操作 45 lr_db_dataset_action("StepName=PrintDataset", 46 "DatasetName=Mydataset", 47 "Action=PRINT", 48 LAST ); 49 50 //释放数据库连接 51 lr_db_disconnect("StepName=Disconnect", 52 "ConnectionName=db1", 53 LAST); 54 55 lr_end_transaction("PerformQuery", LR_AUTO); 56 57 58 return 0; 59 }
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数据库并执行查询的实例脚本:
1 Action() 2 { 3 //***vdf.h*** 4 #include "lrd.h" //位于LR安装目录下的include目录中的头文件 5 6 //相关变量定义 7 static LRD_INIT_INFO lnitlnfo = {LRD_INIT_INFO_EYECAT}; 8 static LRD_DEFAULT_DB_VERSION DBTypeVersion[] = {{LRD_DBTYPE_NONE,LRD_DBVERSION_NONE}}; 9 static LRD_VAR_DESC OBJECT_NAME_D1; 10 static LRD_VAR_DESC NUM ={LRD_VAR_DESC_EYECAT, 10, 32, LRD_DBTYPE_ORACLE, {1, 1, 0},DT_LONG_VARCHAR}; 11 static void FAR * OraEnv1; 12 static void FAR * OraSvc1; 13 static void FAR * OraSrv1; 14 static void FAR * OraSes1; 15 static void FAR * OraStm1; 16 static void FAR * OraDef1; 17 static unsigned long uliFetchedRows; 18 unsigned long rownum; 19 20 //***vuser_init*** 21 22 //Initializes the LRD environment 23 lrd_init(&lnitlnfo, DBTypeVersion); 24 25 //Initializes the database process environment 26 lrd_initialize_db(LRD_DBTYPE_ORACLE,3,0); 27 28 //Allocates and initializes an LRDDBI handle 29 lrd_env_init(LRD_DBTYPE_ORACLE, &OraEnv1,0,0); 30 31 //Explicitly allocates and initializes an LRDDBI Handle 32 lrd_ora8_handle_alloc(OraEnv1,SVCCTX,&OraSvc1,0); 33 34 lrd_ora8_handle_alloc(OraEnv1,SERVER,&OraSrv1,0); 35 36 lrd_ora8_handle_alloc(OraEnv1,SESSION,&OraSes1,0); 37 38 //Creates an access path to a data source for database operations 39 lrd_server_attach(OraSrv1,"netservicename_testbook",-1,0,0); //或 lrd_server_attach(OraSrv1,"localhost:1521/testbook",-1,0,0); 40 41 //Sets an attribute for an LRDDBI handle 42 lrd_ora8_attr_set_from_handle(OraSvc1,SERVER,OraSrv1,0,0); 43 44 //Sets an attribute for an LRDDBI handle 45 lrd_ora8_attr_set(OraSes1,USERNAME,"system",-1,0); 46 47 lrd_ora8_attr_set(OraSes1,PASSWORD,"Testbook_1",-1,0); 48 49 lrd_ora8_attr_set_from_handle(OraSvc1,SESSION,OraSes1,0,0); 50 51 lr_think_time(5); 52 53 //Creates and begins a user session for a server 54 lrd_session_begin(OraSvc1,OraSes1,1,0,0); 55 56 57 //***Action*** 58 59 lrd_ora8_handle_alloc(OraEnv1,STMT,&OraStm1,0); 60 61 //事务开始 62 lr_start_transaction("sqlstart"); 63 64 //Prepares a null-terminated SQL statement for execution 65 lrd_ora8_stmt(OraStm1, "select department_name from department", 1, 0, 0); 66 67 //Executes an SQL statement in Oracle 8.x 68 lrd_ora8_exec(OraSvc1, OraStm1, 0, 0,&rownum, 0, 0, 0, 0, 1); 69 70 //Binds a host variable to a column 71 lrd_ora8_bind_col(OraStm1,&OraDef1,1,&NUM,0,0); 72 73 //Saves the value of a table cell to a parameter 74 lrd_ora8_save_col(OraStm1, 1, 1, 0, "resu1"); 75 76 lrd_ora8_save_col(OraStm1, 1, 4, 0, "resu2"); 77 78 //Fetches the next row in the result set 79 lrd_ora8_fetch(OraStm1, -2, 2, &rownum, 0, 2, 0, 0); 80 81 lr_output_message("Notify:The final message is:%s", lr_eval_string("{resu1}")); 82 lr_output_message("Notify:The final message is:%s", lr_eval_string("{resu2}")); 83 84 85 86 //***vuser_end*** 87 88 //Frees an LRDDBI handle 89 lrd_handle_free(&OraStm1, 0); 90 91 //Ends a user session for a server 92 lrd_session_end(OraSvc1, OraSes1, 0, 0); 93 94 //Deletes an access path to a data source 95 lrd_server_detach(OraSrv1, 0, 0); 96 97 lrd_handle_free(&OraEnv1, 0); 98 99 lr_end_transaction("sqlstart", LR_AUTO); 100 101 return 0; 102 }
以上脚本执行的前提:执行机器上安装了oracle客户端程序并你能正常连接到oracle数据库。当然如果脚本执行机器上安装有oracle服务器程序,就不再需要安装oracle客户端程序了。一般可以先安装个PL/SQL Developer,然后确保PL/SQL Developer能正常连接到oracle数据库,脚本就可以正常连接数据库并执行了,也可以利用LR数据库连接函数lr_db_connect对话框向导中的连接测试来测试与数据库服务器的连通性,需要确保连接测试成功。