A user-defined function parameter in LoadRunner gets its value from a function inside an external DLL. It's useful when the built-in parameter types can't produce the value you need, such as a custom date format. You write the function, build it into a DLL, and then use it like any other parameter.
How it works
- Write a function that follows the required format (shown below).
- Build the function into a DLL.
- Copy the DLL into the bin folder of your VuGen installation. This is VuGen's default library path, so you only need to enter the DLL name and not a full path.
- In VuGen, create a parameter and set its type to User Defined Function. Enter the DLL name and the name of your function in the parameter's properties.
Required function format
__declspec(dllexport) char *functionName(char *, char *)
The function must return a string (char *) and take two char * arguments. LoadRunner passes NULL for both when it uses the function as a parameter, so your function should not rely on them.
Example: return the system date
This function returns today's date as text, which you can use as a parameter value:
#include <time.h>
static char buffer[32];
__declspec(dllexport) char *GetSysDate(char *arg1, char *arg2)
{
time_t now = time(NULL);
struct tm *local = localtime(&now);
strftime(buffer, sizeof(buffer), "%d/%m/%Y", local);
return buffer;
}
Build it as mydll.dll and copy it to VuGen's bin folder. Then create a parameter, for example pSysDate, choose the User Defined Function type, and enter mydll.dll and GetSysDate. Each time the parameter is used, it takes the date this function returns.
Calling the function directly from a script
You can also call the function from your script code. Load the DLL with lr_load_dll, then call the function by name. Declare the function first, or LoadRunner may assume it returns an integer and corrupt the returned text.
char *GetSysDate(char *, char *);
Action()
{
char *date;
lr_load_dll("mydll.dll");
date = GetSysDate("", "");
lr_output_message("System date: %s", date);
return 0;
}
When you call the function yourself, you can pass any two strings or variables. The example function ignores them.
Is a header file easier?
For simple functions, usually yes. You can define the function in a header file (.h) and include it in your script, with no DLL to build or deploy. A DLL is more work to maintain, so it's best kept for cases where you need compiled code or want to share the function across many scripts.
Things to watch
- 32-bit or 64-bit: Build the DLL with the same bitness as the VuGen or load generator process that will load it. A mismatch stops the DLL from loading.
- Load generators: If you run the script from Controller, make sure the DLL is also present on every load generator, for example in its bin folder.
- Returned string: Return a pointer that stays valid after the function ends, as the example does with its static buffer. Never return a pointer to a local variable.
- Threads: The static buffer in the example is shared by every Vuser thread in the same process, so concurrent calls can overwrite each other's result. If you run Vusers as threads, give each call its own buffer or run Vusers as processes. See Running Vuser as a Process vs. Running Vuser as a Thread.
Reference: Parameter types in the VuGen documentation
ReplyDeleteIt was great experience after reading this. thanks for sharing such good stuff with us.
Load runner Course in Delhi
app v training
ReplyDeletesccm training
windows admin training
mysql training
azure training
web methods training