Saturday, September 8, 2012

Generating UUID in Loadrunner

There have been multiple times where I needed to generate UUID (Verion 4) in Loadrunner but there is no inbuild function to do so. Therefore, I had to either create or modify functions to satisfy my need.

NOTE: Loadrunner does have a function(lr_generate_uuid) for UUID but it does not generate your standard UUID.

Below are three different UUID functions that I normally use depending on the requirement.

For example, If only requirement is that UUID be 32 hexadecimal digits then I will use simple LR UUID to generate it. If there are proper checks in place to see the UUID generated is valid then I will use a proper UUID function's. Therefore you can select which one you want to use depending on your requirement.

Simple UUID - this is a simple 32 hexadecimal digits. You can generate hexaadecimal in Loadrunner using %x or %X Random number.

Following screenshot shows how to generate a random Hexadecimal.


Lr UUID - this function generates 32 hexadecimal digits UUID using Version 4(random) variant. This is the most common format I have seen applications use.

Win UUID - this function uses windows inbuild CoCreateGuid function (ole32.dll). The original code is written by Scott Moore and I have modified it a little bit.

NOTE: I have left the deallocation of pointer for you to do in the code.

#define Hexlength 50  //Max length

char *lr_guid_gen(); //explicitely declare the function
char *lr_uuid_gen(); //explicitely declare the function

Action()
{   

  char *Win_UUID=0; //declare window function UUID variable 
  char *slr_UUID=0; //declare loadrunner function UUID variable
  char *lr_uuid;  //declare UUID variable

  Win_UUID=(char *)malloc(sizeof(char)); //allocate dynamic memory
  slr_UUID=(char *)malloc(sizeof(char)); //allocate dynamic memory

  Win_UUID=lr_guid_gen(); //execute Window UUID function
  slr_UUID=lr_uuid_gen(); //execute LR UUID function
  lr_uuid=lr_generate_uuid(); //assign result generated by loadrunner internal
  function to lr_uuid

  /*output a simple UUID*/
   lr_output_message("smp_UUID: %s",lr_eval_string("{sHex}{sHex}-{sHex}-{sHex}-{sHex}-{sHex}{sHex}{sHex}"));
 //you could use something like this as well -> lr_output_message("smp_UUID: %s",lr_eval_string("{sHex}{sHex}-{sHex}-{FourHex}-{sHex}-{sHex}{sHex}{sHex}"));

  /*output loadrunner UUID*/
  lr_output_message("slr_UUID: %s",lr_eval_string(slr_UUID));

  /*output window generated UUID*/
  lr_output_message("Win_UUID: %s",lr_eval_string(Win_UUID));

  /*output base64 UUID*/
  lr_output_message("%s",lr_uuid);
 
 /*frees uuid created by lr_generarte_uuid*/
 lr_generate_uuid_free(lr_uuid);
 
 return 0;

}

 

/*This function uses windows ole32 CoCreateGuid function to generate UUID*/
char *lr_guid_gen()

{

    typedef struct _GUID

    {

        unsigned long Data1;

        unsigned short Data2;

        unsigned short Data3;

        unsigned char Data4[4];

    } GUID;

    char guid[Hexlength];
    GUID m_guid;

    lr_load_dll ("ole32.dll");

    CoCreateGuid(&m_guid);

    sprintf (guid, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",

    m_guid.Data1, m_guid.Data2, m_guid.Data3,

    m_guid.Data4[0], m_guid.Data4[1], m_guid.Data4[2], m_guid.Data4[3],

    m_guid.Data4[4], m_guid.Data4[5], m_guid.Data4[6], m_guid.Data4[7]);


    return guid;
}


/*This function uses loadrunner random numbers to generate version 4 UUID*/
char *lr_uuid_gen()
{
  char uuid[Hexlength];
  switch (atoi(lr_eval_string("{RandNo}")))
  {
   case 1:
    sprintf(uuid,lr_eval_string("{Hex}{Hex}-{Hex}-{FourHex}-{aHex}-{Hex}{Hex}{Hex}"));
    break;
   case 2:
    sprintf(uuid,lr_eval_string("{Hex}{Hex}-{Hex}-{FourHex}-{bHex}-{Hex}{Hex}{Hex}"));
    break;
   case 3:
    sprintf(uuid,lr_eval_string("{Hex}{Hex}-{Hex}-{FourHex}-{EightHex}-{Hex}{Hex}{Hex}"));
    break;
   case 4:
    sprintf(uuid,lr_eval_string("{Hex}{Hex}-{Hex}-{FourHex}-{NineHex}-{Hex}{Hex}{Hex}"));
    break;
   default:
    break;
  }
 return uuid;
}




Output:
Action.c(23): smp_UUID: 492ec6db-dcaf-fb6d-1009-72a542e26ee7
Action.c(26): slr_UUID: c4fbde14-4f41-4963-8400-32f77f6e0633
Action.c(29): Win_UUID: cee9f8fd-3715-4183-b367-a4c13b84a7c8
Action.c(32): LBase_64: J3+Lpj2t20KzpBSgg0e5Bg==

Action.c(23): smp_UUID: 65ea1bc1-8434-e05b-92f5-7e23a0f7e253
Action.c(26): slr_UUID: 358390f7-2405-4b03-8be4-b8c61b90a2b6
Action.c(29): Win_UUID: 75d74eb9-a2bf-42b4-b114-61d634a2c7db
Action.c(32): LBase_64: uzxvhpLXP0ifUjMSqzngCQ==

Action.c(23): smp_UUID: 0a86ce02-7413-1da3-bd39-63fc4cb9d60e
Action.c(26): slr_UUID: 726546c5-500d-4ef2-a27e-f447bb925143
Action.c(29): Win_UUID: db65a02f-1ca1-4fb3-ab22-0e41088b1633
Action.c(32): LBase_64: IJV65qREQUOe5CVwcE5aVQ==
 
Action.c(23): smp_UUID: 6678a6ac-3356-eb2f-63e6-02fe5a5185f2
Action.c(26): slr_UUID: acddc8f9-746d-4655-838c-7802483e06bb
Action.c(29): Win_UUID: e7a7ff1c-4019-4053-a51a-7567582d825c
Action.c(32): LBase_64: i+b5VpgA7Ueis8r1bxBTJg==
 
Action.c(23): smp_UUID: 3f8eb05e-9936-b5fc-6be9-454fd8fb9ab5
Action.c(26): slr_UUID: cc835158-9e86-4df5-8a4e-1e3f25808754
Action.c(29): Win_UUID: 3d604abe-046b-43dc-a5c4-e759fb04f960
Action.c(32): LBase_64: Rk2mSYUuOUmU0Yti_W+a5Q==

If you have a different function to generate UUID in Loadrunner, I would love to know about it.

No comments: