Loadrunner測試數據庫性能,測試SQL語句的腳本例子

    Loadrunner與SQL Server的操做能夠經過錄制的方式來實現,但本文仍是經過直接調用loadrunner自己的function來實現sql語句的操做,mysql

主要用到的是lr_db_connect和lr_db_executeSQLStatement兩個函數.  web

一、腳本協議:選擇web servicesql

在函數lr_db_connect在協議選擇的時候只能選擇Web Services協議。
二、腳本,以測試MySQL爲例:數據庫

/*須要的表結構以下socket

CREATE TABLE `test_data` (ide

  `order_id` BIGINT UNSIGNED NOT 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.',fetch

  UNIQUE (unix

    `order_id`

  )

) ENGINE = innodb COMMENT = 'LoadRunner test data';

*/

Action()

{

  int rc;

  int db_connection; // 數據庫鏈接

  int query_result; // 查詢結果集 MYSQL_RES

  char** result_row; // 查詢的數據衕

 

  char *server = "localhost";

  char *user = "root";

  char *password = "123456789";

  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) {

    lr_error_message("Could not load libmysql.dll");

    lr_abort();

  }

 

  // 建立MySQL對象

  db_connection = mysql_init(NULL);

  if (db_connection == NULL) {

    lr_error_message("Insufficient memory");

    lr_abort();

  }

 

  // 鏈接到MySQL數據庫

  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_connection);

    lr_abort();

  }

  // 向數據庫插入數據

  // 此處的 {ORDER_ID} 是一個參數,簡單測試時能夠用一個常數代替

  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));

    mysql_close(db_connection);

    lr_abort();

  }

  // 從數據庫讀取一個數據並顯示

  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 (query_result == NULL) {

    lr_error_message("%s", mysql_error(db_connection));

    mysql_free_result(query_result);

    mysql_close(db_connection);

    lr_abort();

  }

  // 若是結果集包含多行數據,須要屢次調用 mysql_fetch_row 直到返回NULL

  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);

    mysql_close(db_connection);

    lr_abort();

  }

  // 保存參數,用於刪除這行數據

  lr_save_string(result_row[0], "paramOrderID");

  lr_output_message("Order ID is: %s", lr_eval_string("{paramOrderID}"));

  mysql_free_result(query_result);

 

  // 在事務裏更新一行數據,須要用InnoDB引擎

  rc = mysql_query(db_connection, "BEGIN"); //啓動事務

  if (rc != 0) {

    lr_error_message("%s", mysql_error(db_connection));

    mysql_close(db_connection);

    lr_abort();

  }

  // 使用 "FOR UPDATE" 鎖住要更新的數據行

  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_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);

    mysql_close(db_connection);

    lr_abort();

  }

  result_row = (char **)mysql_fetch_row(query_result);

  if (result_row == NULL) {

    lr_error_message("沒有查詢到結果");

    mysql_free_result(query_result);

    mysql_close(db_connection);

    lr_abort();

  }

  lr_save_string(result_row[0], "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 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"); // 提交事務

  if (rc != 0) {

    lr_error_message("%s", mysql_error(db_connection));

    mysql_close(db_connection);

    lr_abort();

  }

 

  // 再次查找數據,應該爲空了,由於前面的事務更新了標誌

  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 (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_message("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();

  }

  // 刪除數據

  lr_save_string(lr_eval_string("DELETE FROM test_data WHERE order_id = '{paramOrderID}'"),"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;

}

2.sql和OLEDB的例子,ConnectionType=OLEDB時ConnectionString後面必須指定Provider屬性,lr_db_connect中

的ConnectionName能夠隨意指定,可是lr_db_executeSQLStatement中的ConnectionName必須和其保持一致。

Action()

{

       int NumRows=0;

       double times=0;

       int i=1;

       //創建鏈接,ConnectionType=SQL

       lr_db_connect("StepName=DatabaseConnection",

              "ConnectionString=Data Source=172.16.1.215;Initial Catalog=LayIM;Persist Security Info=True;User ID=sa;Password=sa",

              "ConnectionName=MyCon",

              "ConnectionType=SQL",

              LAST );

       /*創建鏈接,ConnectionType=OLEDB

    lr_db_connect("StepName=Connect",

              "ConnectionString=Provider=SQLOLEDB;Data Source=.;Initial Catalog=my_test;User Id=sa;Password=123456",

              "ConnectionName=MyCon",

              "ConnectionType=OLEDB", //ConnectionType=SQL時,ConnectionString不能夠帶有Provider;ConnectionType=OLEDB時,必須帶Provider

              LAST);

    */

       //執行SQL

       lr_start_transaction("SQL查詢");

       NumRows = lr_db_executeSQLStatement("StepName=PerformQuery",

              "ConnectionName=MyCon",

              // 數據庫語句

              "SQLStatement=SELECT * FROM layim_user ORDER BY id DESC ",

              "DatasetName=MyDataSet",

              LAST );

       times=lr_get_transaction_duration("SQL查詢");

       lr_end_transaction("SQL查詢", LR_AUTO);

       lr_output_message("The query returned %d rows.", NumRows);

       lr_output_message("SQL查詢語句執行時間 %f ", times);

       //打印查詢結果

       /*lr_db_dataset_action("StepName=PrintDataset",

           "DatasetName=MyDataSet",

           "Action=PRINT",

           LAST );*/

       //獲取當前第一條記錄

       lr_db_getvalue("StepName=GetValue",

              "DatasetName=MyDataSet",

              "Column=nickname",

              "Row=current",

              "OutParam=MyOutputParam",

              LAST );

       // 輸出當前查詢記錄

       lr_output_message("The value is: %s", lr_eval_string("{MyOutputParam}") );

 

       while (i<=10) {//輸出10條記錄

              lr_db_getvalue("StepName=GetValue",

                                      "DatasetName=MyDataSet",

                                      "Column=nickname",

                                      "Row=next",

                                      "OutParam=MyOutputParam",

                                      LAST);

              lr_output_message("The value %d is: %s", i, lr_eval_string("{MyOutputParam}") );

              i=i+1;

       }

       lr_output_message("The query returned %d rows.", NumRows);

       //釋放結果

    /*lr_db_dataset_action("StepName=RemoveDataset",

        "DatasetName=MyDataSet",

        "Action=REMOVE",

        LAST);*/

       lr_start_transaction("SQL插入");

       NumRows = lr_db_executeSQLStatement("StepName=Insert",

              "ConnectionName=MyCon",

              // Insert語句,本例中第一列id是自增主鍵

              "SQLStatement=Insert into layim_user values ('test','123456','test888888','','/test/test',getdate(),'111111',0) ",

              "DatasetName=MyDataSet",

              LAST );

       times=lr_get_transaction_duration("SQL插入");

       lr_end_transaction("SQL插入", LR_AUTO);

    lr_output_message("The insert date %d rows.", NumRows);

       lr_output_message("SQL插入語句執行時間 %f ", times);

       //關閉連接

       lr_db_disconnect("stepname=Disconnect","connectionname=MyCon",LAST);

       return 0;

}

相關文章
相關標籤/搜索