How to organize user-defined functions into LoadRunner library?

How to organize user-defined functions into LoadRunner library?

Assume, you work and work in LoadRunner, create and create scripts.
Eventually, you face the situation when your code is duplicated in many scripts. This is a wrong way! Take my word for it :)

I will show - why it is and how to struggle against it.

Duplicated code is an evil, because:
  • If you fix/modify a duplicated code, you have to edit each occurrence of this code.
  • It needs add debug messages into duplicated code, you have to add debug functions into each occurrence of this code.
  • If you decide to change the algorithm, implemented in a duplicated code, you are reluctant to find and edit it in each occurrence of this code.
If you have tens or hundreds VuGen tests, containing diplicated code, the support can become a nightmare. Rework will cost too much!!! You must avoid it from the beginning.


Well, what to do?

You have create a user-defined library. It needs place your code into separate h-file.

Let's assume, we will use the following function:
sum(1, 4, -3)
which calculates a sum of arguments. It's easy :)

In LoadRunner VuGen, create new file and write this code:
  1. #ifndef _AR_H_
  2. #define _AR_H_

  3. int sum(int nArg1, int nArg2, int nArg3)
  4. {
  5.     return nArg1 + nArg2 + nArg3;
  6. }

  7. #endif // _AR_H_

Save it as "ar.h" into "Mercury\LoadRunner\include" folder. Actually, the name of file can be any. Out library will perform arithmetic operations (sum :) ), that's why I named the file as
"ar.h".

Your library file is created! To include it, use #include preprocessing directive in Action:
#include "ar.h"

Hint: If your file is not located in a default include folder ("Mercury\LoadRunner\include"), you have to specify full path, for example:
#include "c:\\work\\projects\\include\\ar.h"

Hint: Since you deal with C language, you have to use double backslash inside of path.

That's all! Run your Action and enjoy:
As you see, our function Sum works correctly.


Now, I will answer ans show the following question:

How to process global variables in LoadRunner VuGen script?

Modify our "ar.h" from "Mercury\LoadRunner\include" folder:
  1. #ifndef _AR_H_
  2. #define _AR_H_

  3. int nGlobalValue = 0;

  4. int sum(int nArg1, int nArg2, int nArg3)
  5. {
  6.     return nArg1 + nArg2 + nArg3;
  7. }

  8. #endif // _AR_H_

Then, modify Action like this:

And add a second action ("Action2") to our script:
Note, that we added #include too.

Hint: By the way, the script will work correctly even if you didn't add #include for second action. Nevertheless, I recommend to add this preprocessing.

Execute our updated VuGen script, containing two actions:
As you can see, variable, located in h-file, is shared between actions. It saves his values between actions.
In other words, variable, located in h-file, is a global variable. It can be used for LoadRunner actions communicating.

I hope, library of user-defined functions will simplify your scripts and decrease efforts for their maintenance. As a result, it will save your time and increase your performance :)

17 comments:

Sergey Azarkevich said...

#ifndef "ar.h"
shall be
#include "ar.h"

Dmitry Motevich said...

You right, Sergey!
Thank you for your advertence.

It was my misprinting :) I've just fixed it...

neel said...

Dmitry Motevich ,
one small doubt :),To work with load runner well or to be expert in using load runner is it necessary :) to learn c or c++ in depth?

Dmitry Motevich said...

2neel:
I think, knowledge of C is a mandatory requirement to work with LoadRunner.
Actually, C is not difficult programming language. If you are an *Engineer*, you will study easily.

Mrigs said...

Thanks Dmitry for this wonderful site. All posts here are very informative and helpful.
Thanks again.

ritz said...

Dmitry,

Thanks for the articles; I have benefitted a lot from them.

ritz said...

I am load testing an FTP server currently which sends out an MQ to subscribers once a document is processed. I have created a script that loads documents on the server. We want to look at the time from when the document is posted on the server to when the MQ reaches a subscriber. How can I accomplish this?
Thanks a lot for your help!!

BehzadK said...

WOW this article answers all my questions on this topic. Your work is much appreciated , keep it up and thanks.

Anonymous said...

Good story on including user defined functions.

How do I control the navigation of mulitple actions. So far I can only figure it out using LR_Exit with the proper flag. For example,

vuser_int
initialize stuff

action1
when finished if case 1 do action 2
else do action 3
end action

action2
do this and dont do action 3
end action

action3
do this
end action

vuser_end
all done

I find that if i branch into action3 and do a return 0; it will branch to action2

What elegant solution am I missing

Dmitry Motevich said...

2Anonymous,
Please send your question into LoadRunner group.
Also, provide more detailed explanation of your goal and description on troubles when you faced.

rajesh said...

You have mentioned file->new for creation of library file. But I when i click file->new it show me list of protocols. Which one i have to choose.

Dmitry Motevich said...

to rajesh (September 19),
As I remember File/New worked in LR 9.0. (Actually, I don't remember exactly)
Unfortunately, it does not create new file in LR 9.10. Instead od that, it creates new LR script.

So, Rajesh, you can create new file outside of LR. It will work correctly.

Ritesh Patel said...

I heard that we need to create *.dll file also while creating UDF in load runner. Could you please explain what is the use and what is the syntax to write *.dll file? Where we need to place *.dll file?

Dmitry Motevich said...

@Ritesh Patel,
This is optional. The present article explains how to create library without dll-file.

Sunny-Always The Best said...

I would like to ask you, which tool we have to use to write, compile and execute the below mentioned code to create DLL. Please tell us in detail.

#ifndef _AR_H_
#define _AR_H_

int sum(int nArg1, int nArg2, int nArg3)
{
return nArg1 + nArg2 + nArg3;
}

#endif // _AR_H_

Send us to the below mentioned mail Id : srini.p77@gmail.com

Dmitry Motevich said...

@Sunny-Always The Best (January 21, 2009),
You do NOT need to create DLL-file.
Please read the above article carefully.

As I wrote:
"In LoadRunner VuGen, create new file and write this code"
and then
Save it as "ar.h" into ...

Dmitry Motevich said...

Dear Readers!
Thank you very much for you comments!

Since this article was published more than one year ago, I've just disabled an adding of new comments for the article.