Difference between revisions of "Synthesizing SVT Data"

From PeformIQ Upgrade
Jump to navigation Jump to search
Line 47: Line 47:
"""
"""


 
import sys
import random
import random


Line 168: Line 168:
         pass
         pass


#-------------------------------------------------------------------------------
if __name__ == "__main__":
    main(sys.argv[1:])
#-------------------------------------------------------------------------------
</pre>
=Generating User Profiles=
<pre>
#!/usr/bin/env python
#
#  $Id:$
#
#-------------------------------------------------------------------------------
"""
Generate an abitrary collection of pseudo Credit Card numbers which
satisfy the validation test
"""
import sys
import random
import getopt
#-------------------------------------------------------------------------------
prefix      = '1234'
max_digits  = 16
max_numbers = 1000
#-------------------------------------------------------------------------------
cost_centers = [
  ["XX100000-X", "XXX DIVISION XX100000"],
  ["XX100001-X", "XXX DIVISION XX100001"],
  ...
]
#-------------------------------------------------------------------------------
def gen(prefix):
  no = prefix
  l = list(no)
  i = len(prefix)
  while (i < (max_digits - 1)):
      x = random.randint(0, 9)
      l.append(str(x))
      # print i, l, x
      i += 1
  no = ''.join(l)
  chk_digit = encode(no)
  delta = 10 - chk_digit
  if delta == 10:
      delta = 0
  # print chk_digit, delta
  l.append(str(delta))
  no = ''.join(l)
  return no
#-------------------------------------------------------------------------------
def encode(no):
  n  = 0
  tot = 0
  for i in range(len(no)):
      v = int(no[i])
      if i % 2 == 0:
        inc = v * 2
        if v > 4:
            n += 1
      else:
        inc = v
      tot += inc
  tot += n
  chk = tot % 10
  return chk
#-------------------------------------------------------------------------------
def validate(ccnumber):
  cnt = 0
  tot = 0
  for i in range(len(ccnumber)):
      val = int(ccnumber[i])
      if i % 2 == 0:
        inc = val * 2
        if val > 4:
            cnt += 1
      else:
        inc = val
      tot += inc
  tot += cnt
  if ((tot % 10) == 0):
      return True
  else:
      return False
#-------------------------------------------------------------------------------
def test():
  for no in nos:
      print no
      if validate(no):
        print "Card number VALID"
      else:
        print "Card number INVALID"
#-------------------------------------------------------------------------------
def gen_user_data():
  out = open('Users.dat','w')
  out.write("AGS_Number,FirstName,Username,Password,CostCenter,CreditCardNumber\r\n")
  i = 0
  no_centers = len(cost_centers)
  while i < max_numbers:
      center_idx = random.randint(0, no_centers - 1)
      cost_center = cost_centers[center_idx][0]
      cc_no = gen(prefix)
     
      if validate(cc_no):
        out.write("test_%03d,%03d,test%03d,test%03d,%s,%s\r\n" % \
            (i, i, i, i, cost_center, cc_no))
      else:
        print "Card number %s - INVALID" % cc_no
        cc_no = None
      if cc_no:
        i += 1
      else:
        pass
#-------------------------------------------------------------------------------
USAGE = """\
  Usage:
    $ ./gen_user_data.py [-d] [-v] [-f <filename>]
"""
def usage():
  sys.stderr.write(USAGE)
#-------------------------------------------------------------------------------
def main(argv):
  global debug_flg
  global verbose_flg
  global file
  #----- Process command line arguments ----------------------------
  try:
      opts, args = getopt.getopt(argv, "df:hv", ["debug", "file=", "help", "verbose"])
  except getopt.GetoptError:
      usage()
      sys.exit(2)
  else:
      for opt, arg in opts:
        if opt in ("-d", "--debug"):
            debug_flg = True
        elif opt in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif opt in ("-f", "--file"):
            logfile = arg
        elif opt in ("-v", "--verbose"):
            verbose_flg = True
  gen_user_data()


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


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


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

Revision as of 10:22, 16 April 2008

Overview

Preparing data for performance tests is a very important part of the preparation phase for a load test exercise. The following scripts provide simple examples of approaches to synthesize parameter data for use by the test scripts.

User Login IDs

#!/usr/bin/env python

print 'UserId'

for i in range(200):
   print "svt%03d" % i

Unquoting URL Data

#!/usr/bin/env python

import urllib                                                                                                                                                                       

encoded_data = """
%3CPromptEngineRuntimeContext%20xmlns%3D%22http%3A%2F%2Fwww.crystaldecisions.com%2Freport%22%20xmlns%3Axsi%3D%22http%3A%2F%2F
www.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Axsd%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%20xsi%3Atype%3D%22
PromptEngineRuntimeContext%22%20version%3D%222%22%3E%3CID%3E{MachineXID}%3C%2FID%3E%3CRenderOption%20xsi%3Atype%3D%22
BOwner%26gt%3B%26lt%3B%2FOwner%26gt%3B%26lt%3BDefinition%20xsi%3Atype%3D%22Parameter%22%20id%3D%225%22%26gt%3B%26lt%3B",                                                                         
"""                                                                                                                                                                                 

