Difference between revisions of "Reformat LoadRunner Scripts"

From PeformIQ Upgrade
Jump to navigation Jump to search
 
Line 5: Line 5:
<pre>
<pre>
#!/usr/bin/env python
#!/usr/bin/env python
#
#  $Id:$
#
#
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------

Latest revision as of 17:33, 24 September 2014

Example

The following script is an early draft of a work in progress to reformat LoadRunner scripts to improve their readability.

#!/usr/bin/env python
#
#--------------------------------------------------------------------------

import re
import os
import sys
import types
import getopt
import pprint
import datetime

from string import join, split    

#--------------------------------------------------------------------------

"""
Clean up LoadRuner script data blocks
"""

__version__ = '1.0.0'

debug_flg   = False
verbose_flg = False

strip_logo  = True
strip_css   = True
add_wrsp    = True

tmpfile     = 'xxxxx'

separator   = """\
   //---------------------------------------------------------------------------

"""

save_param_pageID = """\
   web_reg_save_param("pageID",
                      "LB=input type=\\"hidden\\" name=\\"pageID\\" value=\\"",
                      "RB=\\"",
                      LAST);
"""


#--------------------------------------------------------------------------

def process(filename):
   in_logo     = False
   in_ITEMDATA = False
   in_Resource = False

   spacer      = None
   block       = None
   l_name      = 0
   l_value     = 0
   submit_cnt  = 0

   print filename

   ifd = open(filename, 'r')
   ofd = open(tmpfile, 'w')

   p   = re.compile('(  *)("Name=[^,]*") *, *("Value=[^,]*") *, *ENDITEM,(.*)')

   for line in ifd.readlines():
      line = line[:-1]
      line = line.replace('\t', '    ')

      if re.search('test403.xxx.com.au', line):
         line = line.replace('test403.xxx.com.au', '{System}')

      if re.search('web_reg_find\("Text=app', line):
         ofd.write(separator)


      #----- Cull CSS request calls -----------------------------------------------

      if strip_css:
         if re.search('web_concurrent_start', line):
            in_Resource = True
            continue

         if re.search('web_concurrent_end', line):
            in_Resource = False
            continue

         if in_Resource:
            continue

      #----- Cull logo request call ----------------------------------------------

      if strip_logo:
         if re.search(r'web_url\("xxx_logo.gif', line):
            in_logo = True
            continue

         if in_logo and re.search('LAST\)', line):
            in_logo = False
            continue

         if in_logo:
            continue

      #----- Drop in web_reg_save_param(pageID) block -----------------------------

      if add_wrsp:
         if re.search('web_submit_data\("', line):
            submit_cnt += 1
            if submit_cnt > 2:
               ofd.write("%s\n" % save_param_pageID)
            ofd.write("%s\n" % line)
            continue

      #----- Reformat ITEMDATA block -------------------------------------------

      if re.search('ITEMDATA', line):
         block   = []
         l_name  = 0
         l_value = 0
         spacer  = ''
         if debug_flg: print 'ITEMDATA Start'
         in_ITEMDATA = True
         ofd.write("%s\n" % line)
         continue

      if in_ITEMDATA and re.search('LAST', line):
         if debug_flg: print 'ITEMDATA End ========================================='
         in_ITEMDATA = False
         if debug_flg: print 'len block = %d' % len(block)
         block_txt = ''
         for b in block:
            line = "%s%s,%s%s,%sENDITEM,%s\n" % (
                spacer,
                b[1],
                (' '*(l_name - len(b[1]) + 2)),
                b[2],
                (' '*(l_value - len(b[2]) + 2)),
                b[3])
            block_txt += line
         ofd.write("%s" % block_txt)
         ofd.write("%sLAST);\n" % spacer)
         if debug_flg: print 'ITEMDATA End ========================================='
         spacer = None
         continue

      if in_ITEMDATA:
         if debug_flg: print line
         m = p.search(line)
         if m:
            if debug_flg: print '[%s][%s][%s][%s]' % (m.group(1), m.group(2), m.group(3), m.group(4))

            if not spacer:
               spacer = m.group(1)
            name   = m.group(2)
            value  = m.group(3)
            extra  = m.group(4)

            if (submit_cnt > 3) and re.search('=pageID', name):
               value = '"Value={pageID}"'

            l = len(name)
            if l > l_name:
               l_name = l

            l = len(value)
            if l > l_value:
               l_value = l

            block.append((spacer, name, value, extra))
            continue

      ofd.write("%s\n" % line)

   ifd.close()
   ofd.close()

   stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")

   os.rename(filename, 'bak/' + filename + '__' + stamp)
   os.rename(tmpfile, filename)

#----- Main test ----------------------------------------------------------

def main(argv):
   global pp

   print argv

   if not os.path.exists('bak'):
      os.mkdir('bak')

   for script in argv:
      process(script)

#--------------------------------------------------------------------------

if __name__ == "__main__":
   main(sys.argv[1:])

#--------------------------------------------------------------------------