Loadrunner測試數(shù)據(jù)庫性能測試SQL語句性能的腳本例子_第1頁
Loadrunner測試數(shù)據(jù)庫性能測試SQL語句性能的腳本例子_第2頁
Loadrunner測試數(shù)據(jù)庫性能測試SQL語句性能的腳本例子_第3頁
Loadrunner測試數(shù)據(jù)庫性能測試SQL語句性能的腳本例子_第4頁
Loadrunner測試數(shù)據(jù)庫性能測試SQL語句性能的腳本例子_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、此代碼為Loadrunner 8 通過C API類型的Vuser 測試MySQL性能,或者測試sql語句性能的腳本。這東西很少有人用,網(wǎng)上資料很少,一般測試B/S的很多。 CODE:view plaincopy to clipboardprint?/*需要的表結(jié)構(gòu)如下 CREATE TABLE test_data ( order_id BIGINT UNSIGNED NOT NULL COMMENT Order numbers. Must be unique., status BOOL NOT NULL DEFAULT 0 COMMENT Whether data has been used

2、or not. A value of 0 means FALSE., date_used DATETIME NULL COMMENT Date/time that the data was used., UNIQUE ( order_id ) ) ENGINE = innodb COMMENT = LoadRunner test data; */ Action()int rc; int db_connection; / 數(shù)據(jù)庫連接int query_result; / 查詢結(jié)果集 MYSQL_RESchar* result_row; / 查詢的數(shù)據(jù)衕 char *server = localh

3、ost;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 FilesMySQLMySQL Server 5.1binlibmysql.dll);if (rc != 0) lr_error_message(Could not load libmysql.dll); lr_abort(); / 創(chuàng)建MySQL對象db

4、_connection = mysql_init(NULL);if (db_connection = NULL) lr_error_message(Insufficient memory); lr_abort(); / 連接到MySQL數(shù)據(jù)庫rc = mysql_real_connect(db_connection, server, user, password, database, port, unix_socket, flags);if (rc = NULL) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_c

