代码改变世界

python mysql数据库压力测试

2019-12-08 21:38  清风软件测试开发  阅读(1208)  评论(0编辑  收藏  举报

python mysql数据库压力测试

pymysql 的执行时间对比

 1,装饰器,计算插入1000条数据需要的时间

def timer(func):
    def decor(*args):
        start_time = time.time()
        func(*args)
        end_time = time.time()
        d_time = end_time - start_time
        print("the running time is : ", d_time)
    return decor

@timer
def add_test_users(n):
    conn = pymysql.connect(host='localhost' ,port=3306 ,user='root', password='1234qwer', db='test', charset='utf8')
    cursor = conn.cursor()
    for i in range(0, n):
        try:
            sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
            param = (('Tom' + str(i), str(i), 'boy', str(10000 + i), str(1390000000+ i), 'shanghai', str(10 + i)))
            cursor.execute(sql, param)

        except Exception as e:
            return

    conn.commit()
    cursor.close()
    conn.close()
    print('OK')

add_test_users(10)

 

 2,装饰器,计算插入100条数据需要的时间

def timer(func):
    def decor(*args):
        start_time = time.time()
        func(*args)
        end_time = time.time()
        d_time = end_time - start_time
        print("the running time is : ", d_time)
    return decor

@timer
def add_test_users(n):
    usersvalues = []
    for i in range(1, n):
        usersvalues.append(('Tom' + str(i), str(i), 'boy', str(10000 + i), str(1390000000+ i), 'shanghai', str(10 + i)))
    conn = pymysql.connect(host='localhost' ,port=3306 ,user='root', password='1234qwer', db='test', charset='utf8')
    cursor = conn.cursor()
    cursor.executemany('insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)', usersvalues)

    conn.commit()
    cursor.close()
    conn.close()
    print('OK')

add_test_users(10)

 

对比execute和executemany 的耗时对比:

conn = pymysql.connect(host='localhost', port=3306, user='root', password='1234qwer', db='test', charset='utf8')
cur = conn.cursor()
values = []
for i in range(10):
    value = ('Tom' + str(i), str(i), 'boy', str(10000 + i), str(1390000000+ i), 'shanghai', str(10 + i))
    values.append(value)
sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
now_time = time.time()
try:
    cur.executemany(sql, values)
    conn.commit()
except Exception as err:
    print(err)
finally:
    cur.close()
    conn.close()
end_time = time.time()
print("executemany花费时间为: "+ str(end_time-now_time))


conn
= pymysql.connect(host='localhost', port=3306, user='root', password='1234qwer', db='test', charset='utf8') cur = conn.cursor() values = [] for i in range(10): value = ('Tom' + str(i), str(i), 'boy', str(10000 + i), str(1390000000+ i), 'shanghai', str(10 + i)) values.append(value) sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)" now_time = time.time() for val in values: print(val) try: cur.execute(sql, val) conn.commit() except Exception as err: print(err) finally: cur.close() conn.close() end_time = time.time() print("execute花费时间为: "+ str(end_time-now_time))

executemany花费时间为: 0.003998994827270508
execute花费时间为: 0.025983810424804688

Executemany 速度比execute快很多!!!

 

 

pymysql中 execute 和 executemany 性能对比 (外部文件导入)

conn = pymysql.connect(host='localhost', port=3306, user='root', password='1234qwer', db='test', charset='utf8')
cur = conn.cursor()
values = []
with open(r"C:\Users\Administrator\Desktop\students1.txt", "r+",encoding="utf-8") as fo:
    while True:
        line_txt = fo.readline().replace("\r","").replace("\n","")
        if not line_txt:
            break
        line_txt_txts = line_txt.split(',')
        values.append(line_txt_txts)
print(values)

sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
now_time = time.time()
try:
    cur.executemany(sql, values)
    conn.commit()
except Exception as err:
    print(err)
finally:
    cur.close()
    conn.close()
