python测试mysql数据库性能(二)

一,普通写入数据库

二,批量写入数据库

三,普通写入数据库添加事务

config = {
    ‘host‘: ‘localhost‘,
    ‘port‘: 3306,
    ‘database‘: ‘test‘,
    ‘user‘: ‘root‘,
    ‘password‘: ‘1234qwer‘,
    ‘charset‘: ‘utf8‘
}

conn = pymysql.connect(**config)
cur = conn.cursor()

def timer(fn):
    def _wrapper(count):
        start = time.time()
        fn(count)
        seconds = time.time() - start
        print(u"{func}函数每 {count} 条数数据写入耗时 {sec}秒".format(func=fn, count=count, sec=seconds))
    return _wrapper

# 普通写入
@timer
def ordinary_insert(count):
    sql = "insert into students1 (name, age, sex,id,cellphone,address,score) values (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘)"
    for i in range(count):
        cur.execute(sql)

# 批量处理
@timer
def many_insert(count):
    sql = "insert into students1 (name, age, sex,id,cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"

    loop = count / 20
    stus = ((‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘),
            (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘),(‘tom666‘,‘66‘,‘boy‘,‘10066‘,‘13900000066‘,‘shanghai‘,‘66‘))

    for i in range(int(loop)):
        cur.executemany(sql, stus)

# 事务处理
@timer
def transaction_insert(count):
    sql = "insert into students1 (name, age, sex,id,cellphone,address,score)values(%s,%s,%s,%s,%s,%s,%s)"

    stus = (‘tom666‘, ‘66‘, ‘boy‘, ‘10066‘, ‘13900000066‘, ‘shanghai‘, ‘66‘)

    if count > 0:
        try:
            for i in range(count):
                cur.execute(sql, stus)
        except Exception as e:
            conn.rollback()  # 事务回滚
            print(‘事务处理失败‘, e)
        else:
            conn.commit()  # 事务提交
            print(‘事务处理成功, 关闭连接‘, cur.rowcount)
            cur.close()
            conn.close()
    else:
        print("输入的count有问题,无法执行数据库操作!")

def test_insert(count):
    ordinary_insert(count)
    many_insert(count)
    transaction_insert(count)

test_insert(20)

输出结果:

E:\python_projects\practises\venv\Scripts\python.exe E:/python_projects/practises/practise20191116/p20191208.py
<function ordinary_insert at 0x0000026994A7BC18>函数每20条数数据写入耗时0.003995656967163086秒
<function many_insert at 0x0000026994A7B8B8>函数每20条数数据写入耗时0.0009996891021728516秒
事务处理成功, 关闭连接 1
<function transaction_insert at 0x0000026994A7BA68>函数每20条数数据写入耗时0.007994651794433594秒

Process finished with exit code 0

原文地址:https://www.cnblogs.com/111testing/p/12004595.html

时间: 2024-08-03 06:39:04

python测试mysql数据库性能(二)的相关文章

python测试mysql写入性能完整实例

这篇文章主要介绍了python测试mysql写入性能完整实例,具有一定借鉴价值,需要的朋友可以参考下 本文主要研究的是python测试mysql写入性能,分享了一则完整代码,具体介绍如下. 测试环境: (1) 阿里云服务器centos 6.5 (2) 2G内存 (3) 普通硬盘 (4) mysql 5.1.73 数据库存储引擎为 InnoDB (5) python 2.7 (6) 客户端模块 mysql.connector 测试方法: (1) 普通写入 (2) 批量写入 (3) 事务加批量写入

运用Loadrunner测试Mysql数据库性能 TRON?极客

1.前言 针对数据库的性能测试,loadrunner本身支持sql server和oracle数据库,这两种数据库可以用loadrunner直接录制进行测试.而我们项目中使用的是mysql数据库,针对用 loadrunner测试mysql数据库的方法网上也有很多介绍文章,主要有两种方案.一种是利用ODBC连接测试mysql,但是这种方法配置比较麻 烦,如果要录制的话需要安装支持ODBC连接的查询分析器,这工具不好找,能找到的也只能算凑合能用.如果大家有兴趣试试这种方法,可以上网搜搜,学习配 置一

python操作mysql数据库(二)

在上一篇文章里面主要介绍了关于python3连接数据库,创建数据库以及创建表的相关内容,在接下来我们试着在我们刚才创建的表中插入数据,并对其做相关探究. #/usr/bin/env python #_*_coding:utf-8_*_ #导入pymysql模块 import pymysql #打开数据库链接 connect=pymysql.connect(host="192.168.186.157",port=3306,user="winner",passwd=&q

使用Python对MySQL数据库插入二十万条数据

1.当我们测试的时候需要大量的数据的时候,往往需要我们自己造数据,一条一条的加是不现实的,这时候就需要使用脚本来批量生成数据了. import pymysql import random import string # 建立数据库连接 mysql = pymysql.connect(host="数据库IP", user="数据库用户名",port=3306,password="数据库密码", charset='utf8', autocommit=

python + docker, 实现天气数据 从FTP获取以及持久化(二)-- python操作MySQL数据库

前言 在这一节中,我们主要介绍如何使用python操作MySQL数据库. 准备 MySQL数据库使用的是上一节中的docker容器 “test-mysql”. Python 操作 MySQL 我们使用的IDE是 “神奇” 的 pycharm: 1. 首先新建一个python的项目,并且安装 “mysql-connector-python”. “mysql-connector-python” 是MySQL官方对于python的数据驱动,感兴趣的童鞋可以移步这里: https://dev.mysql

python使用mysql数据库

一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装: Ubuntu\deepin >>sudo apt-get install mysql-server >>Sudo apt-get install  mysql-client centOS/redhat >

python使用mysql数据库(转)

一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装: Ubuntu\deepin >>sudo apt-get install mysql-server >>Sudo apt-get install  mysql-client centOS/redhat >

mysql数据库性能参数配置(转)

max_connections MySql的最大连接数,如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,当然这建立在机器能支撑的情况下,因为如果连接数越多,MySql会为每个连接提供连接缓冲区,就会开销越多的内存,连接数太大,服务器消耗的内存越多,以至于影响服务器性能,所以要根据服务器的配置适当调整该值,不能盲目提高设值.可以过'conn%'通配符查看当前状态的连接数量,以定夺该值的大小. show variables like 'max_connections' 最大连接数

Python使用MySQL数据库(新)(转)

http://www.cnblogs.com/fnng/p/3565912.html 一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装: Ubuntu\deepin >>sudo apt-get install mysql-server >>Sudo apt-g