当前位置:网站首页>批量制造测试数据的思路,附源码
批量制造测试数据的思路,附源码
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
边栏推荐
- Oracle data pump usage
- Project framework of robot framework
- Installation and management procedures
- JSP learning 2
- What does cloud disaster tolerance mean? What is the difference between cloud disaster tolerance and traditional disaster tolerance?
- On the value, breaking and harvest of NFT project
- logback的配置文件加载顺序
- Dlib of face recognition framework
- JMeter installation tutorial and solutions to the problems I encountered
- 磁盘管理与文件系统
猜你喜欢
PyTorch:train模式与eval模式的那些坑
Force buckle - 198 raid homes and plunder houses
New project of OMNeT learning
Install MySQL on MAC
Interview question 17.10 Main elements
Deepinv20 installation MariaDB
Force buckle-746 Climb stairs with minimum cost
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
Set cell filling and ranking method according to the size of the value in the soft report
随机推荐
linux上啟動oracle服務
第十天 异常机制
Win11 / 10 home edition disables the edge's private browsing function
Pytorch: the pit between train mode and eval mode
Solution of garbled code on idea console
浅谈 NFT项目的价值、破发、收割之争
文件操作详解(2)
MySql主从复制
Esxi encapsulated network card driver
100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
File system read and write performance test practice
What is homebrew? And use
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
About JMeter startup flash back
Sail soft segmentation solution: take only one character (required field) of a string
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
How to upgrade openstack across versions
Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
MySQL的btree索引和hash索引区别
04 Lua operator