Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os, sys
  4. import random
  5. import time
  6. import string
  7. import mysql.connector
  8. import threading
  9.  
  10. from mysql.connector import errorcode
  11. from mysql.connector.errors import Error
  12.  
  13. # For use in signaling
  14. shutdown_event = threading.Event()
  15.  
  16. # Set username, password, etc
  17. dbconfig = {
  18. "user":"imdb",
  19. "password":"imdb",
  20. "database":"imdb"
  21. }
  22.  
  23. def create_table():
  24. query = u"""CREATE TABLE IF NOT EXISTS imdb.employees (
  25. id INT NOT NULL AUTO_INCREMENT,
  26. fname VARCHAR(30),
  27. lname VARCHAR(30),
  28. hired DATE NOT NULL DEFAULT '1970-01-01',
  29. separated DATE NULL,
  30. job_code CHAR(3) NOT NULL,
  31. store_id INT NOT NULL,
  32. PRIMARY KEY (id))"""
  33.  
  34. cnx = mysql.connector.connect(**dbconfig)
  35. cursor = cnx.cursor()
  36. cursor.execute(query)
  37. cursor.close()
  38. cnx.close()
  39. print "Created table"
  40.  
  41. def rnd_user(num=1000001, threadid=1):
  42. query = u"INSERT INTO imdb.employees (fname, lname, hired, job_code, store_id) VALUES ('%(fname)s','%(lname)s','%(hired)s','%(jobcode)s','%(storeid)s');"
  43. cnx = mysql.connector.connect(**dbconfig)
  44. cnx.autocommit = True
  45. cursor = cnx.cursor()
  46.  
  47. def rnd_date():
  48. return time.strftime("%Y-%m-%d", (random.randrange(2000,2016), random.randrange(1,12), random.randrange(1,28), 0, 0, 0, 0, 1, -1))
  49.  
  50. for x in range(num):
  51. if not shutdown_event.is_set():
  52. fname = genstring(3, 9)
  53. lname = genstring(4, 12)
  54. hired = rnd_date()
  55. jobcode = genstring(3, 3).upper()
  56. storeid = random.randrange(1, 20)
  57.  
  58. cursor.execute(query % {u'fname': fname, u'lname': lname, u'hired': hired, u'jobcode': jobcode, u'storeid': storeid})
  59.  
  60. if x % 1000 == 0:
  61. print "[%2d] Inserted %d rows" % (threadid, x)
  62.  
  63. cnx.close()
  64.  
  65. def genstring(lim_down=3, lim_up=9):
  66. alpha = random.randint(lim_down, lim_up)
  67. vowels = ['a','e','i','o','u']
  68. consonants = [a for a in string.ascii_lowercase if a not in vowels]
  69.  
  70. def a_part(slen):
  71. ret = ''
  72. for i in range(slen):
  73. if i%2 == 0:
  74. randid = random.randint(0,20) #number of consonants
  75. ret += consonants[randid]
  76. else:
  77. randid = random.randint(0,4) #number of vowels
  78. ret += vowels[randid]
  79. return ret
  80.  
  81. fpl = alpha/2
  82. if alpha % 2 :
  83. fpl = int(alpha/2) + 1
  84. lpl = alpha - fpl
  85.  
  86. start = a_part(fpl)
  87. end = a_part(lpl)
  88.  
  89. return "%s%s" % (start.capitalize(), end)
  90.  
  91. def main():
  92.  
  93. # Make sure user account works
  94. try:
  95. cnx = mysql.connector.connect(**dbconfig)
  96. except mysql.connector.Error as err:
  97. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  98. print("Something is wrong with your user name or password.")
  99. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  100. print("Database does not exist")
  101. else:
  102. print("Other Error: %s" % err)
  103. os._exit(1)
  104. else:
  105. cnx.close()
  106.  
  107. # Create the table
  108. create_table()
  109.  
  110. # Hold threads
  111. threads = []
  112. threadId = 1
  113.  
  114. # Loop/create/start threads
  115. for x in range(8):
  116. t = threading.Thread(target=rnd_user, args=(125000,threadId,))
  117. t.start()
  118. threads.append(t)
  119. threadId += 1
  120.  
  121. print "Waiting for threads to complete..."
  122.  
  123. try:
  124. for i in threads:
  125. i.join(timeout=1.0)
  126. except (KeyboardInterrupt, SystemExit):
  127. print "Caught Ctrl-C. Cleaning up. Exiting."
  128. shutdown_event.set()
  129.  
  130. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement