Python 压测Mysql、Doris

import pymysql
from dbutils.pooled_db import PooledDB
import time
from concurrent.futures import ThreadPoolExecutor

pool = PooledDB(
    creator=pymysql,
    host='',
    # host='172.18.1.16',
    user='root',
    password='',
    port=9030,
    connect_timeout=3880000,
    read_timeout=3880000,
    maxconnections=100,
    mincached=2,
    maxcached=5,
    maxshared=3,
    blocking=True,
    cursorclass=pymysql.cursors.DictCursor
)

count_z =0

def insert_data(thread_id, count):
    conn = pool.connection()
    with conn.cursor() as cursor:
        for i in range(count):
            start_time = time.time()  # 获取开始时间
            data_id = thread_id * count + i + 1
            cursor.execute(f"""
            insert into test.tbl_point_query1 (`key`,table_name,column_name,data_type) values ({data_id},'value1', 'value2', 'value3')
            """)
            conn.commit()
            end_time = time.time()  # 获取结束时间
            duration = end_time - start_time  # 计算执行时间

            print(f"Inserted data: {cursor.rowcount}, duration: {duration:.3f}s")  # 输出插入结果和执行时间
    conn.close()
    # return duration



start_time_z = time.time()
# 并发插入数据
with ThreadPoolExecutor(max_workers=2) as executor:
    futures = []
    for i in range(50):
        future = executor.submit(insert_data, i, 20)
        futures.append(future)
    for future in futures:
        future.result()
end_time_z = time.time()
duration = end_time_z - start_time_z  # 计算执行时间

print("All data inserted.")
print(duration)
# print(count_z)

你可能感兴趣的:(python,mysql,数学建模)