栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

性能测试的环境以及测试数据构造

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

性能测试的环境以及测试数据构造

一、建立测试环境

在进行性能测试之前,需要先完成性能测试环境搭建工作;一般情况下性能测试环境可以要求运维和开发工程师协助完成;

测试环境一般包括:硬件环境、软件环境及网路环境;

1.1 性能测试环境到的特点

1)性能测试对测试环境要求独立性高,其他测试人员不可以使用该环境

  • 如果某个环境下运行多个系统,就很难判断其中的某个环境对资源的占用情况

2)尽量保证性能测试环境与真实生产环境的一致性

  • 1)硬件环境 :包括服务器的台数、硬件服务器的配置、网络环境
  • 2)软件环境:
    • 版本一致性:包括操作系、数据库、被测应用程序、第三方软件等
    • 配置一致性:包括操作系统、数据库、被测应用程序和第三方软件等
  • 3)使用场景的一致性
    • 测试数据:基础业务数据必须与用户环境一致;
    • 业务场景:发送请求与负载量与用户环境一致

1.2 如何保证性能环境与真实生产环境的一致性

1)硬件环境---找运维人员申请

2)软件环境--所有的版本和配置可以找开发人员要,然后自己安装

3)使用场景的一致性

测试数据:需要自己构造(通过python脚本构造相同数量级的数据)

业务场景:通过需求分析和运营数据获取主要的业务操作和对应的并发指标

二 、构造测试数据

在性能测试时,相同的sql语句在不同的数据库数据基础上,执行时间不同,因此需要构造与用户相同的大量级的数据;

select*from litemall_goods where id='1181000'

插入数据前的查询时间:0.003

插入10万条数据后的查询时间:0.009

目的:压测环境中的数据尽量与生产环境中的数据一致,

方法:为了创建大量数据,可以直接操作数据库进行添加;

  • 准备插入数据的SQL语句
  • 循环执行SQL语句插入数据
    • 导包
    • 连接数据库
    • 创建游标
    • 执行SQL语句
    • 关闭游标
    • 关闭连接

2.1构造商品数据

通过python脚本,构造10万条商品记录;

2.1.1 首先构造核心---编写sql语句

