Automated Reconstruction of LoadRunner Scripts

From PeformIQ Upgrade
Revision as of 10:31, 5 June 2008 by PeterHarding (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

The following are early versions of scripts I have used to cleanup and reformat LoadRunner scripts after I have recorded them. Ultimately, my intention was to automate the inclusion of parameters and other code such as standard libraries.

The Scripts

See:

Examples

chk.py
lr__.py
url_quote.py

chk.py

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

import re
import sys
import types
import getopt
import pprint

from string import join, split

#--------------------------------------------------------------------------
"""
Clean up LoadRuner script data blocks

"""
#--------------------------------------------------------------------------

debug_flg   = False
verbose_flg = False

in_ITEMDATA = False

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

def process(filename):
   global in_ITEMDATA

   ifd = open(filename, 'r')
   ofd = open('xxx.tmp', 'w')

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

   for line in ifd.readlines():
      line = line[:-1]

      if re.match('ITEMDATA', line):
         in_ITEMDATA = True
         ofd.write("%s\n" % line)
         continue

      if re.match('LAST', line):
         in_ITEMDATA = False
         ofd.write("%s\n" % line)
         continue

      if in_ITEMDATA:
         m = pat.search(line)
         print pat.groupindex

         print m.__dict__

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

   ifd.close()
   ofd.close()

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

def main(argv):
   global pp


   print argv

   file = argv[0]

   process(file)

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

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

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

lr__cleanup.py

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

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

from string import join, split    

#--------------------------------------------------------------------------
"""
Clean up LoadRuner script data blocks
"""
#--------------------------------------------------------------------------

debug_flg   = False
verbose_flg = False

in_ITEMDATA = False

tmpfile     = 'xxx'

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

def cleanup(filename):
   global in_ITEMDATA

   spacer  = None
   block   = None
   l_name  = 0
   l_value = 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('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)

            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()

   os.rename(filename, 'bak/' + filename)
   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:
      cleanup(script)

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

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

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


url_quote.py

#!/usr/bin/env python

import urllib

x = 'WAGGA WAGGA SERVICE'

print urllib.quote(x)

ih = open('WCID.dat', 'r')

while (True):
   l = ih.readline()

   if not l:
      break

   l = l[:-1]

   x = l.split(',')

   print "%s,%s,%s,%s,%s" % (x[0], x[1], x[2], urllib.quote(x[2]), x[3])

ih.close()