当前位置:网站首页>批量制造测试数据的思路,附源码
批量制造测试数据的思路,附源码
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
边栏推荐
- Cartoon: what are IAAs, PAAS, SaaS?
- 安装及管理程序
- 众昂矿业:萤石浮选工艺
- G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
- The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
- JSP learning 1
- 【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
- Qipengyuan horizon credible meta universe social system meets diversified consumption and social needs
- Day 9 static abstract class interface
- 力扣-198.打家劫舍
猜你喜欢
面试题 17.10. 主要元素
Hypermotion cloud migration helped China Unicom. Qingyun completed the cloud project of a central enterprise and accelerated the cloud process of the group's core business system
Use case execution of robot framework
What is the experience of using prophet, an open source research tool?
Creation of RAID disk array and RAID5
Day (5) of picking up matlab
Solution of garbled code on idea console
深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
Sort by character occurrence frequency 451
Real time operation of vim editor
随机推荐
There is a problem with the light switch from 1 to 100
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Use if else to judge in sail software - use the title condition to judge
Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
众昂矿业:萤石浮选工艺
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
You need to know about cloud disaster recovery
Force buckle-746 Climb stairs with minimum cost
Use case labeling mechanism of robot framework
Day 9 static abstract class interface
Set cell filling and ranking method according to the size of the value in the soft report
文件操作详解(2)
浅谈 NFT项目的价值、破发、收割之争
On the security of key passing and digital signature
Project framework of robot framework
最详细的背包问题!!!
100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
Esxi encapsulated network card driver
建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细