以轻商城4个核心数据表为例,在navicte中打开数据表,快速编写插入sql数据:

 同样的另外几个商品信息主表(litemall_goods_attribute;litemall_goods_product;和litemall_goods_specification,将sql语句摘出如下:

# 插入商品主表
INSERT INTO `litemall`.`litemall_goods`(`id`, `goods_sn`, `name`, `category_id`, `brand_id`, `gallery`, `keywords`, `brief`, `is_on_sale`, `sort_order`, `pic_url`, `share_url`, `is_new`, `is_hot`, `unit`, `counter_price`, `retail_price`, `detail`, `add_time`, `update_time`, `deleted`) VALUES ('{}', '{}', '母亲节礼物-舒适安睡组合_{}', 1008008, 1001020, '["http://yanxuan.nosdn.127.net/355efbcc32981aa3b7869ca07ee47dac.jpg", "http://yanxuan.nosdn.127.net/43e283df216881037b70d8b34f8846d3.jpg", "http://yanxuan.nosdn.127.net/12e41d7e5dabaf9150a8bb45c41cf422.jpg", "http://yanxuan.nosdn.127.net/5c1d28e86ccb89980e6054a49571cdec.jpg"]', '', '安心舒适是最好的礼物', 1, 1, 'http://yanxuan.nosdn.127.net/1f67b1970ee20fd572b7202da0ff705d.png', '', 1, 0, '件', 2618.00, 2598.00, '


', '2018-02-01 00:00:00', '2018-02-01 00:00:00', 0); # 插入商品参数表 INSERT INTO `litemall`.`litemall_goods_attribute`(`id`, `goods_id`, `attribute`, `value`, `add_time`, `update_time`, `deleted`) VALUES ('{}','{}' , '重要提醒', '活动时间:5月8日0点-5月14日24点。n请在以上时间段内购买,其余时间均不可享受组合装优惠。', '2018-02-01 00:00:00', '2018-02-01 00:00:00', 0); #插入商品货品表 INSERT INTO `litemall`.`litemall_goods_product`(`id`, `goods_id`, `specifications`, `price`, `number`, `url`, `add_time`, `update_time`, `deleted`) VALUES ('{}','{}' , '["1.5m床垫*1+枕头*2","玛瑙红"]', 1500.00, 198, 'quality=90&thumbnail=200x200&imageView', '2018-02-01 00:00:00', '2022-06-05 22:31:54', 0); #插入商品规格表 INSERT INTO `litemall`.`litemall_goods_specification`(`id`, `goods_id`, `specification`, `value`, `pic_url`, `add_time`, `update_time`, `deleted`) VALUES ('{}', '{}', '规格', '1.8m床垫*1+枕头*2', '', '2018-02-01 00:00:00', '2018-02-01 00:00:00', 0);

由于构造数据时部分数据具有唯一性,比如id是不能重复的,所以其value值要用占位符{}替换;

2.1.2 按照python构造数据步骤进行

# 导包
import pymysql

# 创建连接
conn = pymysql.connect(host="192.168.241.132", user="root", password="123456", database="litemall", port=3306)
# 创建游标
cursor = conn.cursor()
# 插入商品主表
INSERT INTO `litemall`.`litemall_goods`(`id`, `goods_sn`, `name`, `category_id`, `brand_id`, `gallery`, `keywords`, `brief`, `is_on_sale`, `sort_order`, `pic_url`, `share_url`, `is_new`, `is_hot`, `unit`, `counter_price`, `retail_price`, `detail`, `add_time`, `update_time`, `deleted`) VALUES ('{}', '{}', '母亲节礼物-舒适安睡组合_{}', 1008008, 1001020, '["http://yanxuan.nosdn.127.net/355efbcc32981aa3b7869ca07ee47dac.jpg", "http://yanxuan.nosdn.127.net/43e283df216881037b70d8b34f8846d3.jpg", "http://yanxuan.nosdn.127.net/12e41d7e5dabaf9150a8bb45c41cf422.jpg", "http://yanxuan.nosdn.127.net/5c1d28e86ccb89980e6054a49571cdec.jpg"]', '', '安心舒适是最好的礼物', 1, 1, 'http://yanxuan.nosdn.127.net/1f67b1970ee20fd572b7202da0ff705d.png', '', 1, 0, '件', 2618.00, 2598.00, '


', '2018-02-01 00:00:00', '2018-02-01 00:00:00', 0); # 插入商品参数表 INSERT INTO `litemall`.`litemall_goods_attribute`(`id`, `goods_id`, `attribute`, `value`, `add_time`, `update_time`, `deleted`) VALUES ('{}','{}' , '重要提醒', '活动时间:5月8日0点-5月14日24点。n请在以上时间段内购买,其余时间均不可享受组合装优惠。', '2018-02-01 00:00:00', '2018-02-01 00:00:00', 0); #插入商品货品表 INSERT INTO `litemall`.`litemall_goods_product`(`id`, `goods_id`, `specifications`, `price`, `number`, `url`, `add_time`, `update_time`, `deleted`) VALUES ('{}','{}' , '["1.5m床垫*1+枕头*2","玛瑙红"]', 1500.00, 198, 'quality=90&thumbnail=200x200&imageView', '2018-02-01 00:00:00', '2022-06-05 22:31:54', 0); #插入商品规格表 INSERT INTO `litemall`.`litemall_goods_specification`(`id`, `goods_id`, `specification`, `value`, `pic_url`, `add_time`, `update_time`, `deleted`) VALUES ('{}', '{}', '规格', '1.8m床垫*1+枕头*2', '', '2018-02-01 00:00:00', '2018-02-01 00:00:00', 0); goods_start = 100000 #为了避免表中原有的数据重合,从100000开始构建 for i in range(100000): goods_id = goods_start + i print("插入第{}条商品数据,商品id为{}".format(i + 1, goods_id)) cursor.execute(goods_sql.format(goods_id, goods_id, goods_id)) cursor.execute(goods_attr_sql.format(goods_id, goods_id)) cursor.execute(goods_product_sql.format(goods_id, goods_id)) cursor.execute(goods_spec_sql.format(goods_id, goods_id)) conn.commit() # 关闭游标 cursor.close() # 关闭连接 conn.close()

构建结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号