int rc; int db_connection; char *server = "192.168.1.100"; // 数据库的ip地址 char *user = "root"; // 数据库访问用户名 char *password = "123456"; // 密码 char *database = "t3db"; // 数据库名称 int port = 3306; // 访问端口 int unix_socket = NULL; int flags = 0;char** result_row; int query_result; char szSql[256]; int MySqlInit() {rc = lr_load_dll("libmysql.dll"); db_connection = mysql_init(NULL); if (db_connection == NULL) {lr_error_message("Insufficient memory"); lr_abort();}if(rc!=0){lr_error_message("Load MySql.dll Error!"); lr_abort();}rc = mysql_real_connect(db_connection,server, user, password, database, port, unix_socket, flags); if(rc == NULL){lr_error_message("connect mysql error! %s",mysql_error(db_connection)); mysql_close(db_connection); lr_abort();}return rc;}int MySqlUnit() {// 释放MySQL资源 mysql_close(db_connection); return 0;}int InsertValue(char* query) {rc = mysql_query(db_connection,query); if (rc != 0){lr_error_message("%s", mysql_error(db_connection)); }query = NULL;return rc;}int MySqlQuery(char* szSql) {rc = mysql_query(db_connection,szSql); if(rc != 0){lr_error_message("%s",lr_eval_string("?")); lr_error_message("%s", mysql_error(db_connection)); szSql = NULL;return -1;}query_result = mysql_use_result(db_connection); if (query_result == NULL) {lr_error_message("%s", mysql_error(db_connection)); mysql_free_result(query_result); szSql = NULL;return -2;}result_row = (char **)mysql_fetch_row(query_result); if (result_row == NULL) {lr_error_message("Did not expect the result set to be empty"); mysql_free_result(query_result); szSql = NULL;return -3;}mysql_free_result(query_result); szSql = NULL;return 0;} |
这里提供了几个公共函数,看名字大家都明白他们是干啥的。
Step3:Loadrunner里需要怎么编写写呢?
1、添加libmysql.dll到你的工程
2、把公共库添加到你的公共
3、vuser_initvuser_init(){index = 0; MySqlInit(); // 初始化数据库 return 0;} |
vuser_end(){ MySqlUnit(); // 反初始化 return 0;} |
Action(){ int resultValue; char cIndex[10]; char onceAccount[1024]; char insertQuery[22584]; index = index +1; // 组合插入数据库的sql语句 strcpy(insertQuery, "INSERT INTO `t3db`.`role`(GroupID, RoleName, Account, BaseInfo, ExtInfo, LastModify) VALUES('1', '"); strcat(insertQuery, lr_eval_string("{Account}")); strcat(insertQuery, itoa(index, cIndex, 10 )); strcat(insertQuery, "', 'q1031', '111', '111','2013-02-28 20:42:33')"); strcat(insertQuery, ";/0"); lr_start_transaction("Insert"); resultValue = InsertValue(insertQuery); // 调用插入函数 if(resultValue != 0) {lr_end_transaction("Insert",LR_FAIL); }else{lr_end_transaction("Insert",LR_PASS); }sleep(100);return 0;} |