当前位置:网站首页>批量制造测试数据的思路,附源码
批量制造测试数据的思路,附源码
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
边栏推荐
- JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory
- 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
- Solution of garbled code on idea console
- Detailed explanation of file operation (2)
- What is the experience of using prophet, an open source research tool?
- The most detailed knapsack problem!!!
- Download and install mongodb
- Flask如何在内存中缓存数据?
- Use itextpdf to intercept the page to page of PDF document and divide it into pieces
- Redis "8" implements distributed current limiting and delay queues
猜你喜欢
The font of the soft cell changes color
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
You need to know about cloud disaster recovery
Sort by character occurrence frequency 451
Day 10 abnormal mechanism
JMeter installation tutorial and solutions to the problems I encountered
299. 猜数字游戏
Use case execution of robot framework
随机推荐
深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
How to conduct application security test (AST)
Disk management and file system
力扣-198.打家劫舍
Server log analysis tool (identify, extract, merge, and count exception information)
漫画:什么是IaaS、PaaS、SaaS?
MySQL的btree索引和hash索引区别
Gartner 發布新興技術研究:深入洞悉元宇宙
Redis "8" implements distributed current limiting and delay queues
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
Deepinv20 installation MariaDB
Passing header request header information between services through feign
MySQL master-slave synchronization pit avoidance version tutorial
Interview question 17.10 Main elements
Install MySQL on MAC
You need to know about cloud disaster recovery
Nacos detailed explanation, something
Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
04 Lua 运算符