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.


Creating uniquely named log files with details of running processes (TASKLIST)

Thanks to Scott Moore (at LoadTester.com) for the idea behind this article and to the owners of DOStips.com for their article on DOS string manipulation which I used to get the string concatenation right.

When running performance tests or any kind of performance analysis it can be useful to know the processes running on a PC or server which are consuming resources. The Windows command TASKLIST gives a list of currently running processes (basically a text based version of what the Task Manager process tab shows you).

TASKLIST screenshot

By piping the output of this file to a text file you can save details of the running processes for later analysis.

Scott’s article described a batch file which you could run before and after certain events such as batch processing or performance tests to create uniquely named log files containing the TASKLIST information. I had some difficulty getting his time and date stamps to work, possibly due to the timezone settings in Windows which “regionalise” the date and time environment variables. I’ve modified his original batch file to this one below which seems to work on UK PCs running Windows 7, Windows Vista and Windows XP.

Sample Code

for /f “tokens=1-3 delims=/ ” %%d in (“%date%”) do set d=%%d-%%e-%%f

for /f “tokens=1-3 delims=:” %%d in (“%time%”) do set t=%%d%%e

set timestr=%d:~6,4%%d:~3,2%%d:~0,2%-%t:~0,2%%t:~2,2%

tasklist /FO CSV > TASKLIST_Logfile_%timestr%.csv

 

This creates a CSV file named TASKLIST_Logfile_YYYYMMDD-HHMM.csv formatted like the sample below.


Sample Output

“Image Name”,”PID”,”Session Name”,”Session#”,”Mem Usage”

“System Idle Process”,”0″,”Services”,”0″,”24 K”

“System”,”4″,”Services”,”0″,”95,672 K”

“smss.exe”,”272″,”Services”,”0″,”64 K”

“csrss.exe”,”408″,”Services”,”0″,”2,484 K”

“csrss.exe”,”484″,”Console”,”1″,”73,224 K”

“wininit.exe”,”496″,”Services”,”0″,”172 K”

“avgchsva.exe”,”508″,”Services”,”0″,”25,816 K”