Sample LoadRunner script – pad a string with leading zeroes

When working on the previous article, Steven Woodcock found a problem where a string was losing it’s leading zeroes. They needed to be put back before his function worked properly and this is his solution to ensure that the output string was the correct length.

A working (ZIPPED) LoadRunner script can be downloaded so you can see this function in action.

Inputs:
strCharToPad – The String that requires to be padded out i.e. “123456”
cParamName – The name of the parameter you want the end value to be saved to
iValLength – The length you want the string to be padded out to i.e. 9
cValueToAdd – The character you want to pad the string out with, must be a single character i.e. “0”

Outputs:
Creates a LoadRunner parameter of a name specified in the input ‘cParamName’

int PadToX(char* cCharToPad, char* cParamName, int iValLength, char* cValueToAdd)
{

char cTemp[1024] = “”;
int iLoop = 0;
int iMainLoop = 0;
int iSubLoop = 0;

for(iMainLoop = 0; iMainLoop < iValLength; iMainLoop++)
{
if (iMainLoop == strlen(cCharToPad))
{
iLoop = iValLength – iMainLoop;
for (iSubLoop = 0; iSubLoop < iLoop; iSubLoop++)
{
strcat(cTemp, cValueToAdd);
}
}

}

strcat(cTemp, cCharToPad);
lr_save_string(cTemp, cParamName);
}

 

Sample script using Epoch Time to demonstrate large number handling in LoadRunner

This script was provided by Steven Hedley and Steven Woodcock of Sopra Group.

They had a requirement for a script which could add time to the UNIX system time (otherwise known as Epoch time or POSIX time). Epoch time is the number of milliseconds that have passed since 00:00:00 on 1st January 1970. Many computer systems use this system to store the time as a number.

At the time of writing this article the current Epoch time is 1263400124467 this 13-digit number is too long to be used in LoadRunner calculations so to prevent errors occurring in calculations involving this number the chaps from Sopra Group have written a function to split the number into two parts (5 and 8 characters long respectively). The calculation is performed on the second 8-digit long number before the two parts are concatenated together.

The script also ensures that the final calculated time is always 13 digits long by padding any leading zeroes in the values which are used in the calculation. The function uses the web_save_timestamp_param function which returns the current EpochTime. I wasn’t familar with this function until now. Thanks!

Syntax for the function is
AddToEpoch(1800000, “ToTime”); //This takes Epoch time now and rolls it forward 30 minutes, creating a variable ToTime

As usual, the script can be downloaded at the following link:
addtoepoch

If you have samples of scripts which you feel should be shared, please let me know.


LoadRunner function – Add Five Minutes to the current time and save as a string

I needed to write a function which adds five minutes to the current time so that I could create appointment start and end times for an application used in bank branches to make appointments for customers. The function that I’ve written could be modified to add more or less time, the clever bit is making sure that it handles the last few minutes of an hour properly.

For example.
13:56 + 5 minutes = 14:01

The function handles this by converting the 13 and the 56 into integers, doing the necessary sums and then converting the results back into strings which LoadRunner can use.

When the integers are converted back to strings, single digit number are displayed correctly e.g. 01,02,03 etc. rather than 1,2,3.
The function will not work in the last five minutes of the day because I haven’t added a function to add five minutes to 23:56 to get 00:01. (If you’re running tests this late, don’t use this function).

The sample script can be downloaded from my discussion forum.
AddFiveMinutes.zip