Skip to content

Commit 706cc16

Browse files
committed
2.20.11.20 mtls-auto-fill 自动向表中插入数据
1 parent 814b6be commit 706cc16

File tree

7 files changed

+499
-28
lines changed

7 files changed

+499
-28
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.DS_Store
22
*.vscode
33
*.pyc
4-
*.retry
4+
*.retry
5+
dist*

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# file GENERATED by distutils, do NOT edit
22
setup.py
3+
bin/mtls-auto-fill
34
bin/mtls-backup
45
bin/mtls-big-files
56
bin/mtls-delete-rows

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,25 @@ homepage:**http://www.sqlpy.com**
1010

1111
---
1212

13+
- [# mysqltools-python权威指南](#-mysqltools-python权威指南)
1314
- [关于](#关于)
1415
- [安装](#安装)
15-
- [数据库监控项采集 -- mtls-monitor](#数据库监控项采集)
16-
- [数据库备份 -- mtls-backup](#数据库备份)
17-
- [慢查询日志切片分析 -- mtls-log ](#慢查询日志切片分析)
18-
- [tcp端口连通性测试 -- mtls-http](#tcp端口连通性测试)
19-
- [查询给定目录中的大文件 -- mtls-big-files](#查询给定目录中的大文件)
20-
- [温和删除表中的行 -- mtls-delete-rows](#温和删除表中的行)
21-
- [温和文件截断 -- mtls-file-truncate](#温和文件截断)
22-
- [数据库性能测试 -- mtls-perf-bench](#数据库性能测试)
23-
- [断开所有的客户端连接 -- mtls-kill-all-connections](#断开所有的客户端连接)
24-
- [统计慢查询文件中的SQL类型与热点表 -- mtls-sql-distribution](#统计慢查询文件中的SQL类型与热点表)
25-
- [表的最晚更新时间统计 -- mtls-file-stat](#表的最晚更新时间统计)
26-
- [找出长时间没有使用过的表 -- mtls-expired-tables](#找出长时间没有使用过的表)
27-
- [批量生成随机密码 -- mtls-random-passwd](#批量生成随机密码)
28-
- [自定义 mysql 消息 -- mtls-fake-mysqld](#自定义mysql消息)
16+
- [自动向表中插入随机数据](#自动向表中插入随机数据)
17+
- [数据库监控项采集](#数据库监控项采集)
18+
- [数据库备份](#数据库备份)
19+
- [tcp端口连通性测试](#tcp端口连通性测试)
20+
- [查询给定目录中的大文件](#查询给定目录中的大文件)
21+
- [慢查询日志切片分析](#慢查询日志切片分析)
22+
- [温和删除表中的行](#温和删除表中的行)
23+
- [温和文件截断](#温和文件截断)
24+
- [数据库性能测试](#数据库性能测试)
25+
- [断开所有的客户端连接](#断开所有的客户端连接)
26+
- [统计慢查询文件中的SQL类型与热点表](#统计慢查询文件中的sql类型与热点表)
27+
- [表的最晚更新时间统计](#表的最晚更新时间统计)
28+
- [找出长时间没有使用过的表](#找出长时间没有使用过的表)
29+
- [批量生成随机密码](#批量生成随机密码)
30+
- [自定义mysql消息](#自定义mysql消息)
31+
2932
---
3033

3134
## 关于
@@ -82,6 +85,11 @@ homepage:**http://www.sqlpy.com**
8285

8386
---
8487

88+
## 自动向表中插入随机数据
89+
90+
91+
---
92+
8593
## 数据库监控项采集
8694
**1): mysqltools-python已经实现的监控项列表**
8795

bin/mtls-auto-fill

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
1.59 KB
Binary file not shown.

0 commit comments

Comments
 (0)