I think that the following question from the Mercury Support forum neatly illustrates one of the major problems with LoadRunner.
Hi all,
I have a financial application that submits the data in $ amounts. When I looked in the extended log the value is in number format like (43251) but during submission it is converted into dollar format $43,251.00.
How to convert the string into $ format? Is there any function?
Thanks in advance,
The majority of people using LoadRunner aren’t great programmers, and the ones who are aren’t necessarily great programmers in C. Fortunately, the level of skill required for most problems is not very high, and everyone muddles along as best they can.
Not surprisingly, the small proportion of problems that require more than basic knowledge of C are done poorly (or by someone else).
The most common problem that requires additional programming is basic string manipulation. Most LoadRunner scripts are written in C, and manipulating strings with C is like pulling teeth.
I would argue that Mercury’s choice to use C as LoadRunner’s main scripting language, while having the advantages of it being a solid standard, having a low performance overhead, and there being a large pool of people at least partially familiar with the language, has the unfortunate trade-off of it being…well…kind of hard for most people.
Here’s my solution to the above problem.
// This function converts a numeric string to a dollar value.
// eg. 123456 becomes $123,456.00
LPCSTR ConvertStringToDollars(char input[20]) {
int i;
// To account for the extra characters that will be added, the size of output
// should be 4 + strlen(input) + (strlen(input)/3)
char output[30];strcpy(output,"$");
// insert a comma every third character, but not at the start.
for (i=0; i<strlen (input); i++) {
if ( ((strlen(input)-i)%3==0) && (i!=0) ) {
output[strlen(output)] = ′,′;
}
output[strlen(output)] = input[i];
}strcat(output, ".00");
return output;
}Action()
{
lr_output_message(ConvertStringToDollars("123456")); // Prints "$123,456.00"
return 0;
}
This illustrates my point perfectly. Even though the code performs a simple operation, it’s complicated, and requires knowledge of how C stores data in memory in order to implement. Possibly more importantly, implementing the solution in C takes a lot longer than it might in another language. If there are any mistakes in my code, it just proves my point even more.
As computers increase in power and the performance advantage of writing performance test scripts in C shrinks even further compared to other languages, the relative disadvantages of C will grow in the minds of testers.
17 Comments
Comments are closed.
It’s like some kind of brain teasing challenge…
You have the following functions sprintf, strcat, strchr, strcmp, strcpy,
strdup, stricmp, strlen, strlwr, strncat, strncmp, strncpy, strnicmp,
strrchr, strset, strspn, strstr, strtok, strupr, and as many native
language features as you like. Try not to leak or overwrite memory. Your time starts now…
Another recent Mercury Support Forum question (and my answer) demonstrates my point also…
How to find the absolute value of a double ?
Hi,
How to find the absolute value of a double variable ?
function “abs()” gives wrong result.
Thanks & Regards
R.K
How to find the absolute value of a double ?
Your problem is that the standard C library funtion abs() is expecting its argument to be an int rather than a double. As C is a weakly typed language, the function just treats the double value as if it was an int even though the two data types are stored differently in memory. Consequently you get garbage as output.
Cheers,
Stuart.
I’ve said it before. Everyone who uses LoadRunner professionally should know C.
Joel Spolsky agrees with me. He says that all programmers should get Back to Basics.
for (i=0;i if ( ((strlen(input)-i)%3==0) && (i!=0) ){
I used this loop as given in the above page. LR is throwing errors. please find the Error.
ction.c (7): syntax error; found `if’ expecting `;’
Action.c (7): syntax error; found `if’ expecting `)’
Action.c (12): warning: missing return value
Action.c (13): expecting an identifier
Action.c (13): syntax error; found “.00” expecting `)’
Action.c (13): skipping “.00”
Action.c (13): extraneous old-style parameter list
Action.c (14): unrecognized declaration
Action.c (15): unrecognized declaration
LPCSTR ConvertStringToDollars(char input[20])
{
int i;
char output[30];
strcpy(output, “$”);
for (i=0;i if ( ((strlen(input)-i)%3==0) && (i!=0) ){
//for (i=0; i=if ( ((strlen(input)-i)%3==0) && (i!=0) ) {
output[strlen(output)]=’,’;
}
output[strlen(output)] = input[i];
}
strcat(output,”.00″);
return output;
}
Action()
{
lr_output_message(ConvertStringToDollars(“123456”)); // Prints “$123,456.00”
return 0;
}
The code got mangled by the content managment system (must have happened in the last upgrade).
I have corrected all the errors that were introduced. Give it another go.
Cheers,
Stuart.
thanks Stuart …its working
How I can extract text from the last HTTP response received by the virtual user and later to manipulate the string in the script of Loadrunner
I believe your output string length can be 1 char larger than necessary. As the first set of 3 digits (when reading left to right) will never require a comma in front of it.
For a string with 20 characters, 6 commas are necessary and (strlen(input)/3) = 20/3 = 6 is correct. However, for a string with 21 values, 6 commas are necessary (still) yet (strlen(input)/3) = 21/3 = 7 is incorrect. So here’s a nasty way of fixing that:
int fix=0;
if(strlen(input)%3==0) fix=1;
// should be 4 + strlen(input) + (strlen(input)/3) – fix
Worth noting that SaveLen and SaveOffset Attributes for web_reg_save_param can be used to effectively pull substrings into variables which can later be concatenated to suit formatting requirements.
Hi,
I am using for loop to repeat first transaction & after completing for loop, second transaction should start. The script work well without parameterization on one variable, but when the parameterization is used, the for loop is using same (first value) value for the entire loop. My aim is to use the sequential value of the parameter from the file. How to achieve this?
Did you ever figure out how to do this ?
i am having a string in my script say 123456789123. i want to insert four zeroes after the 3rd character i.e. expected string is 1230000456789123. Can anybody help ma out?
Thanks in advance
Hi,
Another problem I am facing is how to implement a SubStr() function in LoadRunner.
It will take 3 arguments – OriginalString, StartPosition, Length. And it should return the substring from the original string.
Does anyone have a custom function for this?
Thanks
Sayantani
India
Hi,
You can use the following-
Go to the parameters property and select update/change the value on – “Each occurance” .
Make sure not to use “Each iteration” option in the drop down.
if we want to make it dynamic and parameterise the value of input then what to do?
coding is ok. but i want to display only integer values. It is supported all characters.
char string1[]= “432151456”;
char string2[]= “.00″;
char string3[20];
char string4[2]=”$”;
int len;
int mo,i,j =0;
len = strlen(string1);
lr_output_message(“%d”,len);
mo=len/3;
lr_output_message(“%d”,mo);
lr_output_message(“%c”,string1[0]);
lr_output_message(“%d”,1%3);
for(i=0;i<len+1;i++)
if(j % 3 == 0 & j!=0 & j!=len+mo)
{
string3[j]= ',';
i–;
j++;
}
else
{
string3[j]=string1[i];
j++;
}
strcat(string3,string2);
strcat(string4,string3);
lr_output_message("%s",string4);