Speeding Up Script Development Time in LoadRunner

26 February, 2008 – 2:56 pm

If you’ve ever had to write lots of LoadRunner scripts you’ll probably be interested in this snippet of code. Imagine you’re in a situation where you need to produce many LoadRunner scripts each with multiple actions. Normally, once you’ve got all your custom correlations down pat (I hate correlation studio BTW) you will also have a ton of stock standard parameters that you need to find and replace within your scripts. You can do this manually in a point and click type fashion; you can even use VuGen’s clunky text editor (Ctrl-H) to search and replace text strings etc, but if you get sick of this approach, and you want to be more consistent / accurate in your parametization, try the following Perl script.

The attached code below does a couple of things. First, when you’re viewing the code in VuGen, you would first select all and copy the code into memory (Ctrl-A, Ctrl-C) Then you can launch the Perl script (mine is just an icon on my quick launch toolbar) itself. Secondly, once launched, the Perl script goes through and makes all the necessary changes to your code as per the following steps:

1. Using Win32::Clipboard, it reads the copied text from your clipboard into a temporary variable called $copy.
2. Using regex, it then goes through and adds all your customizations, such as common web_reg_finds, web_reg_save_params and any other parameters you would repeatedly use.
3. Using Tk, it then shows you a preview of the formatted code so you can decide if everything is good to go.
loadClipper
4. The re-formatted code is automatically copied back into clipboard, so all you need to do is paste that back into VuGen …

Basically this should help you write more consistent code. You do need to be familiar with regex though, in order to write your own rules. See the applyRules subroutine for some examples I’m currently using.


use Tk;
use Win32::Clipboard;
 
my $ver    = "0.8e";
my $clip   = Win32::Clipboard();
my $result = $clip->Get();
my $main   = MainWindow->new();
my $frame   = $main->Frame()->pack(-side=>'top', -fill =>'x');
my $text  = $main->Scrolled('Text');
   $text->pack(  -expand  => 1,
                -fill  => 'both' );
    tie (*TEXT, 'Tk::Text', $text);
 
# Globals
&processHeader();
&correlateSessionIds();
&correlateSnapshots();
 
# Correlations  
&correlate('correl_productCode',           'productCode=',              '\$');
&correlate('correl_productId',             'bcProductId=',              '\$');
&correlate('correl_productVersion',        'productVersion=',           '\$');
&correlate('correl_productSubVersion',     'productSubVersion=',        '\$');
&correlate('correl_taskId',                'taskId=',                   '\$');
&correlate('correl_requestVersion',        'requestVersion=',           '\$');
&correlate('correl_popupTxopenPopupList',  'PopupTxopenPopupList=',     ';');
 
# content tab correls
&correlate('correl_contentId',              'contentId=',               '\$');
# end
 
# Parameters
&parametize('"Name=requestnum", "Value=(\d+)"',
            '"Name=requestnum", "Value={lr_requestNum}"');
&parametize('"Name=LastRequestTotalTime", "Value=(\d+)"',              
            '"Name=LastRequestTotalTime", "Value={lr_lastRequestTotalTime}"');
&parametize('"Name=NUM_advertiserId", "Value=(\d+)"',
            '"Name=NUM_advertiserId", "Value={advertiserId}"');
&parametize('advID=(\d+)',                'advID={advertiserId}');
&parametize('NUM_advertiserId=(\d+)',     'NUM_advertiserId={advertiserId}');
&parametize('advertiserId=(\d+)',         'advertiserId={advertiserId}');
&parametize('"Name=NUM_productId", "Value=(\d+)"',
            '"Name=NUM_productId", "Value={correl_productId}"');
&parametize('productId=(\d+)',         'productId={correl_productId}');
 
# content tab params
&parametize('"Name=NUM_AdvertiserIDOfGeneralInfo", "Value=(\d+)"',
            '"Name=NUM_AdvertiserIDOfGeneralInfo", "Value={advertiserId}"');
&parametize('NUM_AdvertiserIDOfGeneralInfo=(\d+)',
            'NUM_AdvertiserIDOfGeneralInfo={advertiserId}');    
&parametize('contentIdOfgeneralInfo=(\d+)',         'contentIdOfgeneralInfo={correl_contentId}');
&parametize('artPiece.generalInfo.advertiserID.stringValue", "Value=(\d+)"',
            'artPiece.generalInfo.advertiserID.stringValue", "Value={advertiserId}"');
&parametize('artPiece.generalInfo.advertiserID.stringValue=(\d+)',
            'artPiece.generalInfo.advertiserID.stringValue={advertiserId}');        
# end
 
# Clean up the code
&cleanUp();
 
# Print to screen
print TEXT $result;
 
# Loop da loop
MainLoop();
 
