Loadrunner測試數(shù)據(jù)庫、SQL語句性能
Action(){
int rc;
int db_connection; // 數(shù)據(jù)庫連接
int query_result; // 查詢結(jié)果集 MYSQL_RES
char** result_row; // 查詢的數(shù)據(jù)衕
char *server = "localhost";
char *user = "root";
char *password = "123456";
char *database = "test";
int port = 3306;
int unix_socket = NULL;
int flags = 0;
// 找到libmysql.dll的所在位置.
rc = lr_load_dll("C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\libmysql.dll");
if (rc != 0) {
}
// 創(chuàng)建MySQL對象
db_connection = mysql_init(NULL);
if (db_connection == NULL) {
}
// 連接到MySQL數(shù)據(jù)庫
rc = mysql_real_connect(db_connection, server, user, password, database, port, unix_socket, flags);
if (rc == NULL) {
}
// 向數(shù)據(jù)庫插入數(shù)據(jù)
// 此處的 {ORDER_ID} 是一個參數(shù),簡單測試時可以用一個常數(shù)代替
lr_save_string (lr_eval_string("INSERT INTO test_data (order_id) VALUES ({ORDER_ID})"),"paramInsertQuery");
rc = mysql_query(db_connection, lr_eval_string("{paramInsertQuery}"));
if (rc != 0) {
}
// 從數(shù)據(jù)庫讀取一個數(shù)據(jù)并顯示
rc = mysql_query(db_connection, "SELECT order_id FROM test_data WHERE status IS FALSE LIMIT 1");
if (rc != 0) {
}
query_result = mysql_use_result(db_connection);
if (query_result == NULL) {
}
// 如果結(jié)果集包含多行數(shù)據(jù),需要多次調(diào)用 mysql_fetch_row 直到返回NULL
result_row = (char **)mysql_fetch_row(query_result);
if (result_row == NULL) {
}
// 保存參數(shù),用于刪除這行數(shù)據(jù)
lr_save_string(result_row[0], "paramOrderID");
lr_output_message("Order ID is: %s", lr_eval_string("{paramOrderID}"));
mysql_free_result(query_result);
// 在事務(wù)里更新一行數(shù)據(jù),需要用InnoDB引擎
rc = mysql_query(db_connection, "BEGIN"); //啟動事務(wù)
if (rc != 0) {
}
// 使用 "FOR UPDATE" 鎖住要更新的數(shù)據(jù)行
rc = mysql_query(db_connection, "SELECT order_id FROM test_data WHERE status IS FALSE LIMIT 1 FOR UPDATE");
if (rc != 0) {