1、此代码为 Loadrunner 8 通过 C API 类型的 Vuser 测试 MySQL 性能,或者测试 sql 语句性能的脚本。这东西很少有人用,网上资料很少,一般测试 B/S 的很多。 CODE:view plaincopy to clipboardprint?/*需要的表结构如下 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
2、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 db_connection; / 数据库连接 int query_result; / 查询结果集 MYSQL_RES char* result_row; / 查询的数据衕 char *se
3、rver = “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.1binlibmysql.dll“); if (rc != 0) lr_error_message(“Could not load libmys
4、ql.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
5、“, 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 !
6、= 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);
7、 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_
8、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_row0, “paramOrderID“); lr_output_message(“Order ID is: %s“, lr_eval_string(“paramOrderID“); my
9、sql_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_dat
10、a 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_re
11、sult); 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_row0, “paramOrderID“); lr_output_message(“Order ID is:
12、 %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
13、(“%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 ord
14、er_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(
15、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_f
16、ree_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_co
17、nnection); mysql_close(db_connection); lr_abort(); / 释放 MySQL 资源 mysql_close(db_connection); return 0; /*需要的表结构如下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 or not. A value of 0 mea
18、ns 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; / 数据库连接int query_result; / 查询结果集 MYSQL_RESchar* result_row; / 查询的数据衕char *server = “localhost“;char *user = “root“;char *pa
19、ssword = “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();/ 创建 MySQL 对象db_connection = mysql_i
20、nit(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_abor
21、t();/ 向数据库插入数据/ 此处的 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_c
22、onnection);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 =
23、NULL) lr_error_message(“%s“, mysql_error(db_connection);mysql_free_result(query_result);mysql_close(db_connection);lr_abort();/ 如果结果集包含多行数据,需要多次调用 mysql_fetch_row 直到返回 NULLresult_row = (char *)mysql_fetch_row(query_result);if (result_row = NULL) lr_error_message(“Did not expect the result set to be
24、empty“);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);/ 在事务里更新一行数据,需要用 InnoDB 引擎rc = mysql_query(db_connection, “BEGI
25、N“); /启动事务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_conn
26、ection);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_
27、row = NULL) lr_error_message(“没有查询到结果“);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_dat
28、a 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“); / 提交
29、事务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
30、(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_outp
31、ut_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;