|
| 1 | +#!/usr/bin/evn python3 |
| 2 | +""" |
| 3 | +创建指定结构的表、并向其插件数据、主要用于分析数据库实例的写性能 |
| 4 | +""" |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import time |
| 8 | +import mysql |
| 9 | +import string |
| 10 | +import logging |
| 11 | +import argparse |
| 12 | +import threading |
| 13 | +from mysql import connector |
| 14 | +from mysql.connector import errorcode |
| 15 | +from collections import namedtuple |
| 16 | + |
| 17 | + |
| 18 | +if '.' in __file__: |
| 19 | + name,_ = __file__.split('.') |
| 20 | +else: |
| 21 | + name = __file__ |
| 22 | + |
| 23 | +def check_python_version() -> None: |
| 24 | + """ |
| 25 | + 检测当前的 python 版本是否被支持,只支持 python-3.0.x 以上的环境 |
| 26 | + """ |
| 27 | + if sys.version_info.major <= 2: |
| 28 | + print("only support python-3.x",file=sys.stderr) |
| 29 | + sys.exit(1) |
| 30 | + |
| 31 | +def parse_cmd_arags() -> argparse.ArgumentParser: |
| 32 | + """ |
| 33 | + 处理命令行参数 |
| 34 | + """ |
| 35 | + def to_bool(value): |
| 36 | + return value.upper() in ['YES','TRUE','1','ON'] |
| 37 | + |
| 38 | + parser = argparse.ArgumentParser(name) |
| 39 | + parser.add_argument('--host',type=str,default="127.0.0.1",help="mysql host") |
| 40 | + parser.add_argument('--port',type=int,default=3306,help="mysql port") |
| 41 | + parser.add_argument('--user',type=str,default='appuser',help="mysql user") |
| 42 | + parser.add_argument('--password',type=str,default='apps_352',help="mysql user's passowrd ") |
| 43 | + parser.add_argument('--database',type=str,default="tempdb",help="work schema(database)") |
| 44 | + parser.add_argument('--table',type=str,default="t",help="work table") |
| 45 | + parser.add_argument('--thread',type=int,default=1,help='parralel threads') |
| 46 | + parser.add_argument('--rows',type=int,default=100,help="rows") |
| 47 | + parser.add_argument('--log-level',type=str,choices=['info','debug','error'],default="info") |
| 48 | + parser.add_argument('--auto-primary-key',type=to_bool,default=True,choices=[False,True],help="whether table has primary key") |
| 49 | + parser.add_argument('--ints',type=int,default=0,help="int column counts") |
| 50 | + parser.add_argument('--floats',type=int,default=0,help="float column counts") |
| 51 | + parser.add_argument('--doubles',type=int,default=0,help='double column counts') |
| 52 | + parser.add_argument('--varchars',type=int,default=0,help="varchar column counts") |
| 53 | + parser.add_argument('--varchar-length',type=int,default=128,help="varchar column length default 128") |
| 54 | + parser.add_argument('--decimals',type=int,default=0,help="decimal column counts") |
| 55 | + parser.add_argument('--decimal-precision',type=int,default=12,help="total digits length") |
| 56 | + parser.add_argument('--decimal-scale',type=int,default=2,help="the scale of decimal(the number of digits to the right of the decimal point)") |
| 57 | + parser.add_argument('action',type=str,choices=['create','drop','insert']) |
| 58 | + arags = parser.parse_args() |
| 59 | + return arags |
| 60 | + |
| 61 | +def config_logger(args:argparse.ArgumentParser) -> None: |
| 62 | + """ |
| 63 | + 配置日志的输出格式 |
| 64 | + """ |
| 65 | + logger = logging.getLogger(name) |
| 66 | + if args.log_level == "debug": |
| 67 | + logger.setLevel(logging.DEBUG) |
| 68 | + elif args.log_level == "info": |
| 69 | + logger.setLevel(logging.INFO) |
| 70 | + elif args.log_level == "error": |
| 71 | + logger.setLevel(logging.ERROR) |
| 72 | + |
| 73 | + handler = logging.StreamHandler(sys.stderr) |
| 74 | + formater = logging.Formatter("%(asctime)s %(name)s %(process)d %(threadName)s %(levelname)s %(message)s") |
| 75 | + handler.setFormatter(formater) |
| 76 | + logger.addHandler(handler) |
| 77 | + |
| 78 | +def create(args:argparse.ArgumentParser): |
| 79 | + """ |
| 80 | + 根据 args 指定的参数来创建表 |
| 81 | + """ |
| 82 | + logger = logging.getLogger(name) |
| 83 | + columns = [] |
| 84 | + # 检查是否自动加 primary key . |
| 85 | + if args.auto_primary_key == True: |
| 86 | + columns.append("id int not null auto_increment primary key") |
| 87 | + # 检查 int 字段的数量 |
| 88 | + if args.ints >= 1: |
| 89 | + for i in range(args.ints): |
| 90 | + columns.append(f"i{i} int not null") |
| 91 | + # 检查 varchar 字段的数量 |
| 92 | + if args.varchars >= 1: |
| 93 | + for c in range(args.varchars): |
| 94 | + columns.append(f"c{c} varchar({args.varchar_length}) not null") |
| 95 | + # 检查 float 字段的数量 |
| 96 | + if args.floats >= 1: |
| 97 | + for f in range(args.floats): |
| 98 | + columns.append(f"f{f} float not null") |
| 99 | + # 检查 double 字段的数量 |
| 100 | + if args.doubles >= 1: |
| 101 | + for d in range(args.doubles): |
| 102 | + columns.append(f"d{d} double not null") |
| 103 | + # 检查 decimal 字段的数量 |
| 104 | + if args.decimals >= 1: |
| 105 | + if args.decimal_precision < args.decimal_scale: |
| 106 | + logger.error("decimal-precision argument must big then decimal-scale") |
| 107 | + sys.exit(2) |
| 108 | + if args.decimal_precision <=0: |
| 109 | + logger.error("decimal-precision argument must big then 0") |
| 110 | + sys.exit(3) |
| 111 | + for d in range(args.decimals): |
| 112 | + columns.append(f"dm{d} decimal({args.decimal_precision},{args.decimal_scale})") |
| 113 | + |
| 114 | + # 拼接 SQL |
| 115 | + sql = f"create table {args.database}.{args.table} ( {','.join(columns)});" |
| 116 | + |
| 117 | + cnx = None |
| 118 | + try: |
| 119 | + cnx = connector.connect(host=args.host,port=args.port,user=args.user,password=args.password,database=args.database) |
| 120 | + cursor = cnx.cursor() |
| 121 | + except connector.Error as err: |
| 122 | + if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: |
| 123 | + logger.error(f"host={args.host} port={args.port} user={args.user},passwrod={args.password}") |
| 124 | + finally: |
| 125 | + if cnx != None and hasattr(cnx,'close'): |
| 126 | + cnx.close() |
| 127 | + |
| 128 | + logger.info(sql) |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +def main(): |
| 135 | + check_python_version() |
| 136 | + args = parse_cmd_arags() |
| 137 | + config_logger(args) |
| 138 | + if args.action == 'create': |
| 139 | + create(args) |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
| 145 | + logger = logging.getLogger(name) |
| 146 | + logger.info("hello world.") |
| 147 | + |
0 commit comments