data = urllib.unquote(encoded_data)                                                                                                                                                               

print data                           

Generating Synthetic Credit Card Numbers

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

"""
Generate a abitrary collection of pseudo Credit card numbers which
satisfy the validation test
"""

import sys
import random

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

prefix      = '1234'
max_digits  = 16
max_numbers = 1000

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

def gen(prefix):
   no = prefix

   l = list(no)

   i = len(prefix)

   while (i < (max_digits - 1)):
      x = random.randint(0, 9)

      l.append(str(x))
      # print i, l, x
      i += 1

   no = ''.join(l)

   chk_digit = encode(no) 

   delta = 10 - chk_digit

   if delta == 10:
      delta = 0

   # print chk_digit, delta

   l.append(str(delta))

   no = ''.join(l)

   return no

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

def encode(no):
   n   = 0
   tot = 0

   for i in range(len(no)):
      v = int(no[i])

      if i % 2 == 0:
         inc = v * 2
         if v > 4:
            n += 1
      else:
         inc = v

      tot += inc

   tot += n

   chk = tot % 10

   return chk

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

def validate(ccnumber):
   cnt = 0
   tot = 0

   for i in range(len(ccnumber)):
      val = int(ccnumber[i])

      if i % 2 == 0:
         inc = val * 2
         if val > 4:
            cnt += 1
      else:
         inc = val

      tot += inc

   tot += cnt

   if ((tot % 10) == 0):
      return True
   else:
      return False

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

def test():
   for no in nos:
      print no

      if validate(no):
         print "Card number VALID"
      else:
         print "Card number INVALID"

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

def main():
   i = 0

   while i < max_numbers:
      cc_no = gen(prefix)
      
      if validate(cc_no):
         print cc_no
      else:
         print "Card number %s - INVALID" % cc_no
         cc_no = None

      if cc_no:
         i += 1
      else:
         pass

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

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

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

Generating User Profiles

#!/usr/bin/env python
#
#  $Id:$
#
#-------------------------------------------------------------------------------

"""
Generate an abitrary collection of pseudo Credit Card numbers which
satisfy the validation test
"""

import sys
import random
import getopt

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

prefix      = '1234'
max_digits  = 16
max_numbers = 1000

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

cost_centers = [
   ["XX100000-X", "XXX DIVISION XX100000"],
   ["XX100001-X", "XXX DIVISION XX100001"],
   ...
]

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

def gen(prefix):
   no = prefix

   l = list(no)

   i = len(prefix)

   while (i < (max_digits - 1)):
      x = random.randint(0, 9)

      l.append(str(x))
      # print i, l, x
      i += 1

   no = ''.join(l)

   chk_digit = encode(no) 

   delta = 10 - chk_digit

   if delta == 10:
      delta = 0

   # print chk_digit, delta

   l.append(str(delta))

   no = ''.join(l)

   return no

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

def encode(no):
   n   = 0
   tot = 0

   for i in range(len(no)):
      v = int(no[i])

      if i % 2 == 0:
         inc = v * 2
         if v > 4:
            n += 1
      else:
         inc = v

      tot += inc

   tot += n

   chk = tot % 10

   return chk

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

def validate(ccnumber):
   cnt = 0
   tot = 0

   for i in range(len(ccnumber)):
      val = int(ccnumber[i])

      if i % 2 == 0:
         inc = val * 2
         if val > 4:
            cnt += 1
      else:
         inc = val

      tot += inc

   tot += cnt

   if ((tot % 10) == 0):
      return True
   else:
      return False

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

def test():
   for no in nos:
      print no

      if validate(no):
         print "Card number VALID"
      else:
         print "Card number INVALID"

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

def gen_user_data():
   out = open('Users.dat','w')

   out.write("AGS_Number,FirstName,Username,Password,CostCenter,CreditCardNumber\r\n")

   i = 0

   no_centers = len(cost_centers)

   while i < max_numbers:
      center_idx = random.randint(0, no_centers - 1)

      cost_center = cost_centers[center_idx][0]

      cc_no = gen(prefix)
      
      if validate(cc_no):
         out.write("test_%03d,%03d,test%03d,test%03d,%s,%s\r\n" % \
             (i, i, i, i, cost_center, cc_no))
      else:
         print "Card number %s - INVALID" % cc_no
         cc_no = None

      if cc_no:
         i += 1
      else:
         pass

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

USAGE = """\

  Usage:

     $ ./gen_user_data.py [-d] [-v] [-f <filename>]

"""


def usage():
   sys.stderr.write(USAGE)

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

def main(argv):
   global debug_flg
   global verbose_flg
   global file

   #----- Process command line arguments ----------------------------

   try:
      opts, args = getopt.getopt(argv, "df:hv", ["debug", "file=", "help", "verbose"])
   except getopt.GetoptError:
      usage()
      sys.exit(2)
   else:
      for opt, arg in opts:
         if opt in ("-d", "--debug"):
            debug_flg = True
         elif opt in ("-h", "--help"):
            usage()
            sys.exit(0)
         elif opt in ("-f", "--file"):
            logfile = arg
         elif opt in ("-v", "--verbose"):
            verbose_flg = True

   gen_user_data()

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

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

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

Other Examples