暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

python,实现MySQL的查询并发测试,并统计总耗时

原创 %Lucky 2023-03-07
1230

python脚本如下,可直接复制如下脚本,进行更改数据库连接信息与查询SQL,其余信息均不变化。

#!/usr/bin/env python
# coding = UTF-8
#Author:Lucky,time:2023/3/7

import time
import pymysql  # pip3 install PyMySQL
import threading
from time import ctime
from itertools import chain

threads_count = 75  # 并发线程数

def run_insert():
    #此处填写数据库的相关连接信息
    db = pymysql.connect(host="192.168.30.72", port=3001, user="root", passwd="Aa123456", db="tpch1t", charset="utf8")
    cur = db.cursor()
    sql ="select L_PARTKEY from LINEITEM where L_ORDERKEY=699959906;"
    cur.execute(sql)
    results = cur.fetchall()    #获取全部的查询结果,默认是元组的打印方式
    resultlist = list(chain.from_iterable(results))    #获取的结果用list方式打印
    print("resultlist:",resultlist)
    db.close()  # 关闭连接
    cur.close()    #关闭游标
local_var = threading.local()

def Clean(args):
    local_var.name = args
    run_insert()

threads = []
for i in range(threads_count):
    t = threading.Thread(target=Clean, args=(i,))
    threads.append(t)

print('start:', ctime())
start = time.time()

if __name__ == '__main__':
    for i in threads:
        i.start()
    for i in threads:
        i.join()

seconds = time.time() - start
print('end:', ctime())
print("总耗时:{sec}秒".format(sec=seconds))

打印结果显示:

/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 /Users/lucky/Desktop/python/a.py
start: Tue Mar  7 20:44:34 2023   #开始测试时间
resultlist: [66505992, 172600573]           #执行2个并发都某条SQL的查询结果打印
resultlist: [66505992, 172600573]
end: Tue Mar  7 20:44:34 2023        #结束测试时间

总耗时:0.14472699165344238秒

Process finished with exit code 0
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论