LoadRunner Generating Random numbers

Source: Internet
Author: User

loadrunner generates random numbers
 
When performing a stress test on website registration, you need to parameterize the registered user name. Because a large amount of test data can be used, you choose to parameterize by generating random numbers. At first, the parameter random function that comes with loadrunner is used. The code is as follows:
 
"Name = RegForm [name]", "Value = {NewParam}", ENDITEM,
 
The parameter selection is: random number
technology sharing
 
Because the system limits the number of digits in the user name, and for the convenience of statistics, the code is modified to:
 
"Name = RegForm [name]", "Value = lr {NewParam} {NewParam1} {NewParam2}", ENDITEM,
 
Note that the maximum number of digits of the value after parameterization cannot exceed the system limit, otherwise incorrect parameters may be submitted. Load the scene and run the test after the parameterization is completed, which can basically meet the requirements of stress testing.
    During the test, it was found that there would still be some failed transactions. After checking the logs, it was found that the registration failed due to duplicate user names. To reduce this situation, another random number method was tried. code show as below:
    int name_num, rand_num, i;
    char StrTable [] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    char i_name [20] = "";
    srand ((unsigned) time (NULL));
    lr_log_message ("srand is% d", srand ((unsigned) time (NULL)));
    name_num = rand ()% 20;
    lr_log_message ("rand number is% d", name_num);
for (i = 0; i <= name_num; i ++)
{
rand_num = rand ()% 62;
strncat (i_name, StrTable + rand_num, 1);
}
    lr_log_message ("the string is% s", i_name);
    lr_save_string (i_name, "i_name_value"); // Save a random number
    ...
 
    "Name = RegForm [name]", "Value = lr {i_name_value}", ENDITEM,
 
     After modifying the script, a large number of failures were found during the stress test. After debugging the script, it was found that the same parameter may be generated multiple times. Consider whether the seed is not initialized each time. Set srand ((unsigned) time (NULL )); After placing in vuser_init (), there will be no duplicates when looping. Later, when I wanted to simply combine these two methods:
 
     vuser_init ()
{
srand ((unsigned) time (NULL));
return 0;
}
 
Action ()
{
int name_num, rand_num, i;
    char StrTable [] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    char i_name [20] = "";
    name_num = rand ()% 20;
    lr_log_message ("rand number is% d", name_num);
for (i = 0; i <= name_num; i ++)
{
rand_num = rand ()% 62;
strncat (i_name, StrTable + rand_num, 1);
}
    lr_log_message ("the string is% s", i_name);
    lr_save_string (i_name, "i_name_value"); // Save a random number
    ...
 
"Name = RegForm [name]", "Value = lr {NewParam} {i_name_value}", ENDITEM,
 
     Basically can meet the testing needs, if you have time, I hope to find a better method.
 
 
 
 
 
 
Some uses of random functions in Loadrunner
 
    Sometimes I need to randomly select the values in the drop-down menu or list in this script, then we need to use random numbers. The following is an example of the use of random numbers.

Example 1:
str = rand (); // Generate an arbitrary random number str = rand ()% 200 // Generate a random number with a maximum of 200 str = rand ()% 100 + 200 // Generate a random number between 200-300
For example:
int iRand;
Action ()
{srand (time (NULL)); // Special note: add this sentence, take a different random value each time

iRand = rand ()% 100; if (30> = iRand) {lr_output_message ("The value of iRand is:% d, iRand <30", iRand);}

else {lr_output_message ("The value of iRand is:% d, iRand> 30", iRand);}

 return 0;}

Example two:

One point of application in associations: (determining the value of count in an association)

web_reg_save_param ("search_param", "LB = <p> <A HREF=", "RB=>", "ORD = All"); ... search rand_selection = (rand ()% atoi (lr_eval_string ("{search_param_count} ")) + 1);

Example three:

Generate a new value (randselection) as a suffix to get (randomly) the searched value

sprintf (my_new_parameter, "{search_param_% d}", rand_selection);
 
 
 
 
 
 
 
 
 
Zhang Zhuoran
 

A method for Loadrunner to generate random letters
char * randstring (int slen)
{
    int i, randid;
    char temp [100] = "";
    char character_set [52] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' , 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', ' X ',' Y ',' Z ',' a ',' b ',' c ',' d ',' e ',' f ',' g ',' h ',' i ',' j ' , 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',' t ',' u ',' v ',' w ',' x ',' y ',' z '};
    for (i = 1; i <= slen; i ++)
     {
         randid = rand ()% 52;
         sprintf (temp, "% s% c", temp, character_set [randid]);
      }
        return (temp);
}

 

How to call the function:

lr_save_string (randstring (5), "searching");

// Call the randstring function to generate a 5-bit random string

A very simple method can be combined with a loadrunner method to generate a unique random number.

If you want to generate a random number from 0 to 99, you can use:

int which; which = rand ()% 100;
So which number is a random number from 0 to 99

For example: combine lr_whoami or use a web_save_timestamp_param to get the current timestamp, so that you can make a unique parameter value.

Try not to use lr's own parameterized random value, which will greatly reduce the performance of the load generator.

 

 

 

 

 

How does lr get the current system timestamp
   Generally use the time function to get the current unix timestamp

   The lr program is as follows:

   int t1;

   char a [20];

   t1 = time (); // Get the current system time

    // Store time as different parameter types according to different situations

   lr_save_int (t1, "time1"); // Save t1 as a shaping parameter time1

   sprintf (a, "% d", t1); // store t1 in array a

   lr_save_string (a, "time2"); // Save array a as a string parameter time2

   lr_output_message ("Current time is% s", lr_eval_string ("{time1}"));

   lr_output_message ("Current time is% s", lr_eval_string ("{time2}"));

   // The following procedure can substitute time1 and time2 into different form items

 

 

 

Three ways to use random numbers in LR
 

  There are three ways to get random numbers in LR:
 
First, parameterize the variables, use the random parameter mode that comes with LR, and randomly match the number of the specified length
 
 
Second, define variables, use the rand () function
  For example, int Num;
       Num = rand (); // This way you get a random number, but this random number is different every time
 
  // If you need to get a random number within a specified length, such as a random number from X to Y, you can use the following algorithm
       Num = rand ()% (Y-X + 1) + X // This will get any number that contains the house of X to Y
 
Third, define variables, use the srand function
  The function prototype is: srand ((unsighed) time (null))
  For example, int Num;
       Num = srand (time (null))
 
loadrunner generates random numbers

label:

Original address: http://www.cnblogs.com/qmfsun/p/4561524.html

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.