MySQL压力测试脚本

数据库压测


import mysql.connector, threading

THREAD_NUM = 10
WORKER_NUM = 100

def working():
    conn = mysql.connector.connect(host='192.168.1.12', user='mogu', password='123456', database='blog')
    for i in range(WORKER_NUM):
        cursor = conn.cursor()
        cursor.execute('select * from wp_users where ID = %s', ('1',))
        values = cursor.fetchall()
        cursor.close()
    conn.close()

def main():
    Threads = []
    for i in range(THREAD_NUM):
        t = threading.Thread(target=working, name=str(i))
        t.setDaemon(True)
        Threads.append(t)
    for t in Threads:
        t.start()
    for t in Threads:
        t.join()
    print("main end")

if __name__ == "__main__":
    main()