当前位置:网站首页>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):将当前的吞吐量保持一定的时间;
边栏推荐
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- Is it safe to open an account online
- 离线、开源版的 Notion—— 笔记软件Anytype 综合评测
- What are cache penetration, cache breakdown, and cache avalanche
- Developers, MySQL column finish, help you easily from installation to entry
- VB cannot access database stocks
- C# 更加优质的操作MongoDB数据库
- [glide] cache implementation - memory and disk cache
- The Block:USDD增长势头强劲
- Difference between redis' memory obsolescence strategy and expiration deletion strategy
猜你喜欢
上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Which domestic cloud management platform manufacturer is good in 2022? Why?
Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?
Learn more about the basic situation of 2022pmp examination
DataKit——真正的统一可观测性 Agent
Difference between redis' memory obsolescence strategy and expiration deletion strategy
Hidden corners of coder Edition: five things that developers hate most
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
ble HCI 流控机制
随机推荐
DataKit——真正的统一可观测性 Agent
ble HCI 流控机制
C# 服务器日志模块
Solution du système de gestion de la chaîne d'approvisionnement du parc logistique intelligent
Load test practice of pingcode performance test
go-micro教程 — 第二章 go-micro v3 使用Gin、Etcd
MVC模式和三层架构
什么是低代码开发?
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
kaili不能输入中文怎么办???
Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
leetcode:421. The maximum XOR value of two numbers in the array
Datakit -- the real unified observability agent
【HCIA持续更新】WLAN概述与基本概念
Electronic pet dog - what is the internal structure?
Summary of tx.origin security issues
手里10万元存款买什么理财产品收益最高?
被PMP考试“折磨”出来的考试心得,值得你一览
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
新的职业已经出现,怎么能够停滞不前 ,人社部公布建筑新职业