0
点赞
收藏
分享

微信扫一扫

Python实现MySQL 压测工具

   mysql存储过程Procedure可以循环建立1000万行的测试数据,可以参考python/insert_mysql_benchmark.py at main · laufei90/python (github.com)

1、argparse模块解析命令行参数

    --host :数据库地址;

    --user:用户名

    --password:密码

    --port:端口

    --thread_size:线程数

    --row_size:插入数据行数

    -v:版本号

2.、创建数据库并创建数据表

    create_db_and_table函数,创建数据库test_insert_data_db及数据表test_insert_data_table。

3、插入数据到数据库

    random_string函数:随机生成字母和数字组成的字符串作为数据表的字段name 。

    add_row函数:调用execute方法插入一行数据。

    insert_data函数:打开数据库连接,插入指定的row_size。

4、主函数多线程插入数据

5、测试结果如下:

   命令行中使用time来查看insert_mysql_benchmark.py插入1万行数据的时间为5.8秒。

    time python3 insert_mysql_benchmark.py --host=192.168.15.219 --user=phpmyadmin --password=phpmyadmin --port=3306 --thread_size=10 --row_size=1000

举报

相关推荐

0 条评论