Difference between revisions of "Automated Reconstruction of LoadRunner Scripts"

From PeformIQ Upgrade
Jump to navigation Jump to search
Line 24: Line 24:
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------


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


from string import join, split
from string import join, split


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


debug_flg  = False
debug_flg  = False
Line 42: Line 43:


in_ITEMDATA = False
in_ITEMDATA = False


#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
Line 52: Line 52:
   ofd = open('xxx.tmp', 'w')
   ofd = open('xxx.tmp', 'w')


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


   for line in ifd.readlines():
   for line in ifd.readlines():
Line 72: Line 72:


         print m.__dict__
         print m.__dict__


       ofd.write("%s\n" % line)
       ofd.write("%s\n" % line)
Line 99: Line 98:
</pre>
</pre>


==lr__.py==
==lr__cleanup.py==


<pre>
<pre>
Line 105: Line 104:
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------


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


from string import join, split     
from string import join, split     


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


debug_flg  = False
debug_flg  = False
Line 125: Line 124:
in_ITEMDATA = False
in_ITEMDATA = False


tmpfile = 'xxxxx'
tmpfile     = 'xxx'


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


def process(filename):
def cleanup(filename):
   global in_ITEMDATA
   global in_ITEMDATA


Line 142: Line 141:
   ofd = open(tmpfile, 'w')
   ofd = open(tmpfile, 'w')


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


   for line in ifd.readlines():
   for line in ifd.readlines():
Line 221: Line 220:


   for script in argv:
   for script in argv:
       process(script)
       cleanup(script)


#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
Line 256: Line 255:


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


ih.close()
ih.close()
</pre>
</pre>




[[Category:LoadRunner]]
[[Category:LoadRunner]]
[[Category:Python]]
[[Category:Python]]

Revision as of 09:30, 5 June 2008

Overview

The following are scripts I have used to cleanup and reformat LoadRunner after I have recorded them - 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()