|
| 1 | +#!/usr/bin/env python3 |
| 2 | +#-*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +自动的向给定的库表中完全随机的填充数据 |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import logging |
| 11 | +import argparse |
| 12 | +import concurrent.futures |
| 13 | +from mysql import connector |
| 14 | +from datetime import datetime |
| 15 | +from collections import namedtuple |
| 16 | +from mtls.values import InsertSQL,TableMeta |
| 17 | +from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor |
| 18 | + |
| 19 | + |
| 20 | +name = os.path.basename(__file__) |
| 21 | + |
| 22 | +InsertStat = namedtuple('InsertStat','start_at end_at tps cost_time') |
| 23 | + |
| 24 | +def parser_cmd_args(): |
| 25 | + """ |
| 26 | + 处理命令行参数 |
| 27 | + """ |
| 28 | + args = argparse.ArgumentParser(name) |
| 29 | + args.add_argument("--host",type=str,default="127.0.0.1",help="MySQL 主机 IP ") |
| 30 | + args.add_argument("--port",type=int,default=3306,help="MySQL 端口") |
| 31 | + args.add_argument("--user",type=str,default="appuser",help="用户名") |
| 32 | + args.add_argument("--password",type=str,default="mtls@0352",help="密码") |
| 33 | + args.add_argument("--database",type=str,default="tempdb",help="库名") |
| 34 | + args.add_argument("--table",type=str,default="t",help="表名") |
| 35 | + args.add_argument("--rows",type=int,default=100,help="要插入的行数") |
| 36 | + args.add_argument("--process",type=int,default=1,help="并发的进程数") |
| 37 | + args.add_argument("action",choices=('review','execute')) |
| 38 | + return args.parse_args() |
| 39 | + |
| 40 | + |
| 41 | +def insert(host="127.0.0.1",port=3306,user="apuser",password="mtls@0352",database="tempdb",table="t",rows=100): |
| 42 | + """ |
| 43 | +
|
| 44 | + """ |
| 45 | + t_meta = TableMeta(host,port,user,password,database,table) |
| 46 | + |
| 47 | + if t_meta.err != None and len(t_meta.meta): |
| 48 | + # |
| 49 | + logging.exception(t_meta.err) |
| 50 | + return [] |
| 51 | + |
| 52 | + # 如果执行到这里,说明表存在,并且可以正常的取得元数据 |
| 53 | + meta = [_ for _ in t_meta.meta] |
| 54 | + ist = InsertSQL(database,table,meta) |
| 55 | + |
| 56 | + # |
| 57 | + cnx = None |
| 58 | + start_at = datetime.now() |
| 59 | + try: |
| 60 | + cnx = connector.connect(host=host,port=port,user=user,password=password) |
| 61 | + cursor = cnx.cursor() |
| 62 | + |
| 63 | + for i in range(rows): |
| 64 | + sql,args,*_ = ist[i] |
| 65 | + cursor.execute(sql,args) |
| 66 | + cnx.commit() |
| 67 | + |
| 68 | + except Exception as err: |
| 69 | + logging.exception(str(err)) |
| 70 | + return None |
| 71 | + finally: |
| 72 | + if hasattr(cnx,'close'): |
| 73 | + cnx.close() |
| 74 | + end_at = datetime.now() |
| 75 | + |
| 76 | + # 微秒级的精度 |
| 77 | + cost_time = (end_at - start_at).microseconds |
| 78 | + tps = (rows/cost_time) * 1000000 |
| 79 | + stat = InsertStat(start_at=start_at,end_at=end_at,tps=tps,cost_time=cost_time/1000000) |
| 80 | + return stat |
| 81 | + |
| 82 | +def create_report(stats:InsertStat=None): |
| 83 | + """ |
| 84 | + """ |
| 85 | + assert stats is not None |
| 86 | + sum_tps = 0 |
| 87 | + sum_cost_time = 0 |
| 88 | + for s in stats: |
| 89 | + if hasattr(s,'tps'): |
| 90 | + sum_tps = sum_tps + s.tps |
| 91 | + sum_cost_time = sum_cost_time + s.cost_time |
| 92 | + |
| 93 | + tps = sum_tps / len(stats) |
| 94 | + cost_time = sum_cost_time / len(stats) |
| 95 | + |
| 96 | + print("-"*36) |
| 97 | + print(f"|tps = {tps}") |
| 98 | + print(f"|cost_time = {cost_time}") |
| 99 | + print("-"*36) |
| 100 | + |
| 101 | + |
| 102 | +def main(): |
| 103 | + """ |
| 104 | + """ |
| 105 | + |
| 106 | + args = parser_cmd_args() |
| 107 | + # 多进程压力测试 |
| 108 | + stats = [] |
| 109 | + if args.process > 1: |
| 110 | + # 创建进程池 |
| 111 | + with ProcessPoolExecutor(max_workers=args.process) as e: |
| 112 | + futures = [e.submit(insert,args.host,args.port,args.user,args.password,args.database,args.table,args.rows) |
| 113 | + for i in range(args.process)] |
| 114 | + |
| 115 | + for future in concurrent.futures.as_completed(futures): |
| 116 | + # 取得“期物的值”以此来触发执行 |
| 117 | + _ = future.result() |
| 118 | + stats.append(_) |
| 119 | + else: |
| 120 | + # 单进程压力测试 |
| 121 | + tmp = insert(args.host,args.port,args.user,args.password,args.database,args.table,args.rows) |
| 122 | + stats.append(tmp) |
| 123 | + |
| 124 | + print("\nReport:") |
| 125 | + create_report(stats) |
| 126 | + print("Compelete.\n") |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
| 132 | + |
0 commit comments