5、onnection); lr_abort(); / 向數(shù)據(jù)庫插入數(shù)據(jù)/ 此處的 ORDER_ID 是一個(gè)參數(shù),簡單測試時(shí)可以用一個(gè)常數(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) lr_error_message(%s, mysql_error(db_connection); mys

6、ql_close(db_connection); lr_abort(); / 從數(shù)據(jù)庫讀取一個(gè)數(shù)據(jù)并顯示rc = mysql_query(db_connection, SELECT order_id FROM test_data WHERE status IS FALSE LIMIT 1);if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();query_result = mysql_use_result(db_connection);if (qu

7、ery_result = NULL) lr_error_message(%s, mysql_error(db_connection); mysql_free_result(query_result); mysql_close(db_connection); lr_abort();/ 如果結(jié)果集包含多行數(shù)據(jù),需要多次調(diào)用 mysql_fetch_row 直到返回NULLresult_row = (char *)mysql_fetch_row(query_result); if (result_row = NULL) lr_error_message(Did not expect the resu

8、lt set to be empty); mysql_free_result(query_result); mysql_close(db_connection); lr_abort();/ 保存參數(shù),用于刪除這行數(shù)據(jù)lr_save_string(result_row0, 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_connectio

9、n, BEGIN); /啟動(dòng)事務(wù)if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();/ 使用 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) lr_error_message(%s, mysql_error(db_con

10、nection); mysql_close(db_connection); lr_abort();query_result = mysql_use_result(db_connection);if (query_result = NULL) lr_error_message(%s, mysql_error(db_connection); mysql_free_result(query_result); mysql_close(db_connection); lr_abort();result_row = (char *)mysql_fetch_row(query_result); if (re

11、sult_row = NULL) lr_error_message(沒有查詢到結(jié)果); mysql_free_result(query_result); mysql_close(db_connection); lr_abort();lr_save_string(result_row0, paramOrderID);lr_output_message(Order ID is: %s, lr_eval_string(paramOrderID);mysql_free_result(query_result);lr_save_string(lr_eval_string(UPDATE test_data

12、 SET status=TRUE, date_used=NOW() WHERE order_id=paramOrderID),paramUpdateQuery);rc = mysql_query(db_connection, lr_eval_string(paramUpdateQuery);if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();rc = mysql_query(db_connection, COMMIT); / 提交事務(wù)if (rc

13、 != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort(); / 再次查找數(shù)據(jù),應(yīng)該為空了,因?yàn)榍懊娴氖聞?wù)更新了標(biāo)志rc = mysql_query(db_connection, SELECT order_id FROM test_data WHERE status IS FALSE LIMIT 1);if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connec

14、tion); lr_abort();query_result = mysql_use_result(db_connection);if (query_result = NULL) lr_error_message(%s, mysql_error(db_connection); mysql_free_result(query_result); mysql_close(db_connection); lr_abort();result_row = (char *)mysql_fetch_row(query_result);if (result_row = NULL) lr_output_messa

15、ge(Result set is empty as expected); mysql_free_result(query_result); else lr_error_message(Did not expect the result set to contain any rows); mysql_free_result(query_result); mysql_close(db_connection); lr_abort(); / 刪除數(shù)據(jù)lr_save_string(lr_eval_string(DELETE FROM test_data WHERE order_id = paramOrd

16、erID),paramDeleteQuery);rc = mysql_query(db_connection, lr_eval_string(paramDeleteQuery);if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort(); / 釋放MySQL資源mysql_close(db_connection); return 0; /*需要的表結(jié)構(gòu)如下CREATE TABLE test_data (order_id BIGINT UNSIGNED N

17、OT NULL COMMENT Order numbers. Must be unique.,status BOOL NOT NULL DEFAULT 0 COMMENT Whether data has been used or not. A value of 0 means FALSE.,date_used DATETIME NULL COMMENT Date/time that the data was used.,UNIQUE ( order_id) ENGINE = innodb COMMENT = LoadRunner test data;*/Action()int rc;int

18、db_connection; / 數(shù)據(jù)庫連接int query_result; / 查詢結(jié)果集 MYSQL_RESchar* 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 FilesMySQLMySQL Server 5.1

19、binlibmysql.dll);if (rc != 0) lr_error_message(Could not load libmysql.dll); lr_abort();/ 創(chuàng)建MySQL對象db_connection = mysql_init(NULL);if (db_connection = NULL) lr_error_message(Insufficient memory); lr_abort();/ 連接到MySQL數(shù)據(jù)庫rc = mysql_real_connect(db_connection, server, user, password, database, port,

20、unix_socket, flags);if (rc = NULL) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();/ 向數(shù)據(jù)庫插入數(shù)據(jù)/ 此處的 ORDER_ID 是一個(gè)參數(shù),簡單測試時(shí)可以用一個(gè)常數(shù)代替lr_save_string (lr_eval_string(INSERT INTO test_data (order_id) VALUES (ORDER_ID),paramInsertQuery);rc = mysql_query(db_connection,

21、lr_eval_string(paramInsertQuery);if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();/ 從數(shù)據(jù)庫讀取一個(gè)數(shù)據(jù)并顯示rc = mysql_query(db_connection, SELECT order_id FROM test_data WHERE status IS FALSE LIMIT 1);if (rc != 0) lr_error_message(%s, mysql_error(db_connecti

22、on); mysql_close(db_connection); lr_abort();query_result = mysql_use_result(db_connection);if (query_result = NULL) lr_error_message(%s, mysql_error(db_connection); mysql_free_result(query_result); mysql_close(db_connection); lr_abort();/ 如果結(jié)果集包含多行數(shù)據(jù),需要多次調(diào)用 mysql_fetch_row 直到返回NULLresult_row = (char

23、 *)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); mysql_close(db_connection); lr_abort();/ 保存參數(shù),用于刪除這行數(shù)據(jù)lr_save_string(result_row0, paramOrderID);lr_output_message(Order ID is: %s, lr_eval_string(para

24、mOrderID);mysql_free_result(query_result);/ 在事務(wù)里更新一行數(shù)據(jù),需要用InnoDB引擎rc = mysql_query(db_connection, BEGIN); /啟動(dòng)事務(wù)if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();/ 使用 FOR UPDATE 鎖住要更新的數(shù)據(jù)行rc = mysql_query(db_connection, SELECT order_id FROM test_data

25、WHERE status IS FALSE LIMIT 1 FOR UPDATE);if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();query_result = mysql_use_result(db_connection);if (query_result = NULL) lr_error_message(%s, mysql_error(db_connection); mysql_free_result(query_result); mys

26、ql_close(db_connection); lr_abort();result_row = (char *)mysql_fetch_row(query_result);if (result_row = NULL) lr_error_message(沒有查詢到結(jié)果); mysql_free_result(query_result); mysql_close(db_connection); lr_abort();lr_save_string(result_row0, paramOrderID);lr_output_message(Order ID is: %s, lr_eval_string

27、(paramOrderID);mysql_free_result(query_result);lr_save_string(lr_eval_string(UPDATE test_data SET status=TRUE, date_used=NOW() WHERE order_id=paramOrderID),paramUpdateQuery);rc = mysql_query(db_connection, lr_eval_string(paramUpdateQuery);if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();rc = mysql_query(db_connection, COMMIT); / 提交事務(wù)if (rc != 0) lr_error_message(%s, mysql_error(db_connection); mysql_close(db_connection); lr_abort();/ 再次查找數(shù)據(jù),應(yīng)該為空了,因?yàn)榍懊娴氖聞?wù)更新了標(biāo)志rc = mysql_query(db_connection, SELECT order_id FROM test_data WHERE status IS FA

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論