lr_param_sprintf Function in Loadrunner with Examples

It is very useful function in LoadRunner. Let us understand it with a situation. Let us consider a situation where we want to generate a random string of say length 4,  during script execution. We can do the same by entering all possible permutations and combinations in one parameter file, which would definitely be not an easy way to do so. Apart from this, we have another method which is lr_param_sprintf() function.

This function is quite similar to the function  sprintf() used in the language C, but we have one difference between the two, which is that it is written to parameter in LoadRunner while the function that is used in C write the output to the string buffer. Let us see the syntax of this function, as given below.

Syntax: int lr_param_sprintf(const char *paramName, const char *format[,args….]);

We can see that this function returns int value means any value which is of type integer. This function returns a 0 on success, so if you get any non-zero value you should understand that your function has delete has failed.

One thing that should be kept in mind is that we should need to have some preparation for it for using this function. For example if  we are you are using using alphabets to create random string of four characters, then we we should byou should have a parameter with all 26 alphabets* stored in the parameter because four characters will be selected from these 26 characters stored in the parameter which will be randomly selected to form the string. Let us see the screenshot below , which shows how these characters will be stored in LoadRunner and how it will look in LoadRunner window. We can also see that the name of the parameter is p_RandomLetter.

lr_param_sprintf Function in Loadrunner with Examples

Now let us understand  our scenario where we have to create a string of 4 characters which will be selected from any 26 characters available in in parameter p_RandomLetter. Let us see the following code to understand it better, where we will be using our function lr_param_sprintf  to create random string((like AGKG, HKGF, MANS, etc.) from the characters available in the parameter p_RandomLetter.

lr_param_sprintf (“p_RandomName”, “%s%s%s%s”,

                lr_eval_string(“{p_RandomLetter}”),

                lr_eval_string(“{p_RandomLetter}”),

                lr_eval_string(“{p_RandomLetter}”),

                lr_eval_string(“{p_RandomLetter}”));   

p_RandomName is the name of the parameter where we want to store random string of four character.

%s%s%s%s” is used denote a string consisting of four character which we can see in the function given above. Here we are using for 4 %S, which is used to represent four character string.

p_RandomLetter” is the name of the parameter where we have stored all the 26 alphabets from A,B,C,….. to X, Y, Z.

lr_eval_string()” this frequently used loadrunner function is used to evaluate the value of the parameter (p_RandomLetter) and convert it into a string. Now one thing to be noted here is that we cannot use parameter values directly as they are, even if the values are string itself. It has to be first converted into string using lr_eval_sting() function

Now we are ready to use “p_RandomName” parameter wherever you want to use this random name generated.

Overall, we can say that it is very useful function for creating random string and should be used very wisely for productive results in performance testing using LoadRunner.

Read other Loadrunner Functions with examples here.

Leave a Comment