(Original) Call DLL in LoadRunner

Source: Internet
Author: User
Tags strtok

As required by the project, I plan to create a DLL for dynamically retrieving data from the database. First of all, I'm sure to check whether there is a ready-made DLL from Google. I saw the post of Kern Zhang and intend to use it, because it was a repost, it was not very full. After searching for a long time, I realized that he was using ODBC. I had configured ODBC data sources on my machine, but I still could not use them. When initializing hstmt (I guess it is an SQL handle), I plan to decompile his DLL. Let's change it, but C cannot be decompiled and can only be written by myself.

My requirement here is: Dynamically Retrieve data from the MySQL database

Therefore, I can directly use the C API provided by MySQL

Use Visual C ++ to create a -- project -- Win32 dynamic-link libray, and then write it in C.

Note that, first, we need to include MySQL. h. This file is usually under % MySQL %/include. Second, we need to reference # pragma comment (Lib, "C: \ xampplite \ mysql \ Lib \ opt \ libmysql. lib "), we can use the functions provided by c api to write

1. Initialization: mysql_init (& MySQL );

2. Establish a connection: mysql_real_connect (& MySQL, host, user, password, dbname, 0, null, 0)

3. Query: mysql_query (SQL );

4. mysql_use_result (& MySQL)

5. mysql_fetch_row (RES)

6. process the retrieved results as needed.

After compiling the DLL, place it and the LoadRunner script in the same directory, and then place libmysql. dll in this directory.

 

In lordrunner, it runs according to run vuser as a thread by default. The dll that I have written has passed the test in generator, but fails in controller, the main reason for the analysis is that the pointer has accessed the areas that shouldn't be accessed, and the database initialization has failed. After reading a lot of information, it should be caused by my public variables. Under run vuser as a thread, this causes insecure variables. I also thought about a lot of solutions and asked people in group C who said it was a very troublesome thing. I do not recommend that I spend a long time solving this problem. Finally, I changed the running mode in LoadRunner and changed it to run vuser as a process. The result is normal.

 

However, I began to worry about whether this mode will affect performance. Find an article (http://rdc.taobao.com/blog/qa? P = 2046)

If you run each vuser as a process, a driver is started for each vuser instance and loaded to the memory. If you set 10 vusers, there are 10 drivers in the task manager. Loading multiple drivers into the memory will occupy a large amount of RAM (Random Access to memory) and other system resources, this limits the number of vusers that can run on any load generator.

If you run each vusername by line, controlleris an instance of only the driver program (such as mdrv.exe) for every 50 vusernames. These thread vusers will share the memory segment of the parent driver process. This eliminates the need to reload the driver multiple times, saves a lot of memory space, and thus can run more vusers on a load generator.

Web performance testing is commonly used in the thread mode, but the thread resources are allocated from the process resources. Therefore, multiple threads in the same process have shared memory space, this may cause synchronization of multiple threads. If the scheduling is not good, problems may occur. For example, the resources used by thread a must wait for thread B to be released, B can continue only after other resources are released. Therefore, the thread mode is not suitable for all scenarios. Some articles describe that the thread mode is only applicable to security protocols.

Therefore, we can think that the multi-thread method is to run more vusers in each load generator.

In the end, you may be concerned about whether the two methods have different pressures on the server. I have read the descriptions and running data from many articles. For the server, as long as the number of users is set to work normally, and the speed is not affected by the local hardware, the pressure on the server should be the same.

 

We plan to follow run vuser as a process. If there is a better solution in the future, replace it with thread.

 

LoadRunner script

Vuser_init ()
{

Int I;
Char * Host = "192.168.22.193 ";
Char * user = "test ";
Char * Password = "test ";
Char * dbname = "test ";

Lr_load_dll ("test. dll ");
I = db_init (host, user, password, dbname );
If (! I ){
Lr_error_message ("init dB error ");
Return-1;
}

Return 0;
}

/***

The purpose of setting int I = 5 and I = I + 5 is mainly to solve the difference between our parameters in each vuser, in addition, it increases progressively according to our requirements. I know that there are settings in LoadRunner to solve the difference between the parameters used by each vuser. They divide the parameters into several blocks, and each vuser extracts the corresponding data from the corresponding block. If we have a parameter from 1-and five vusers, vuser1 will get 1 at the beginning, and vuser2 will get 21 at the beginning .... Vuser5 will get 81. However, the actual situation is that at the beginning of the operation, there was no such data in the database. We want these vusers to read parameters in ascending order, for example, vuser1 takes 1, vuser2 takes 2, and so on. The second loop to vuser1, which takes 6

***/

// Concurrent user count
Int I = 5;

Action ()
{

Int ID;
Char STR [10];

// Char * TMP;
Char SQL [] = "select textcontent from cdr_mmvd where id = ";
Id = atoi (lr_eval_string ("{ID}") + I;
// ITOA (ID, STR, 10 );

Strcat (SQL, ITOA (ID, STR, 10 ));
I = I + 5;

// Lr_log_message ("output is % s \ n", STR );

Lr_log_message ("output is % s \ n", query (SQL ));
Freeresult ();
// Lr_think_time (1 );
Return 0;
}

 

Vuser_end ()
{
Close ();
Return 0;
}

 

Below is my DLL source code. I am not very familiar with C, so it is easy to write.

# Include "Lib. H"
# Include <stdio. h>
# Include <Winsock. h>
# Include <string. h>
# Include "C: \ xampplite \ mysql \ include \ mysql. H"
# Pragma comment (Lib, "C :\\ xampplite \ mysql \ Lib \ opt \ libmysql. lib ")

Int db_int (char *, char *);
Char * query (char *);
Void freeresult ();
Void close ();

File * FP;

MySQL;
Mysql_res * res;
Mysql_row row;
Char key [6];

Int db_init (char * Host, char * user, char * password, char * dbname)
{
If (FP = fopen ("DLL. log", "W +") = NULL)
Printf ("file not be opened! ");
Mysql_init (& MySQL );

If (! Mysql_real_connect (& MySQL, host, user, password, dbname, 0, null, client_multi_results )){
Fprintf (FP, "MySQL Connection Failed: % s \ n", mysql_error (& MySQL ));
Return 0;
}
Else
Return 1;

}

Char * query (char * SQL ){
Char * TMP;
Fprintf (FP, "Start to query \ n ");
If (! Mysql_query (& MySQL, SQL )){
Res = mysql_use_result (& MySQL );
Row = mysql_fetch_row (RES );
TMP = strtok (row [0], "= ");
TMP = strtok (null, "= ");
Strncpy (Key, TMP, 6 );
Key [6] = '\ 0 ';
Return key;
}
Else {
Fprintf (FP, "query failed: % s \ n", mysql_error (& MySQL ));
Return "1 ";
}
}

Void freeresult (){
Mysql_free_result (RES );
}

Void close (){
Mysql_close (& MySQL );
Mysql_library_end ();
Fprintf (FP, "Close MySQL connection. Finished. \ n ");
Fclose (FP );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.