Objective : In this tutorial we would connect to mysql database in loadrunner and insert a row in sample database. We would be connecting to database using web protocol.
Pre-Requisite :
Sample Script:
Create a test script in web- HTTP/HTML protocol in Load runner.
Using below script you can talk to mysql database and insert a row in the sample database table
===================
// The MySQL 5.0 C API documentation is available from:
// http://dev.mysql.com/doc/refman/5.0/en/c-api-functions.html
// Note that this code may have problems with thread safety.
// It is therefore best to run each vuser as a process rather than as a thread.
Action()
{
int rc; // return code
int db_connection; // Declaractions is a bit dodgy. Should really use MYSQL defined in mysql.h
int query_result; // Declaractions is a bit dodgy. Should really use MYSQL_RES defined in mysql.h
char** result_row; // Return data as array of strings. Declaractions is a bit dodgy. Should really use MYSQL_ROW defined in mysql.h
char *server = "localhost";
char *user = "root";
char *password = ""; // very naughty to leave default root account with no password![Smile :) :)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
char *database = "testing";
int port = 3306; // default MySQL port
int unix_socket = NULL; // leave this as null
int flags = 0; // no flags
// You should be able to find the MySQL DLL somewhere in your MySQL install directory.
rc = lr_load_dll("C:\\Program Files (x86)\\HP\\LoadRunner\\Lib\\libmysql.dll");
if (rc != 0) {
lr_error_message("Could not load libmysql.dll");
lr_abort();
}
// Allocate and initialise a new MySQL object
db_connection = mysql_init(NULL);
if (db_connection == NULL) {
lr_error_message("Insufficient memory");
lr_abort();
}
// Connect to the database
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();
}
// INSERT a row into the database table
lr_param_sprintf("paramInsertQuery", "INSERT INTO test (LastName, FirstName) VALUES ('testLN4', 'testFN4');"); // use current time as order ID for this example
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();
}
else
{
lr_outpput_message("Insert Successful");
}
// Free the MySQL object created by mysql_init
mysql_close(db_connection);
return 0;
}
Pre-Requisite :
- Prepare a database in mysql similar to this tutorial - http://www.speforums.com/resources/database-test-plan-using-jmeter-for-stored-procedure-testing.8/
- Copy libmysql.dll from mysql installation to your loadrunner lib directory
Sample Script:
Create a test script in web- HTTP/HTML protocol in Load runner.
Using below script you can talk to mysql database and insert a row in the sample database table
===================
// The MySQL 5.0 C API documentation is available from:
// http://dev.mysql.com/doc/refman/5.0/en/c-api-functions.html
// Note that this code may have problems with thread safety.
// It is therefore best to run each vuser as a process rather than as a thread.
Action()
{
int rc; // return code
int db_connection; // Declaractions is a bit dodgy. Should really use MYSQL defined in mysql.h
int query_result; // Declaractions is a bit dodgy. Should really use MYSQL_RES defined in mysql.h
char** result_row; // Return data as array of strings. Declaractions is a bit dodgy. Should really use MYSQL_ROW defined in mysql.h
char *server = "localhost";
char *user = "root";
char *password = ""; // very naughty to leave default root account with no password
char *database = "testing";
int port = 3306; // default MySQL port
int unix_socket = NULL; // leave this as null
int flags = 0; // no flags
// You should be able to find the MySQL DLL somewhere in your MySQL install directory.
rc = lr_load_dll("C:\\Program Files (x86)\\HP\\LoadRunner\\Lib\\libmysql.dll");
if (rc != 0) {
lr_error_message("Could not load libmysql.dll");
lr_abort();
}
// Allocate and initialise a new MySQL object
db_connection = mysql_init(NULL);
if (db_connection == NULL) {
lr_error_message("Insufficient memory");
lr_abort();
}
// Connect to the database
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();
}
// INSERT a row into the database table
lr_param_sprintf("paramInsertQuery", "INSERT INTO test (LastName, FirstName) VALUES ('testLN4', 'testFN4');"); // use current time as order ID for this example
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();
}
else
{
lr_outpput_message("Insert Successful");
}
// Free the MySQL object created by mysql_init
mysql_close(db_connection);
return 0;
}