Unique Name Creation (518 Views)
Reply
Frequent Advisor
DaveP_1
Posts: 74
Registered: ‎08-19-2010
Message 1 of 4 (518 Views)

Unique Name Creation

I had an issue where I needed to be able to create a unique customer name. It had to be "real", inasmuch as it must start with a capital (I know that some names don't but mostly they do) and it must not contain digits. This is the (rather basic) code I came up with. It does work, hence I thought I would share it. It does not give hyphens or apostrophes (so no O'Connor or Smythe-Thompson). Maybe someone will re-write it properly! I place it in globals.h.

 

==================================================

 

void unique_name(void)
{
    /*
    This routine creates a string that changes every 1000th of a second.
    It should give a unique string when used in a script as it is unlikely that
    2 scripts will call it at the same time.
    I use this to create a surname that is unique to a system. The Forename is simply
    a substring of the surname, and Forename2 (in case you need a married couple)
    is a substring of Forename.
    The resultant string is created in 2 parts, as the date string is rather long.
    Also, if the full string is used, the year would be evaluated first, which would
    mean that all surnames in a test would start with the same letter - starting with
    the "time" portion helps this.
    This is a rather crude implementation and needs the following LoadRunner parameters
    creating:
        pTimeStr and save the format %M%S.000
        pDateStr and save the format %Y%m%d%H
    These are the output parameters:
        fSurname
        fForename
        fForename2
    I like to create these as LoadRunner "VuserID" parameters - this way they are
    highlighted in the code - the vuserID is created at the test start so is safely overwritten
    when this function is run.
    This was a quick fix rather than have the function return values.
    My C skills are legendary but for all the wrong reasons!
    There is a neater way to do this but I've not progressed it.
*/
    extern double pow(double x,double y);
    extern long strtol(const char *s, char **end_ptr, int base);
    extern double strtod(const char * str, char ** endptr);
   
    char* str_hldr;
    char* unique_str;
    int i;
    int j;
    int rc;
    double float_num;
    long divisor;
    long curr_char;
    long mod_no;
    long place_hldr;
    long unique_num;


    str_hldr = (char *)malloc(16 * sizeof(char));
    unique_str = (char *)malloc(16 * sizeof(char));

// #################################
    strcpy(str_hldr, "");
    strcpy(unique_str, "");

//    unique_num = 20110101; // early date
//    unique_num = 20201223; // future date and hour
//    unique_num = 5959999; // high minutes, seconds and milliseconds
//    unique_num = 0000000; // low minutes, seconds and milliseconds

    // Minutes & seconds part
    float_num = strtod(lr_eval_string("{{pTimeStr}}"), NULL); // converts to floating point
    unique_num = (long)(float_num * 1000); // Moves decimal 3 places left and converts to a long int

    mod_no = unique_num % 26;

    curr_char = 'a'; // Place 1
    curr_char = curr_char + mod_no;
    sprintf(str_hldr,"%c", curr_char);
    strcat(unique_str, str_hldr);

    for(i=4; i>0; i--) // Places 2 - 5
    {
        divisor = pow(26, i);
        place_hldr = unique_num / divisor;
        unique_num = unique_num - (place_hldr * divisor);
        curr_char = 'a';
        curr_char = curr_char + place_hldr;
        sprintf(str_hldr,"%c", curr_char);
        strcat(unique_str, str_hldr);
    }

    // Date and hours part
    unique_num = atol(lr_eval_string("{{pDateStr}}"));
    mod_no = unique_num % 26;

    curr_char = 'a'; // Place 1
    curr_char = curr_char + mod_no;
    sprintf(str_hldr,"%c", curr_char);
    strcat(unique_str, str_hldr);

    for(i=6; i>0; i--) // Places 1 - 6
    {
        divisor = pow(26, i);
        place_hldr = unique_num / divisor;
        unique_num = unique_num - (place_hldr * divisor);
        curr_char = 'a';
        curr_char = curr_char + place_hldr;
        sprintf(str_hldr,"%c", curr_char);
        strcat(unique_str, str_hldr);
    }

    unique_str[0] = toupper(unique_str[0]);

    lr_save_string(unique_str, "fSurname");

    unique_str += 4;
    unique_str[6] = '\0';
    unique_str[0] = toupper(unique_str[0]);

    lr_save_string(unique_str, "fForename");
    unique_str += 1;
    unique_str[0] = toupper(unique_str[0]);

    lr_save_string(unique_str, "fForename2");
    rc=lr_get_debug_message();
    lr_set_debug_message(rc, LR_SWITCH_OFF);
    lr_set_debug_message(18, LR_SWITCH_ON);
    lr_log_message("%s, %s, %s", lr_eval_string("{{fSurname}}"), lr_eval_string("{{fForename}}"), lr_eval_string("{{fForename2}}"));
    lr_set_debug_message(18, LR_SWITCH_OFF);
    lr_set_debug_message(rc, LR_SWITCH_ON);
    strcpy(unique_str, "");

// #################################

    // Now free the buffers
    free(str_hldr);
    free(unique_str);

} // End of function unique_name(void)

==================================================

Please use plain text.
Advisor
iluminati
Posts: 14
Registered: ‎12-14-2011
Message 2 of 4 (436 Views)

Re: Unique Name Creation

Thanks DaveP. That's a really useful function that I'd like to use in my tests.

Please use plain text.
Frequent Advisor
DaveP_1
Posts: 74
Registered: ‎08-19-2010
Message 3 of 4 (430 Views)

Re: Unique Name Creation

You're very welcome. I thought more people would be interested...

Please use plain text.
Trusted Contributor
AdiKidron
Posts: 201
Registered: ‎03-21-2011
Message 4 of 4 (424 Views)

Re: Unique Name Creation

Hi,

Here are some additional sites with useful C Code for LoadRunner:

 

http://www.bish.co.uk/home/34-recent/117-evaluate-string-content-in-loadrunner

 

http://www.jds.net.au/tech-tips/loadrunner-log-options/

 

https://github.com/randakar/y-lib

 

We are currently working on an HP site to allow assets uploading and sharing. In future versions VuGen will be linked directly to this repository, allowing a quick search, upload and download of materials.

If you have additional sources to share or known contributors, please post below. I will contact them once the site will be public.

 

Regards,

Adi

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation