Thursday, July 5, 2012

LoadRunner Sub-Transactions

Often in a loadrunner script, many transactions are of a common type, like "save" or "search" or "report_view".  These common operations need to be aggregated for reporting purposes.  You want to easily create a chart showing, for example "Search Response Time" or "Searches Per Second" or "Search Response Time (percentile)".

One easy way to do this is to run the following perl script which will take any transaction and wrap it in a second "sub-transaction" of the name input.  For example, suppose you have the following in your Action.c:

lr_start_transaction("Search1");
...
lr_end_transaction("Search1", LR_AUTO);

...
...


lr_start_transaction("Search2");
...
lr_end_transaction("Search2", LR_AUTO);


Following running the script in the directory with your Action.c file, passing the parameter "search" ("perl lr-subtrans.pl search"), you would now have:


lr_start_transaction("Search1");
lr_start_transaction("search");
...
lr_end_transaction("search", LR_AUTO);
lr_end_transaction("Search1", LR_AUTO);
...
...

lr_start_transaction("Search2");
lr_start_transaction("search");
...
lr_end_transaction("search", LR_AUTO);
lr_end_transaction("Search2", LR_AUTO);


This would then allow very easy reporting of the aggregate search transaction.  After running the script, you have to go in and remove the sub-transactions around transactions that are not relevant, like perhaps "login", "homepage", "back", etc.


Script


#  lr-subtrans.pl
#  Loadrunner add subtransaction script
#
#
use File::Copy;

$transname = $ARGV[0];
$actionfile = "Action.c";
$backupfile = "Action.c.bak";
$outfile = "Action.c.subtrans.txt";

if ($transname eq "-?" or $transname eq "/?" or $transname eq "-help" or $transname eq "/help" or $transname eq "")
{
die "USAGE: perl lr-subtrans.pl <transactionName>     (Note: each transaction will be wrapped with a transaction named <transactionName>)\n";
}

copy($actionfile, $backupfile) or die "File cannot be copied.";


open (INFILE, $actionfile) or die "Can't open $actionfile";
open (OUTFILE, ">$outfile") or die "can't open $outfile";

while ($line = <INFILE>)
{
# Add server parameter and default check
if ($line =~ "lr_start_transaction")
{
print OUTFILE $line;
print OUTFILE "\tlr_start_transaction(\"$transname\"); \n";
}
# BIIT ID ("SESSION ID") RULE
elsif ($line =~ "lr_end_transaction")
{
print OUTFILE "\tlr_end_transaction(\"$transname\", LR_AUTO); \n";
print OUTFILE $line;
}
else 
{
print OUTFILE $line;
}
}
close (INFILE);
close (OUTFILE);

# Copy Action.c.correlated.txt to Action.c
copy($outfile, $actionfile) or die "Copy failed: $!";