How to perform basic operations on LoadRunner parameters?

How to perform basic operations on LoadRunner parameters?

LoadRunner functions and scripts, containing in the present article, will be interested for LoadRunner beginners in the first place.

I will describe - how to perform basic mathematical operations on LoadRunner parameters:
  • How to convert LoadRunner parameter's value to integer number?
  • How to save integer number to LoadRunner parameter?
  • How to perform mathematical operations on LoadRunner parameter?
You can use this article as a LoadRunner tutorial on LoadRunner parameter operations.
Well... May I start? :)

  1. How to convert LoadRunner parameter's value to integer number?

    Actually, this is not a difficult task. We have to use 'atoi' function.
    'atoi' function converts a string to an integer value.

    Please, see the following code:

    1. lr_save_string("543210", "prmCounter");
    2. lr_output_message("String value of prmCounter: %s", lr_eval_string("{prmCounter}"));

    3. i = atoi(lr_eval_string("{prmCounter}"));

    4. lr_output_message("Integer value of prmCounter: %d", i);


    The result is:
    The key line of code is:

    1. i = atoi(lr_eval_string("{prmCounter}"));

    We get parameter's string value using lr_eval_string function and after that atoi function converts it to int value. Apparently, it is easy :)


  2. How to save integer number to LoadRunner parameter?

    There are several way how to convert integer and save it to parameter.

    1. lr_save_int function. It saves an integer to a parameter.
      Code is very simple:

      1. int i;

      2. i = 433;
      3. lr_save_int(i, "prmCounter");

      4. lr_output_message("String value of prmCounter: %s", lr_eval_string("{prmCounter}"));


      The result is:

    2. sprintf function. It writes formatted output to a string. Then we use lr_save_string function to save the string to parameter:

      1. int i;
      2. char szBuf[12];

      3. i = 118;
      4. sprintf(szBuf, "%d", i);
      5. lr_save_string(szBuf, "prmCounter");

      6. lr_output_message("String value of prmCounter: %s", lr_eval_string("{prmCounter}"));


      The result is:

    3. itoa function. It converts an integer to a string. And then we use lr_save_string function to save the string to parameter:

      1. int i;
      2. char szBuf[12];

      3. i = 27;
      4. itoa(i, szBuf, 10);
      5. lr_save_string(szBuf, "prmCounter");

      6. lr_output_message("String value of prmCounter: %s", lr_eval_string("{prmCounter}"));


      The result is:


  3. How to perform mathematical operations on LoadRunner parameter?

    Let's a parameter contains integer value and we have to double it (multiply by two).
    In this case, the algorithm is:
    1. Convert and save LoadRunner parameter to integer number
      We discussed this operation above.
    2. Multiply integer number by two
      H'm... Do you have guesses how to do it? :)
    3. Save new integer number to LoadRunner parameter?
      This operation was discussed above too.

    So, the final code is:

    1. int i;
    2. lr_save_string("11", "prmCounter");
    3. lr_output_message("String value of prmCounter: %s", lr_eval_string("{prmCounter}"));

    4. i = atoi(lr_eval_string("{prmCounter}"));
    5. i *= 2;

    6. lr_save_int(i, "prmCounter");
    7. lr_output_message("String value of prmCounter: %s", lr_eval_string("{prmCounter}"));


    And its result is:

So, as you can see, working with LoadRunner parameters is not difficult.

As usual - let me know, if you have some questions on this topic.
I will try to help you and provide detailed answer.

--
Yours faithfully,
Dmitry Motevich, speaking LoadRunner :)



Related articles:

20 comments:

Anonymous said...

can i have idea about "ContenChecK" in load runner??

Unknown said...

Very good explanation,i appreciate it.
Thanks a lot

Subbu

Unknown said...

Thanks a lot
I appreciate it

Subbu

Dmitry Motevich said...

Thank you, Subbarao

Anonymous said...

Its a excellent work you doing sire.
Please keep it up. Hope one day I also be expert like you and teach / guide people.
Thanks
Ganesh

Bradley Parker said...

Again, thanks for your wonderful explanations to all of this!!!

My question: How do we perform mathmatical formulas on parameters from an array?

For instance, if we have an array (formed by a web_reg_save_param with ORD=All), how do we figure out which of the 5 paramters has a specific value.

Param_1 = 1
Param_2 = 3
Param_3 = 5
Param_4 = 7
Param_5 = 1

How would we be able to figure out which parameter was greater than "6"? And do it in a way where we don't just print out the parameter name or value, but we can get the script to actually use that parameter in a later part of the script.

Your genius would be very welcome here... :)

Thanks!

Dmitry Motevich said...

to Bradley Parker (September 20),
Imaging that you have an array of integers. How would you find the required value?
The same algorithm is for QRP parameters.

I think, it's a simple question of programming.

Let me know if you have any difficulties with it. Also, do not forget to show your solutions you tryed.

Bradley Parker said...

I'm putting my attempt at it below. I'm getting errors trying to run it, though, so any help would be great.


int i, pcount, tmpvalue, grand;
char *tmpParamName, *newkey;

......

// Grabbing the total number of arrayed parameters
pcount = atoi(lr_eval_string("{ProductKey_count}"));

// Sets the value for "grand" so we can compare the ProductKey value to it.
grand = 1000;

// For each occurance of ProductKey, run the loop
for (i = 1; i <= pcount; i++) {
// Create full name of a current parameter
sprintf(tmpParamName, "{ProductKey_%d}", i);
// Save parameter value as an integer
tmpvalue = atoi(lr_eval_string(tmpParamName));
// Output the value of current parameter
lr_output_message("Value of %s: %s", tmpParamName, tmpvalue);
// IF the parameter value is lower than the set "grand" value, replace "grand"
// with the parameter value and save the parameter name as "newkey"
if (tmpvalue < grand) {
grand = tmpvalue;
newkey = tmpParamName;
} // End 'if'
} // End 'for'

