【性能測試】:loadrunner直壓MYSQL數據庫的腳本開發

有時性能測試,會涉及到直接壓測數據庫,測試數據庫處理sql的水平,或者經過sql腳本向數據庫寫數據作鋪地數據mysql

這裏貼上一個本身用的對數據庫操做的腳本sql

一,首先要去下載一個LR壓MYSQL的一個庫文件,放到bin和include目錄下數據庫

二,在頭文件中,添加對庫的引用性能

 
 

#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 "HOSTIP"
#define MYSQLUSERNAME "USER"
#define MYSQLPASSWORD "PASSWORD"
#define MYSQLDB "DATABASE_NAME"
#define MYSQLPORT "PORT"
char chQuery[1024];
int statu=1;
int status=0;
MYSQL *Mconn;
char sql_query[256], sql_sub_query[50];
int i=0, res=0, conn_fail=0, conn_iter=0;
char response[50];// 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 = 0;
lr_end_transaction("mysql_conn", LR_FAIL); //初始化數據庫失敗斷定事務失敗
}
/*------------------------------------------------------------------------*/
do
{
/* Connect to database */
if (!mysql_real_connect(Mconn, MYSQLSERVER, MYSQLUSERNAME, MYSQLPASSWORD, MYSQLDB, atoi(MYSQLPORT), NULL, 0))
{
conn_fail = -2;
conn_iter++;
sleep(100);
}
else
conn_fail = 0;
}
while(conn_fail < 0 && conn_iter < 10);
if (conn_fail < 0)
{
lr_message("Error -2: %s", mysql_error(Mconn));
//mysql_close(Mconn);
//return -2;
statu = 0;
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 0;
} 測試



三,Action中的腳本ui

 
 

Action()
{
if(statu){ //成功鏈接數據庫後才進行執行sql的操做
lr_start_transaction("mysql_select");
sprintf(chQuery, "SELECT * from cms-member-dev.cms_member_basic where id = '1007449268564533248'");//查詢論壇用戶
//sprintf(chQuery,"UPDATE lr_test SET password = "%s" WHERE username='tom'",lr_eval_string ("{pwd}"));
/*
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!=0){ //判斷sql是否執行成功
lr_end_transaction("mysql_select",LR_FAIL);
}else{
lr_end_transaction("mysql_select",LR_PASS);
}

lr_start_transaction("mysql_insert");
sprintf(chQuery,"SELECT * from cms_member_basic where id = '1007449268564533248'");//插入數據
status = lr_mysql_query(Mconn, chQuery);
if(status!=0){ //判斷sql是否執行成功
lr_end_transaction("mysql_insert",LR_FAIL);
}else{
lr_end_transaction("mysql_insert",LR_PASS);
}
}
return 0;
} spa

 

 三 VUSER_END()中的腳本code

vuser_end()  
{  
    lr_mysql_disconnect(Mconn); //關閉數據庫鏈接  
    return 0;  
} 
相關文章
相關標籤/搜索