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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值