Wednesday, November 20, 2013

How to generate GUID in LoadRunner


Compatibility:  Java (all version), SOAP UI, LoadRunner/VuGen (all version)

Today I came across a SOAP request that uses a java function (java.util.UUID.randomUUID()) to generate random number. In very high level, this java function generate strong pseudo random number using randomUUID() function from java util library. Here is the part of the SOAP request with Java function:

${=java.util.UUID.randomUUID()}
ABC


If I need to generate this random number using Java, that is easy. Here is 
the example:

import java.util.*;

public class UUIDGenerator {
    public static void main(String[] args) {
        // creating UUID
        UUID uid = UUID.fromString("12300000-afcd-11bd-b23e-10b96e4ef00d");
        // checking the value of random UUID
        System.out.println("Randomly generated UUID value: "+uid.randomUUID());
    }
}

Output would be:

Randomly generated UUID value: 69b9c547-2278-45d0-a632-b627e30119aa

But the problem was to convert this solution to LoadRunner. Thanks to Scotte 
Moore for coming up with the creative solution. Here are the steps:
•    Create a test using the protocols that generate C scripts.
•    Download the “ole32.dll” file from online (If you google it, you should be able to find it)
•    Save that “ole32.dll” file into the test folder so that lr_load_dll() function can load this dll
•    Finally, you can either create seperate functions to generate GUID/randomUUID or can enabled in the action that will use it.

Action() {
    lr_guid_gen();
    return 0;
}

int lr_guid_gen(){
    typedef struct _GUID{
        unsigned long Data1;
        unsigned short Data2;
        unsigned short Data3;
        unsigned char Data4[8];
    } 
    GUID;
    GUID m_guid;
    char buf[50];
    char bufx[50];
    lr_load_dll ("ole32.dll");
    CoCreateGuid(&m_guid);

    sprintf (buf, "{%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]);

    lr_save_string(buf, "lrGUID");
    lr_output_message(lr_eval_string(buf));
    return 0;
}
Output would be similar to following:

Action.c(54): {1FC2EFD0-3887-45D7-9432-5682802E6D82}