测试工程师之数据生成(python连接数据库生成)

最近在公司需要进行性能测试,所以学习了一下第一步造数据,初始化环境;也是记录一下最近学习的,做一些总结以及加深印象。本人也是初次学习,有什么需要优化的地方,还希望大家可以指出来,在此还谢过大家!!!

首先造数据的方式很多,我只选择了我觉得最简单的一种而已,毕竟我的代码能力不强,需要学习的东西也是太多了~~
我使用的是python代码连接数据库进行生成,不多说,先上代码。

import pymysql
import time
import random
from faker import Faker
from concurrent.futures import ProcessPoolExecutor
from Crypto.Cipher import AES
import base64

#封装一个加密算法AES-128-CBC,数据采用PKCS#7填充,
#因为公司的数据需要这个加密
class Encrypt:
    def __init__(self, key='默认值', iv = '默认值'):
        self.key = key.encode('utf-8')
        self.iv = iv.encode('utf-8')
        
    def pkcs7padding(self, text):
        """明文使用PKCS7填充 """
        bs = 16
        length = len(text)
        bytes_length = len(text.encode('utf-8'))
        padding_size = length if (bytes_length == length) else bytes_length
        padding = bs - padding_size % bs
        padding_text = chr(padding) * padding
        self.coding = chr(padding)
        return text + padding_text

    def aes_encrypt(self, content):
        """ AES加密 """
        cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
        # 处理明文
        content_padding = self.pkcs7padding(content)
        # 加密
        encrypt_bytes = cipher.encrypt(content_padding.encode('utf-8'))
        # 重新编码
        result = str(base64.b64encode(encrypt_bytes), encoding='utf-8')
        return result

#这里封装的是对手机号码中间进行掩码处理
	def phonelist1(self,phone):
	    # 取出中间四位
	    list = phone[3:7]
	    # 加密
	    newphone = phone.replace(list, '****')
	    return newphone

#这里封装连接数据库以及数据生成
def data_handler(urls):
    conn = pymysql.connect(host='数据库地址',
                         port=3306, user='用户名',
                         password='密码',
                         db = '数据库名',
                         charset='utf8')
    cursor = conn.cursor()

    for i in range(urls[0], urls[1]):
        cas = Encrypt()
        fake = Faker("zh_CN")
        name = "压测用户"+ str(i)
        main = random.randint(0, 1)
        phone = fake.phone_number()
        mobile1 = cas.aes_encrypt(phone)
        mobile2 = cas.phonelist1(phone)
        status_flag = 1
        version_num = 1
        
        try:
            sql = 'insert into user(name,mobile,mobile_one,main,status_flag,version_num) values(%s,%s,%s,%s,%s,%s)'
            cursor.execute(sql, [name, mobile1, mobile2, main, status_flag,version_num])
            conn.commit()

        except pymysql.Error as e:
            print("插入失败"+str(e))
        finally:
            pass

    cursor.close()
    conn.close()

#这里使用进程去执行数据生成,加快速度
def run():
    urls = [(1,20000),(20001,40000),(40001,60000),(60001,80000)]
    with ProcessPoolExecutor() as excute:
        ##ProcessPoolExecutor 提供的map函数,可以直接接受可迭代的参数,并且结果可以直接for循环取出
        excute.map(data_handler,urls)

if __name__ == '__main__':
    start_time = time.time()
    run()
    stop_time = time.time()
    print('插入数据耗时 %.2fs' % (stop_time - start_time))

写得不够好,还要继续优化;

参考资料:
https://blog.csdn.net/qq_36562656/article/details/107617866
https://blog.csdn.net/weixin_39736547/article/details/111858643
https://blog.csdn.net/zhangcongyi420/article/details/103323126

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值