LoadRunner Knowledge Base article 5218 “Generating Unique Names”, posted by John Sawbridge, gives a function to generate a “unique” string [a-zA-Z] up to 20 characters long. The function accepts a string length argument and always writes the output to a parameter called {rname}. He notes “If you invoke the function successively in the same second, you will get the same name.”

Clearly this is a weak solution. As the function writes to the same parameter each time, it is not possible to use the function more than once in a LoadRunner script without overwriting your original value and if you are running a non-trivial number of concurrent Vusers or start more than one Vuser at a time, your chances of non-unique return values are high.

Fortunately, there are better and easier ways to generate unique usernames in LoadRunner.

First of all, seeding a random number generator loses much of its point if the seed is not a frequently changing value. Calling srand(time(NULL)), as the LoadRunner documentation suggests, is not useful for all situations as the time() function returns a value in seconds. A sneaky way to get a greater precision timestamp to use as a seed value is to create a date/time parameter that returns a time that includes milliseconds, and convert it to an int with atoi(). Another way to get a high-precision timestamp would be to use lr_load_dll and run a native Windows function. But why do all of this by hand when LoadRunner has a parameter type “random”, which will let you specify a min and a max value?

LoadRunner Parameter dialog. Random Number parameter

While it is fun to play around with random numbers, and I hope that someone got something out of the above paragraph, if you must generate a unique username, the easiest solution is just to create a data table full of them and set the “select next row” property of your file parameter to “unique”.

John, your Knowledge Base article rates “1-Very Unhelpful”.

 

Published On: February 2, 2004Tags: ,

2 Comments

  1. Aju Sreekumar December 14, 2007 at 3:57 pm

    I have recently started working on Loadrunner leaving back my short stay in the Java/J2EE arena. As I do not have a profile in the mercury support site yet, I have not seen the original article by John, which is the center point to this discussion. Right now I use this approach to generate unique UserIds. Here is the code snippet.

    // modify the uniqueId for each test run
    char *uniqueId = “XXX1”;
    char memberId[20];

    //Generate Unique MemberIds
    sprintf(memberId,
    “%s%s%s”,
    uniqueId,
    lr_eval_string(“{VUserID}”),
    lr_eval_string(“{IterationNumber}”));

    For each test run, I increment/modify the uniqueId and keep track of the changes in my workbook, there by I can refer back and find out at a later stage when was the memberId created. This approach works fine for us and removes any ambiguity that may arise with srand or millisecond approach.

    The down side that I can foresee with the approach as suggested by the author is that there is a small percentage of chance for the userIds/usernames to be repeated when running multiple tests. Please share your thoughts on this.

    Regards,
    AJ

  2. kone July 2, 2013 at 2:22 pm

    srand(time(NULL));
    num = (rand()%(atoi(lr_eval_string(“{c_AreaIDs_count}”))))+1;
    sprintf(a,”{c_AreaIDs_%d}”,num);
    lr_save_string(lr_eval_string(a),”AreaID”);
    sprintf(a,”{c_AreaName_%d}”,num);
    lr_save_string(lr_eval_string(a),”AreaName”);

Comments are closed.