# Subs
sub processHeader {
    if ($result =~ /{/) {
        $custom =   '{'."\n\n\t\/* custom parameters by loadClipper v$ver \n\t\tend mods\n\t*\/\n".
                    "\n\n\t// advertiserId from dbApi added by loadClipper v$ver \n".
                    "\t".'dbApi("selectUniqueNext", "advertiserId", "advertisers","","");'."\n";                
        $result =~ s/{/$custom/;
    }
    
    if ($result =~ /Action=http:\/\/(.+?)\//g) {
        $custom = pack("A30","\t{host_302}")."\t$1\n";
        $result =~ s/$1/{host_302}/g;
        $result =~ s/\t\tend mods\n/\t$custom\n\t\tend mods\n/;
    }
}
 
sub correlate {
    my $param   = $_[0];
    my $LB      = $_[1];
    my $RB      = $_[2];
    
    my $search  = $LB.'(\d+|\w+)'.$RB;
    print $search . "\n";
    
    $LB =~ s/\\//g;
    $RB =~ s/\\//g;
    my $replace = $LB."{".$param."}".$RB;
      
    my $web_reg_save =  
                    "\n\n\t// auto correlation by loadClipper v$ver \n".
                    "\tweb_reg_save_param(\"$param\",\n".
                    "\t\t\"LB=$LB\",\n".
                    "\t\t\"RB=$RB\",\n".
                    "\t\t\"Search=Body\",\n".
                    "\t\t"."LAST);\n\n\n\t";

    $result =~ s/$search/$replace/g;
    
    my @indexes = ();
    my $index   = 0;
    while ($result =~ m/(web.+?\);)/sg) {
        $this = $1;
      
        if ($this =~ /$param/g) {
            $index = pop(@indexes) || index($result, $this);
            substr($result, $index, 0) = $web_reg_save;
            last;
        }
        
        if ($this =~ /web_submit_data/g) {
            $index = index($result, $this);
            push(@indexes, $index);
        }  
    }      
}
 
sub correlateSessionIds {    
    # Process local popup session IDs
    my %popupSessions = ();
    while ($result =~ m/(web_url\("Popup;jsessionid=)(.+?)",/sg) {
        $popupSessions{"$2"} = index($result, "$1$2");      
    }

    foreach $key(keys %popupSessions) {
        my $search  = $key;
        my $index   = $popupSessions{$key};
        my $param   = ($key =~ /([\w\d]{10})!/g) ? "correl_popup_".$1 : undef;
        my $replace = "{$param}";
        my $web_reg_save =  
                    "\n\n\t// auto correlation by loadClipper v$ver \n".
                    "\tweb_reg_save_param(\"$param\",\n".
                    "\t\t\"LB=Popup;jsessionid=\",\n".
                    "\t\t\"RB=\\\"\",\n".
                    "\t\t\"Search=Body\",\n".
                    "\t\t"."LAST);\n\n\n\t";
        substr($result, $index, 0) = $web_reg_save;
        $result =~ s/$search/$replace/g;
    }
    
    # Process opener and primary session IDs
    if ($result =~ /jsessionid=(.+?)_/g) {
        $result =~ s/$1/{correl_mainSessionId}/g;
        $result =~ s/{correl_mainSessionId}_\d+/{correl_mainSessionId}_{lr_sessionNum}/g;
    }    
}
 
sub correlateSnapshots {
    # Random snapshot number viewer based on ascii->char conversion of action_name
    if ($result =~ /^([\w\_])+\(\)/g) {
        @num = unpack("C*",$1);
        foreach (@num) {
            $tNumber += $_;
        }
        $tNumber *= 100;
        $custom = pack("A30","\t{Snapshot tNumber start}")."\t$tNumber\n";
        $result =~ s/\t\tend mods\n/\t$custom\n\t\tend mods\n/;
        @match = ($result =~ /Snapshot=t(\d+)\.inf/g);
        foreach (@match) {
            $result =~ s/Snapshot=t$_\.inf/Snapshot=t$tNumber\.inf/g;
            $tNumber++;
        }
    }
}
 
sub parametize {
    my $search  = $_[0];
    my $replace = $_[1];
      
    if ($result =~ /$search/g) {
        my $value  = $1;
        my $param  = ($replace =~ /{(.+?)}/g) ? $1 : undef;
        $result =~ s/$search/$replace/g;
    }
}
 
sub cleanUp {
    $result =~ s/[\r]+/\n/g;
    $result =~ s/\n\n/\n/g;
    $clip->Empty;
    $clip->Set($result);
}
 

Share it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • Slashdot
  • Technorati
  • YahooMyWeb
  1. 2 Responses to “Speeding Up Script Development Time in LoadRunner”

  2. Nice one. But, Load Runner does not currently support any kind of performance monitoring on Solaris 10.

    By geetha on Aug 27, 2008

  3. Script development (productivity) and performance monitoring of sol10 are 2 different issues. In answer to the second (and hopefully not sounding like a HP zealot), it is possible to monitor sol10 using things like sitescope with custom monitor templates. Although I really don’t like sitescope so much, and normally prefer to roll my own monitors using native kstats or dtrace for sol10.

    Regards,
    Tim

    By Tim on Aug 27, 2008

Post a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image