lr_output_message("Value of %s: %s", newkey, grand);


I'm getting the following errors...
Action.c(268): Error: C-interpreter run time error: Action.c (268): Error -- memory violation : Exception ACCESS_VIOLATION received.
Action.c(268): Notify: CCI trace: Action.c(268): lr_eval_string(0x00000000).
Action.c(268): Notify: CCI trace: Compiled_code(0): Action().

Dmitry Motevich said...

to Bradley Parker,
Two questions:
1. What is 268'th lineof your script?
2. About this line:
sprintf(tmpParamName, "{ProductKey_%d}", i);
Did you allocate the memore for tmpParamName before

Bradley Parker said...

Thanks Dmitry!
Line 268 is:
tmpvalue = atoi(lr_eval_string(tmpParamName));


"Did you allocate the memore for tmpParamName before"
- I didn't do anything special for tmpParamName before except declare it as a variable. How do I "allocate the memory?"

Thanks for your quick help!!!

Bradley Parker said...

Dmitry,

I feel like my problem may be related to how I'm declaring the variables. I'm declaring them before the Action function right below the "#include "as_web.h"" statement.

However, I'm setting the chars to be pointers. This doesn't seem like the right thing to do, but it's the only way I can get it to compile correctly. I'm getting errors that say "found `char' expected `pointer to char'." Chainging it to a pointer to char fixes the compile problems, but not the script. What is the appropriate way to declare variables?

Thanks so much for your knowledge! It's a blessing to many.

-Bradley

Dmitry Motevich said...

to Bradley Parker (September 23 & 23),
The problem is related to this line:
sprintf(tmpParamName, "{ProductKey_%d}", i);

You didn't allocate memory for tmpParamName pointer.

There two ways how to allocate memory:
1) Define tmpParamName as an array of chars:
char tmpParamName[257];
2) Work with dynamic memory functions - malloc, calloc.

First way is simpler.

Also, you should get more stronger knowledge on C-programming.

Bradley Parker said...

Dmitry,

Thanks for the suggestions and yes, I agree, that I need more C knowledge. However due to my lack of C knowledge, it makes it hard for me to differentiate between what is a C error and what is a LR caused error when compiling.

I had previously tried to set up the variable like you suggested, but it just will not compile. Here is what I get:

When setting the variable as...
char tmpParamName[257];
char newkey[257];

I get this message when compiling...
Action.c (276): operands of = have illegal types `array 257 of char' and `pointer to char'
Action.c (276): lvalue required

Line 276 is...
newkey = tmpParamName;


Any ideas? Thanks for your help so far!

Dmitry Motevich said...

to Bradley Parker (September 24),
Use sprintf or strcpy to copy string value into array of chars.
I repeat - read any good book on C language. This question is a trivial one in C-language.
The reading will save your and my time :)

Unknown said...

char tmpParamName[64], *newkey;
lr_output_message("Value of %s: %d", tmpParamName, tmpvalue);
lr_output_message("Value of %s: %d", newkey, grand);

change as above and it should work.
The problem was your declaration was not right and you used %s instead of %d to print an integer.

Anonymous said...

Great blog! I have a question I couldn't find an answer for on your blog. Maybe you can help?

Scenario:

I have two LR Parameter of type File that share the same file, which has two columns, like this:

[Files.dat]:
Path,Name
C:\Temp,test1.doc
C:\Temp,test2.doc

First parameter is named filePath and defined as Select Column, by number: 1. Select Next Row: Sequential and Update Value On: Each iteration.

Second parameter is named fileName and is defined as Select Column, by number: 2, Select Next Row: Same as filePath.

Can I get to the number of files listed in files.dat? I have tried:

lr_paramarr_len("{filePath}")
lr_paramarr_len("filePath")
lr_paramarr_len("files.dat")
lr_paramarr_len("files")
lr_eval_string("{filePath_count}")

But it only gives me '0' or wrong strings...
How to get the number of rows in the files.dat or is it not possible?

The parameters 'work' fine. The fileName parameter correctly corresponds with the filePath param and on a second iteration the second file and path are used. Still I am unable to get the count...

Any help would be greatly appreciated!

-Stijn

Dmitry Motevich said...

@Anonymous (Stijn),
There is not standard LoadRunner function to get number of values from a parameter file.
You can count number of non-empty lines programmatically (for example write code in C or in VBScript.)

Anonymous said...

Ah that explains my trouble to find it :-)

Thanks Dmitry, now I know I can stop looking.

-Stijn

Unknown said...

Hi Dmitry,

I play around with an example in your tutorial "How to perform basic operations on LoadRunner parameters?"...

Then I encounter a changed behavior of a LoadRunner parameter/variable. I don't know if it's a bug, or "working as designed!" :). Please see the code segment below:

Action()
{
int i;
char s[12] = "11";

lr_output_message("This is the string: %s", s);


/* Convert string to integer */
i = atoi(s);
i *=2;

lr_output_message("This is the integer: %d", i);

/* Convert integer to string */
lr_save_int(i, "s");

lr_output_message("This is the string: %s", lr_eval_string("{s}"));

return 0;
}

The issue is: After the statement lr_save_int(i, "s"); the syntax lr_output_message("This is the string: %s", s); no longer works correctly. It gives wrong result. Therefore, I must use the syntax lr_eval_string("{s}") as shown in the code segment above.

So, my question is: why the syntax lr_output_message("This is the string: %s", s); does not work consistently?

Thanks for taking time to help your blog readers. Wish you all the best.

Loc

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.