当前位置:网站首页>批量制造测试数据的思路,附源码
批量制造测试数据的思路,附源码
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
边栏推荐
- Day 10 abnormal mechanism
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
- RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page
- ESXi封装网卡驱动
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- 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
- 299. 猜数字游戏
- Sail soft segmentation solution: take only one character (required field) of a string
- Installation and management procedures
- Use if else to judge in sail software - use the title condition to judge
猜你喜欢
Use case execution of robot framework
Questions about disaster recovery? Click here
Interview question 17.10 Main elements
Using JSON server to create server requests locally
众昂矿业:萤石浮选工艺
1959年高考数学真题
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
Real time operation of vim editor
【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
随机推荐
The first line and the last two lines are frozen when paging
Day 9 static abstract class interface
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
Es common query, sorting and aggregation statements
About background image gradient()!
Deepinv20 installation MariaDB
Six scenarios of cloud migration
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
linux上啟動oracle服務
Disk management and file system
loggie 源码分析 source file 模块主干分析
Win11 / 10 home edition disables the edge's private browsing function
Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
Day (3) of picking up matlab
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
LVM and disk quota
How to quickly batch create text documents?
Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
Start Oracle service on Linux
04 Lua operator