end_time = time.time()
print("executemany花费时间为: "+ str(end_time-now_time))
students2.txt 文件内容:
Tom1,20,boy,10001,13900000001,shanghai,91
Tom2,21,boy,10002,13900000002,shanghai,92
Tom3,22,boy,10003,13900000003,shanghai,93
Tom4,24,boy,10004,13900000004,shanghai,94
Tom5,25,girl,10005    ,13900000005,shanghai,95
Tom6,26,girl,10006    ,13900000006,shanghai,96
Tom7,27,girl,10007    ,13900000007,shanghai,97
Tom8,28,girl,10008    ,13900000008,shanghai,98
Tom9,29,boy,10009,13900000009,shanghai,99
Tom10,30,boy,10010,13900000010,shanghai,100
conn = pymysql.connect(host='localhost', port=3306, user='root', password='1234qwer', db='test', charset='utf8')
cur = conn.cursor()
values = []
with open(r"C:\Users\Administrator\Desktop\students1.txt", "r+",encoding="utf-8") as fo:
    while True:
        line_txt = fo.readline().replace("\r","").replace("\n","")
        if not line_txt:
            break
        line_txt_txts = line_txt.split(',')
        values.append(line_txt_txts)
print(values)
sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
now_time = time.time()
for val in values:
    print(val)
    try:
        cur.execute(sql, val)
        conn.commit()
    except Exception as err:
        print(err)
cur.close()
conn.close()

end_time = time.time()
print("execute花费时间为: "+ str(end_time-now_time))

外部导入txt文件流
executemany花费时间为: 0.004998683929443359
execute花费时间为: 0.030979633331298828

 

 

python多线程执行mysql

简单方式开启多线程
Def run(sql):
pass sql = 'select * from students1 where score = 90' t1 = threading.Thread(target=run, args=(sql,)) t2 = threading.Thread(target=run, args=(sql,)) t3 = threading.Thread(target=run, args=(sql,)) t1.start() t2.start() t3.start()

 

 

 

 多线程运行时间

def add_del_update_search():
    coon = pymysql.connect(host="localhost", port=3306, user="root", password="1234qwer", db="test", charset="utf8")
    sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
    param = ('tom555', '55', 'boy', '10055', '13900000055', 'shanghai', '55')
    cursor = coon.cursor()
    try:
        count = cursor.execute(sql, param)
        coon.commit()
        print(count)
    except Exception as e:
        print(e)
        coon.rollback()
    cursor.close()
    coon.close()

start_time = time.time()
t1 = threading.Thread(target=add_del_update_search)
t2 = threading.Thread(target=add_del_update_search)
t3 = threading.Thread(target=add_del_update_search)
t1.start()
t2.start()
t3.start()
end_time = time.time()
d_time = end_time - start_time
print("多线程运行时间是 : ", str(d_time))

单线程运行时间

def add_del_update_search():
    coon = pymysql.connect(host="localhost", port=3306, user="root", password="1234qwer", db="test", charset="utf8")
    sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
    param = ('tom555', '55', 'boy', '10055', '13900000055', 'shanghai', '55')
    cursor = coon.cursor()
    try:
        count1 = cursor.execute(sql, param)
        count2 = cursor.execute(sql, param)
        count3 = cursor.execute(sql, param)
        coon.commit()
        print(count1)
        print(count2)
        print(count3)
    except Exception as e:
        print(e)
        coon.rollback()
    cursor.close()
    coon.close()

start_time = time.time()
add_del_update_search()
end_time = time.time()
d_time = end_time - start_time
print(“单线程运行时间是 : ", str(d_time))

 

单线程 for 循环操作数据库

def add_del_update_search (n):
    coon = pymysql.connect(host="localhost", port=3306, user="root", password="1234qwer", db="test", charset="utf8")
    sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
    param = ('tom555', '55', 'boy', '10055', '13900000055', 'shanghai', '55')
    cursor = coon.cursor()
    for i in range(0, n):
        try:
            cursor.execute(sql, param)
            coon.commit()
        except Exception as e:
            return
    cursor.close()
    coon.close()

start_time = time.time()
add_del_update_search(100)
end_time = time.time()
d_time = end_time - start_time
print("单个线程运行时间是 : ", str(d_time))

多线程 for 循环操作数据库

def add_del_update_search():
    coon = pymysql.connect(host="localhost", port=3306, user="root", password="1234qwer", db="test", charset="utf8")
    sql = "insert into students1(name, age, sex, id, cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"
    param = ('tom555', '55', 'boy', '10055', '13900000055', 'shanghai', '55')
    cursor = coon.cursor()
    try:
        count = cursor.execute(sql, param)
        coon.commit()
    except Exception as e:
        print(e)
        coon.rollback()
    cursor.close()
    coon.close()

start_time = time.time()
for i in range (100):
    t = threading.Thread(target=add_del_update_search)
    t.start()

end_time = time.time()
d_time = end_time - start_time
print("多线程运行时间是 : ", str(d_time))