Synthesizing CreditCard Data for LoadTesting

From PeformIQ Upgrade
Jump to navigation Jump to search

Data Generation

Generate a Valid Credit Card Numbers

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

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

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

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

main()

Synthesizing Credit Card Data

Generate data file for import to Application

Here is a script used to synthetically generate an upload data file for Credit Card data:

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

import os
import csv
import sys
import getopt
import random
import datetime

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

debug_flg     = False
max_flg       = False
random_flg    = False
verbose_flg   = False

__version__   = '1.0.2'


file          = None

no_days       = 1
date          = datetime.datetime.today()

dates         = ['070729', '070730', '070731']

xcodes        = [
                  '1002220', 
                  ... 
                  
                  '1211220'
                ]

#----- Set up credit card transactiuon type list ---------------------

cc_merchants = []

cc_merchants_in  = open("cc_merchants.csv", "rb")

reader = csv.reader(cc_merchants_in)

for row in reader:
  cc_merchants.append(row)

cc_merchants_in.close()  # Explicitly close the file *NOW*

no_merchants = len(cc_merchants)

#----- Set up credit card numbers in cc_no list ----------------------

cc_no = []

cc_in  = open("cc_no.txt", "rb")

reader = csv.reader(cc_in)

for row in reader:
  cc_no.extend(row)

cc_in.close()  # Explicitly close the file *NOW*

#----- Set up the file header and footer -----------------------------

header = """\
01,xxx,xxx,%s,2004,1,,,2/
02,,xxx,1,%s,,AUD,2/
"""

footer = """\
...
"""

#----- Set up the body of the file -----------------------------------

max_no_tx =   10  # Number of transactions per CC
cost      = 1000  # Cost of the transaction
tx_id     =    0  # Number that appears in transaction description

def write_data(date):
   global cost, tx_id

   file_date    = date.strftime("%y%m%d")
   expense_date = (date - datetime.timedelta(days=1)).strftime("%y%m%d")

   print "Generating expense data for '%s'" % expense_date

   out = open(file_date,"w")

   out.write(header % (file_date, expense_date))

   for i in range(len(cc_no)): 
      if random_flg:
         no_tx      = random.randint(0, max_no_tx)
      elif max_flg:
         no_tx      = 5
      else:
         no_tx      = 1

      tx_cnt        = 0
      total         = 0.0
      expense_lines = ''

      while tx_cnt < no_tx:
         cost = random.weibullvariate(alpha=3.0,beta=1) * 2000.0
         type = cc_merchants[random.randint(0, no_merchants-1)]

         expense_lines += "16,699,%.0f,,%s,,%s\n" % (cost, type[0], type[1])

         total    += cost
         tx_cnt   += 1
         tx_id    += 1
         cost     += 5

      expense_block  = "01,%s,,...,%03.0f,%d,,...,,/\n" % (cc_no[i], total, tx_cnt)
      expense_block += expense_lines
      expense_block += "99,...,%d/\n" % (tx_cnt + 2)

      out.write(expense_block)

   out.write(footer)
   out.close()

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

USAGE = """\

  Usage:

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

"""

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

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

