Skip to main content

How do we write a user defined function in LoadRunner?

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

  1. Write a function that follows the required format (shown below).
  2. Build the function into a DLL.
  3. 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.
  4. 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

Comments

Post a Comment

Popular Posts

Pacing Time in LoadRunner

What is Pacing? Where and why to use it? -Pacing is the time which will hold/pause the script before it goes to next iteration. i.e Once the   Action   iteration is completed the script will wait for the specific time(pacing time) before it starts the next one. It works between two actions. eg, if we record a script there will be three default actions generated by the Load Runner:   vuser_init, Action   and   vuser_end,   the pacing will work after the   Action   block and hold the script before it goes to repeat it. The default blocks generated by LoadRunner is shown below: Actions marked in Red Now we know what is pacing and we use it between two iteration. The next question comes to mind is why we use pacing: Pacing is used to: To control the number of TPS generated by an user. To control number of hits on a application under test.     Types of Pacing: There are three options to control the pac...

What is Performance Testing and why it is important?

We spend most of our time by browsing Facebook, watching videos, uploading photos and many other online activities. Have you ever noticed if you are watching a video and all of sudden the website goes down? If you are trying to book an online ticket and on the payment page, the website stops responding? If you are adding the last available item on sale in the shopping cart and the website gets hung? Isn't it stressful? For every growing business, it's very crucial to keep a tap on the performance of their application. If a website takes more than 2 seconds to respond then users tend to move to some other website which can cause a huge business loss.  Let's say you are on a retail website and browsing for some item that takes around 2-3 seconds to respond to each click or loading the page. In most cases, the user will go for some other website. Performance Testing gives an idea about how the system is going to behave on load and can help the business owners to ...

What is Pacing and how it's different from the Think Time

What is pacing? Pacing is the wait between two iterations of a script. Once an iteration finishes, the script pauses for a set amount of time (the pacing time) before it starts the next one. When you record a script, LoadRunner generates three default blocks: vuser_init , Action and vuser_end . Pacing takes effect after the Action block, holding the script back before it repeats. The default blocks LoadRunner generates are shown below. Actions marked in red What is think time? Think time copies real user behavior by adding a delay between two actions or transactions, like the pause a person takes before clicking the next link. Can think time replace pacing? In an interview, I was once asked: if you add think time at the end of a script, won't it work as pacing too? If so, why did HP provide two separate settings for the same job? They are not the same, because each has a different purpose. With think time, the thread stays active and LoadRunner waits for the ...