当前位置:网站首页>gatling 之性能测试
gatling 之性能测试
2022-07-04 15:44:00 【Wu_Candy】
一、创建项目
使用mvn创建项目模板
1、“cd 到你要创建这个项目的文件夹下”
2、输入:mvn archetype:generate # 加载完毕后
3、输入:gatling # 筛选出gatling的mvn原型模板
4、 输入:1 # 因为只筛选出一个,所以输入1即可
5、输入: 20 # 要安装的版本的序号",20为 gatling2.3.1
6、输入:com.klicen # groupid 组名,一般为XX.XX 第一个XX为域com、cn、org等,第二个XX为公司名称
7、输入:klicen # artifactId 坐标ID,一般为项目名称,会在当前目录创建一个同名的文件夹
8、输入:1.0 # version 版本名称
9、输入: klicen # package 包名
10、输入:Y # 确认
二、IntelliJ IDEA导入项目
1、导入项目--点击file-open-选择我们刚才新建的项目模板进行导入
2、安装scala插件,可以直接用zip 安装到本地
3、在项目src/test/scala 下创建scala class
如下示例:
package cnblogsCase
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class baidu extends Simulation{
//第一步:定义一个要方法的协议
val httpProtocol = http.baseUrl("https://www.baidu.com")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.doNotTrackHeader("1")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
//第二步:定义要操作的步骤1,打开百度首页
val homepage=repeat(5){ //重新五次
exec(http("home page").get("/").check(status.is(200))).pause(10 seconds)
}
//第三步:定义要操作的步骤2,在百度首页搜索AI 关键字
val searchcb=repeat(5){//重新五次
exec(http("search cnblogs").get("/s").queryParam("wd","AI").check(status.is(200))).pause(10 seconds)
}
//第四步:定义场景模式
val scn=scenario("search baidu home page").exec(homepage,searchcb)
//第五步:加载场景执行
setUp(scn.inject(atOnceUsers(5)).protocols(httpProtocol))
}
4、拷贝此package 文件夹到 E:\All\gatling\gatling-exe\user-files\simulations
5、在E:\All\gatling\gatling-exe\bin 下执行 gatling.bat 文件
E:\All\gatling\gatling-exe\bin>gatling.bat
GATLING_HOME is set to "E:\All\gatling\gatling-exe"
JAVA = ""D:\Program Files\Java\jdk1.8.0_131\\bin\java.exe""
Choose a simulation number:
[0] cnblogsCase.baidu
[1] computerdatabase.BasicSimulation
[2] computerdatabase.advanced.AdvancedSimulationStep01
[3] computerdatabase.advanced.AdvancedSimulationStep02
[4] computerdatabase.advanced.AdvancedSimulationStep03
[5] computerdatabase.advanced.AdvancedSimulationStep04
[6] computerdatabase.advanced.AdvancedSimulationStep05
6、在列表中选择 0 【[0] cnblogsCase.baidu,我们写好的脚本】
7、可以查看报告:如下:
Simulation cnblogsCase.baidu completed in 104 seconds
Parsing log file(s)...
Parsing log file(s) done
Generating reports...
Global Information
> request count 36 (OK=36 KO=0 )
> min response time 18 (OK=18 KO=- )
> max response time 1106 (OK=1106 KO=- )
> mean response time 518 (OK=518 KO=- )
> std deviation 288 (OK=288 KO=- )
> response time 50th percentile 573 (OK=573 KO=- )
> response time 75th percentile 671 (OK=671 KO=- )
> response time 95th percentile 991 (OK=991 KO=- )
> response time 99th percentile 1100 (OK=1100 KO=- )
> mean requests/sec 0.346 (OK=0.346 KO=- )
Response Time Distribution
> t < 800 ms 31 ( 86%)
> 800 ms < t < 1200 ms 5 ( 14%)
> t > 1200 ms 0 ( 0%)
> failed 0 ( 0%)
或
Gatling的变量的值至少有如下几种来源:
1、using Feeders——文件、数据库
2、extracting data from responses and saving them, e.g. with HTTP Check’s saveAs——从请求的返回值中提取和保存内容
3、manually with the Session API——用各种java提供的函数生成,缺点是可能影响一点性能
三、虚拟用户场景
例如:setUp( scn.inject( nothingFor(4 seconds)).protocols(httpConf))
1、nothingFor(duration):设置一段停止的时间;
不解释
2、atOnceUsers(nbUsers):立即注入一定数量的虚拟用户;
setUp(scn.inject(atOnceUsers(50)).protocols(httpConf))
3、rampUsers(nbUsers) over(duration):在指定时间内,设置一定数量逐步注入的虚拟用户;
setUp(scn.inject(rampUsers(50) over(30 seconds)).protocols(httpConf))
4、constantUsersPerSec(rate) during(duration):定义一个在每秒钟恒定的并发用户数,持续指定的时间;
setUp(scn.inject(constantUsersPerSec(30) during(15 seconds)).protocols(httpConf))
5、constantUsersPerSec(rate) during(duration) randomized:定义一个在每秒钟围绕指定并发数随机增减的并发,持续指定时间;
setUp(scn.inject(constantUsersPerSec(30) during(15 seconds) randomized).protocols(httpConf))
6、rampUsersPerSec(rate1) to (rate2) during(duration):定义一个并发数区间,运行指定时间,并发增长的周期是一个规律的值;
setUp(scn.inject(rampUsersPerSec(30) to (50) during(15 seconds)).protocols(httpConf))
7、rampUsersPerSec(rate1) to(rate2) during(duration) randomized:定义一个并发数区间,运行指定时间,并发增长的周期是一个随机的值;
setUp(scn.inject(rampUsersPerSec(30)to(50)during(15seconds) randomized).protocols(httpConf))
8、heavisideUsers(nbUsers) over(duration):定义一个持续的并发,围绕和海维赛德函数平滑逼近的增长量,持续指定时间(译者解释下海维赛德函数,H(x)当 x>0时返回1,x<0时返回0,x=0时返回0.5。实际操作时,并发数是一个成平滑抛物线形的曲线);
setUp(scn.inject(heavisideUsers(50) over(15 seconds)).protocols(httpConf))
9、splitUsers(nbUsers) into(injectionStep) separatedBy(duration):定义一个周期,执行injectionStep里面的注入,将nbUsers的请求平均分配;
setUp(scn.inject(splitUsers(50) into(rampUsers(10) over(10 seconds)) separatedBy(10 seconds)).protocols(httpConf))
10、splitUsers(nbUsers) into(injectionStep1) separatedBy(injectionStep2):使用injectionStep2的注入作为周期,分隔injectionStep1的注入, 直到用户数达到nbUsers;
setUp(scn.inject(splitUsers(100) into(rampUsers(10) over(10 seconds)) separatedBy atOnceUsers(30)).protocols(httpConf))
请求限制(原文为Throttling)
如果你希望设置每秒钟请求的数量,而不是每秒钟的并发数,那么应该考虑使用constantUsersPerSec(…)去设置用户的到达比例。在大多数的情况下,请求都是不需要使用请求限制的,或者至少是多余的。
但在某些情况下,我们需要使用Gatling自带的throttle方法,来对请求数做一个限制。
注意
- 我们仍然需要在场景等级中注入虚拟用户。请求限制只是用来给定的场景中设置一个确定的吞吐量,以及注入的用户数量和持续时间。这就是设置了一个瓶颈,也就 是一个上限。如果没有设置了足够的用户数,则不会达到这个限制。如果注入持续的时间没有达到请求限制的设定,那么所有虚拟用户执行完毕 后,simulation会被终止。如果注入的时间超过了请求限制的设定,那么请求限制就会起作用,阻止simulation的执行。
- 请求限制可以在每个场景中进行配置,移步这里找到更多:http://gatling.io/docs/2.1.7/general/scenario.html#scenario-throttling
setUp(scn.inject(constantUsersPerSec(100) during(30 minutes))).throttle(
reachRps(100) in (10 seconds),
holdFor(1 minute),
jumpToRps(50),
holdFor(2 hours)
)
解释下上述代码:
simulation会限制每秒钟100个请求,持续10秒钟,然后保持住这个吞吐量并持续1分钟;接着跳到每秒钟50个请求,再保持这个吞吐量2个小时;
这个代码块中设置的请求限制如下:
- reachRps(target) in (duration):设置一个恒定的吞吐量,并持续一段时间;
- jumpToRps(target):立即将限制切换为另一个吞吐量;
- holdFor(duration):将当前的吞吐量保持一定的时间;
边栏推荐
- leetcode:421. The maximum XOR value of two numbers in the array
- 昆明三环闭合工程将经过这些地方,有在你家附近的吗?
- wuzhicms代码审计
- Flask 轻量web框架
- The Block:USDD增长势头强劲
- Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
- 缓存穿透、缓存击穿、缓存雪崩分别是什么
- Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
- egg. JS learning notes
- Spark 中的 Rebalance 操作以及与Repartition操作的区别
猜你喜欢
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
CocosCreator事件派發使用
The test experience "tortured" by the PMP test is worth your review
La 18e Conférence internationale de l'IET sur le transport d'électricité en courant alternatif et en courant continu (acdc2022) s'est tenue avec succès en ligne.
Smart Logistics Park supply chain management system solution: digital intelligent supply chain enables a new supply chain model for the logistics transportation industry
To sort out messy header files, I use include what you use
ble HCI 流控机制
Learn more about the basic situation of 2022pmp examination
第十八届IET交直流输电国际会议(ACDC2022)于线上成功举办
7 RSA密码体制
随机推荐
Is it safe for CITIC Securities to open an account online? Is the account opening fee charged
Firewall basic transparent mode deployment and dual machine hot standby
Leetcode list summary
整理混乱的头文件,我用include what you use
To sort out messy header files, I use include what you use
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Summary of tx.origin security issues
CocosCreator事件派发使用
C# 更加优质的操作MongoDB数据库
Zebras are recognized as dogs, and the reason for AI's mistakes is found by Stanford
《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
Is it safe for Great Wall Securities to open an account? How to open a securities account
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
[glide] cache implementation - memory and disk cache
S2b2b solution for lighting industry: efficiently enable the industrial supply chain and improve the economic benefits of enterprises
How to implement a delay queue?