def main(argv):
   global debug_flg
   global max_flg
   global random_flg
   global verbose_flg
   global file, date, no_days

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

   try:
      opts, args = getopt.getopt(argv, "dD:f:hmn:rv",
                 ["date", "debug", "file=", "help", "max", "no_days", "random", "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 ("-D", "--date"):
            target_date = arg
            date = datetime.datetime.strptime(target_date, "%Y%m%d")
            print date
         elif opt in ("-f", "--file"):
            logfile = arg
         elif opt in ("-h", "--help"):
            usage()
            sys.exit(0)
         elif opt in ("-m", "--max"):
            max_flg = True
         elif opt in ("-n", "--no_days"):
            no_days = int(arg)
         elif opt in ("-r", "--random"):
            random_flg = True
         elif opt in ("-v", "--verbose"):
            verbose_flg = True

   dates = []

   for n in range(no_days):
      d = date - datetime.timedelta(days=n)
      print d.strftime("%y%m%d")
      dates.append(d)

   # print date.strftime("%y%m%d")

   for d in dates:
      write_data(d)

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

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

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

Synthesizing AMEX Credit Card Data

Generates AMEX data file for import to SAP

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

import csv
import sys
import getopt
import random

#----- Define globals ---------------------------------------------------

debug_flg   = False
verbose_flg = False

max_no_tx   =    3  # Number of transactions to be created per CC

users         = []
cc_user_map   = {}

#----- Setup header, footer and body format strings ---------------------

header  = """\
...
"""

body_1 = """
...
"""

body_2 = """\
...
"""

#===== Define classes ===================================================

class User:
   pass

   def __init__(self, csv):
      self.UserId           = csv[0]
      self.APS              = csv[0]
      self.CostCenter       = csv[1]
      self.CreditCardNumber = csv[2]
      self.Name             = 'tester'

#===== Define functions =================================================
#----- Read in user data and set up credit card numbers in cc_no list ---

def read_user_data():
   users_in  = open("users.dat", "rb")

   reader    = csv.reader(users_in)

   for row in reader:
     user                               = User(row)
     users.append(user)
     cc_user_map[user.CreditCardNumber] = user

   users_in.close()  

#----- Now create a sorted list of keys to the cc catalog ---------------

def gen_cc_list():
   global cc_list

   cc_list  = cc_user_map.keys()

   cc_list.sort()   # cc_list is *NOW* a sorted list of credit card numbers.

#----- Write the file -----------------------------------------------------------

def generate_data():

   line_cnt    =    3   # Counts the number of lines in the file
   tx_id       =    0   # CC Transaction ID
   gen_cnt     =    0   # No of cards for which data was generated
   body_cnt    =    0   # No of card entries generated
   total_cost  =    0.0 # Total costs generated

   no_cards    = len(cc_list)

   sift = open("XXXXXX.SFT","w")

   sift.write(header)

   for i in range(no_cards): 
      card_no = cc_list[i]
      user    = cc_user_map[card_no]
      emp_no  = user.EmpNo
      name    = user.Name
      name_25 = name.ljust(25)
      name_26 = name.ljust(26)

      no_tx   = random.randint(0, max_no_tx)

      if no_tx > 0:
         if debug_flg: print card_no
         gen_cnt += 1

      tx_idx  = 0

      while tx_idx < no_tx:
         cost        = random.weibullvariate(alpha=3.0,beta=1) * 20.0
         description = "Description for transaction %05d".ljust(167) % tx_id

         total_cost += cost

         sift.write(body_1 %(card_no, name, aps, cost, cost, random.choice(["060901", "060831"]), description))  

         line_cnt   += 1
         tx_idx     += 1
         tx_id      += 1
         body_cnt   += 1

         sift.write(body_2 %(card_no, name_25, aps))

      line_cnt  +=1

   line_cnt +=2

   sift.write(footer % line_cnt)
   sift.close() 

   print "Generated %d cc entries for %d cards with total cost of $%8.2f" % (body_cnt, gen_cnt, total_cost)

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

def process():
   read_user_data()
   gen_cc_list()
   generate_data()

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

Usage = """
  Usage: AMEX_file.py  [--help] [--logfile=<filename>] [--date=<DATE>] [--verbose]

  Generates AMEX data file for import to SAP"    

  e.g:

    $ ./AMEX__file.py


"""

def usage():
    print >> sys.stderr, Usage

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

def main(argv):
   global debug_flg
   global verbose_flg
   global logfile
   global target_date

   try:
      opts, args = getopt.getopt(argv, "dhl:u:t",
                     ["date=", "debug", "help", "logfile=", "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 ("-D", "--date"):
            target_date = arg
         elif opt in ("-h", "--help"):
            usage()
            sys.exit(0)
         elif opt in ("-l", "--logfile"):
            logfile = arg
         elif opt in ("-v", "--verbose"):
            verbose_flg = True

   process()
 
#------------------------------------------------------------------------

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

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

Quick and Dirty Script to Generate Transaction Data

An early and very simple version of the script...

#!/usr/bin/env python

EMAIL = "xxx@performiq.com.au"

fmt = """\
...
"""

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

def gen_data(day):
   print "Generating data for date - %s" % day
   ofd = open("%s.txt" % day, "w+")
   ofd.write(fmt % (day, ))
   ofd.close()

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

dates = ['070612', '070613', '070614', '070615']

for day in dates:
   gen_data(day)

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