Skip to content

Instantly share code, notes, and snippets.

@sennajox
Created November 16, 2012 05:58
Show Gist options
  • Save sennajox/92729a200ba7dcd08844 to your computer and use it in GitHub Desktop.
Save sennajox/92729a200ba7dcd08844 to your computer and use it in GitHub Desktop.
A python script for press testing mysql
#!/usr/bin/env python2
import sys, os
import random
import time
import MySQLdb
if __name__ == "__main__":
argc = len(sys.argv)
if argc < 2:
print "usage:%s ip [port] [user] [pwd]" % sys.argv[0]
exit(1)
ip = sys.argv[1]
port = 3306
if argc > 2:
port = int(sys.argv[2])
usr = "root"
if argc > 3:
usr = sys.argv[3]
pwd = ""
if argc > 4:
pwd = sys.argv[4]
print pwd
try:
# connect to mysql
conn = MySQLdb.connect(host=ip,port=port,user=usr,passwd=pwd)
cursor = conn.cursor()
count = 1000000
'''
for i in range(0, count):
'''
while True:
#sql = "update mytest.user set time = %d where user_id = %d" % (time.time(), random.randint(0, 30000))
id = random.randint(0, count)
cur_time = time.time()
sql = "insert into mytest.user values('%d', 'abc', '%d') on duplicate key update time = %d " % (id, cur_time, cur_time)
print sql
last = time.time()
cursor.execute(sql)
conn.commit()
print "elapsed time: %0.3f s" % (time.time() - last)
time.sleep(0.001)
except MySQLdb.Error, e:
cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
error = "%s - Error %d: %s" % (cur_time, e.args[0], e.args[1])
print error
exit(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment