当前位置:网站首页>批量制造测试数据的思路,附源码
批量制造测试数据的思路,附源码
2022-04-23 16:38:00 【测试开发Kevin】
造数据核心思想
在性能测试的工作中,我们经常会遇到造数据的需求,这个工作可以让开发同学来配合,但这必定还要看开发同学的脸色行事!遇到nice的开发同学自然工作顺利,但是遇到配合度不高的开发同学,往往需要协调领导来促成此事,这就比较麻烦了!本文就通过循环批量插入数据的实例来模拟造数据这个场景,提供的都是造数据所需的干货SQL代码!本文默认读者了解基础sql语句和存储过程的编写方法。
造数据的核心思想很简单:就是编写存储过程,循环插入数据
造数据实例
这里通过向mock数据库插入数据来给大家做demo演示:
首先,创建一张表api_record2,字段信息如下:
CREATE TABLE api_record2(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
app_name VARCHAR(30) DEFAULT 'test',
host_ip VARCHAR(30),
access_time DATETIME,
method VARCHAR(10),
url VARCHAR(1000),
api_data VARCHAR(1000),
return_value VARCHAR(1000),
INDEX index_app_name ( app_name )
);
具体的字段信息含义这里就不多做解释了(相信大家看命名就能够猜测出其含义,如果有问题欢迎留言),id是主键,app_name加了索引。
接下来就是写存储过程了,代码如下:
DELIMITER ;;
DROP PROCEDURE IF EXISTS `insertdata`;
CREATE PROCEDURE insertdata()
BEGIN
DECLARE i INT;
SET i=1;
WHILE(i<=10)DO
INSERT INTO api_record2 (app_name,host_ip,access_time,method,url,api_data,return_value) VALUES (CONCAT('test',FLOOR( 1 + RAND() * (100 - 1))),'127.0.0.1',NOW(),'post','/yourapp_mock/func2','{"user":"kevin","pwd":"123",}','ok');
SET i=i+1;
END WHILE;
COMMIT; #如果是大数据量,建议在加一层循环,把commit放在这层循环的外部,不至于事物失败,全部回滚
END;;
DELIMITER ;
CALL insertdata();
存储过程语句分析
DELIMITER ;; 语句;;
delimiter是mysql分隔符,在mysql客户端中分隔符默认是分号,一般情况下,在客户端命令行中,如果一行SQL命令以“;”结尾,回车后,会执行该条命令。如在创建存储过程,函数等,这时就需要全部输入一段命令,最后再一次性执行,此时解释器就需要通过delimiter来处理,把分号换成其他符号,如:“//”或“;;”,这样能够解释该段命令中的分号,实现一次性执行该段命令,否则会报错,无法执行。
DROP PROCEDURE IF EXISTS `insertdata` 如果存储过程insertdata存在,则放弃执行
CREATE PROCEDURE insertdata() 创建存储过程
BEGIN 事务开始
END 事务结束
COMMIT 事务提交
WHILE(i<=10)DO
insert sql 语句
SET i=i+1;
END WHILE;
批量插入数据核心:循环插入10条语句,语句实现与其他语言的循环语句几乎一致
insert语句中CONCAT('test',FLOOR( 1 + RAND() * (100 - 1))) 表示应用名称为:test开头+1-99的随机数,主要目的是给大家讲解如何插入随机数据
insert语句中NOW()表示获取当前时间
CALL insertdata(); 执行存储过程
这个例子比较简单,但已经很清晰的解释了造数据的思路和具体方法,相信大家可以结合自己的业务写出更为复杂的sql语句然后进行insert操作!原创不易,如果文章帮助了大家欢迎点赞转发!
版权声明
本文为[测试开发Kevin]所创,转载请带上原文链接,感谢
https://blog.csdn.net/liwenxiang629/article/details/124315849
边栏推荐
- Findstr is not an internal or external command workaround
- Database dbvisualizer Pro reported file error, resulting in data connection failure
- Research and Practice on business system migration of a government cloud project
- Interview question 17.10 Main elements
- Oracle data pump usage
- 第十天 异常机制
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
- 5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
- [pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
- 无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
猜你喜欢
Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
漫画:什么是IaaS、PaaS、SaaS?
Install MySQL on MAC
The first line and the last two lines are frozen when paging
Install redis and deploy redis high availability cluster
Government cloud migration practice: Beiming digital division used hypermotion cloud migration products to implement the cloud migration project for a government unit, and completed the migration of n
DDT + Excel for interface test
Gartner 發布新興技術研究:深入洞悉元宇宙
Dlib of face recognition framework
随机推荐
建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
Sail soft segmentation solution: take only one character (required field) of a string
Selenium IDE and XPath installation of chrome plug-in
力扣-198.打家劫舍
logback的配置文件加载顺序
欣旺达:HEV和BEV超快充拳头产品大规模出货
Loading order of logback configuration file
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
PyTorch:train模式与eval模式的那些坑
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
Installation and management procedures
On the security of key passing and digital signature
Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function
Day 10 abnormal mechanism
第十天 异常机制
NVIDIA显卡驱动报错
Oak-d raspberry pie cloud project [with detailed code]
Real time operation of vim editor
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
MySQL master-slave synchronization pit avoidance version tutorial