Back to Performance Test |
See Also Scala、Apache JMeter、ApacheBench、wrk |
Gatling
Contents
1. Quickstart
http://gatling.io/docs/current/quickstart/
1.1. Installing
➜ gatling wget https://repo1.maven.org/maven2/io/gatling/highcharts/gatling-charts-highcharts-bundle/2.3.0/gatling-charts-highcharts-bundle-2.3.0-bundle.zip
➜ gatling-charts-highcharts-bundle-2.3.0 bin/gatling.sh -h
➜ gatling-charts-highcharts-bundle-2.3.0 tree . -d
├── bin //执行脚本
│ ├── gatling.sh //运行测试
│ └── recorder.sh //启动录制脚本的UI的(不推荐使用)
├── conf //应用自身的配置
│ ├── gatling-akka.conf //
│ ├── gatling.conf //
│ ├── logback.xml //
│ └── recorder.conf //
├── lib //应用自身依赖的库文件
│ └── zinc
├── results //存放测试报告
├── target
│ └── test-classes
└── user-files //存放测试脚本
├── bodies
├── data
└── simulations
└── computerdatabase
├── BasicSimulation.scala //
└── advanced
13 directories
## Gatling执行脚本
➜ gatling-charts-highcharts-bundle-2.3.0 st user-files/simulations/computerdatabase/BasicSimulation.scala
1.2. Test Case
http://gatling.io/docs/current/quickstart/#test-case
Test Case中的关键概念,详见#Concepts
simulations
scenarios A scenario is a chain of requests and pauses
feeders
recorder
loops
1.2.1. Gatling Scenario Explained
http://gatling.io/docs/current/quickstart/#gatling-scenario-explained
首先定义一个URL: val httpConf
然后定义Case中的场景: val scn = scenario("Scenario Name")
最后启动模拟器: setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
1.3. Running Gatling
http://gatling.io/docs/current/quickstart/#running-gatling
1.3.1. Execute Case
Launch $GATLING_HOME/bin/gatling.sh
➜ gatling-charts-highcharts-bundle-2.3.0 bin/gatling.sh
GATLING_HOME is set to /Users/liyan/app/test/gatling/gatling-charts-highcharts-bundle-2.3.0
18:43:51.565 [WARN ] i.g.c.ZincCompiler$ - Pruning sources from previous analysis, due to incompatible CompileSetup.
Choose a simulation number:
[0] computerdatabase.BasicSimulation
[1] computerdatabase.advanced.AdvancedSimulationStep01
[2] computerdatabase.advanced.AdvancedSimulationStep02
[3] computerdatabase.advanced.AdvancedSimulationStep03
[4] computerdatabase.advanced.AdvancedSimulationStep04
[5] computerdatabase.advanced.AdvancedSimulationStep05
0
Select simulation id (default is 'basicsimulation'). Accepted characters are a-z, A-Z, 0-9, - and _
Select run description (optional)
Simulation computerdatabase.BasicSimulation started...
Reports generated in 0s.
Please open the following file: results/basicsimulation-1505126722282/index.html
When the simulation is done, the console will display a link to the HTML reports.
1.3.2. Scala Crashed Issue
https://stackoverflow.com/questions/40628310/scala-build-crashed
Scala 2.12 require's newer version of JDK then 1.8.0_111
1.3.3. Config connection timed out
export JAVA_OPTS="-Dgatling.http.connectionTimeout=5000"
1.3.4. Failed to Open Socket Issue
gatling j.n.ConnectException: Failed to open a socket.
Kernel_Parameters#limits.conf update limits.conf on Linux
Kernel_Parameters#Change_ulimit_settings action on MacOS
测试机系统调优参考#Operations
1.3.5. Cannot assign requested address Issue
http://blog.bruceding.com/342.html
sudo sysctl -w net.ipv4.ip_local_port_range="1025 65535"
sudo sysctl -w net.ipv4.tcp_tw_recycle=1
sudo sysctl -w net.ipv4.tcp_tw_reuse=1
sudo sysctl -w net.ipv4.tcp_timestamps=1
sudo sysctl -w net.ipv4.tcp_max_tw_buckets=50000
$ ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}'
ESTAB 44853
State 1
FIN-WAIT-1 1
FIN-WAIT-2 2
TIME-WAIT 1338
LISTEN 5
2. Advanced Tutorial
http://gatling.io/docs/current/advanced_tutorial/
2.1. Isolate processes
可以把Case定义为object, 在exec的时候传入多个
val scn = scenario("Scenario Name").exec(Search.search, Browse.browse, Edit.edit)
2.2. Configure virtual users
setUp(users.inject(atOnceUsers(10)).protocols(httpConf))
setUp(
users.inject(rampUsers(10) over (10 seconds)),
admins.inject(rampUsers(2) over (10 seconds))
).protocols(httpConf)
2.3. Loop statements
http://gatling.io/docs/current/general/scenario/#scenario-loops
2.4. Check and failure management
http://gatling.io/docs/current/http/http_check/#http-check
object HuaWei {
val auth = exec(http("hw.auth")
.post("/LoginAuth/") //post uri
.header("Content-Type", "application/json") //设置body数据格式
//将json参数用StringBody包起,并作为参数传递给function body()
.body(StringBody("""{
"appid": "500006",
"channelData": {
"appId": "1088896",
"gameAuthSign": "EWn+oMLaUYNvmiZmDC5XtlO9H",
"playerId": "600005",
"ts": "1490250993465"
},
"channelId": "HW"
}"""))
.asJSON
.check(
//token过期,code=440, msg="session timeout"
jsonPath("$.code").ofType[Int].is(440).saveAs("code"),
jsonPath("$.msg").ofType[String].is("session timeout")
// jsonPath("$.code").ofType[Int].is(0)
)
)
}
3. General
http://gatling.io/docs/current/general/
3.1. Concepts
http://gatling.io/docs/current/general/concepts/
- Virtual User Gatling 用消息来实现,比用 Thread 效率上要高很多
- Scenario 某个用户的具体行为
- Simulation 一次压力测试,可以同时包含多个用户的不同行为
- Session
- Feeders
- Checks
- Assertions
- Reports
3.2. Operations
http://gatling.io/docs/current/general/operations/ OS Tuning: Open Files Limit & Kernel and Network Tuning
包含一些测试机需要调整的系统参数,例如ulimit -n 10240
3.3. Configuration
http://gatling.io/docs/current/general/configuration/ 配置文件、命令行参数等
3.4. Simulation setup
http://gatling.io/docs/current/general/simulation_setup/
https://testerhome.com/topics/4094 中文翻译
Injection 定义虚拟用户的操作
atOnceUsers(nbUsers) 立即注入一定数量的虚拟用户
rampUsers(nbUsers) over(duration) 将一定数量的虚拟用户,在指定时间内逐步注入进来
constantUsersPerSec(rate) during(duration) 定义一个在每秒钟恒定的并发用户数,持续指定的时间;
constantUsersPerSec(rate) during(duration) randomized 定义一个在每秒钟围绕指定并发数随机增减的并发,持续指定时间
rampUsersPerSec(rate1) to (rate2) during(duration) 定义一个并发数区间,运行指定时间,并发增长的周期是一个规律的值
rampUsersPerSec(rate1) to(rate2) during(duration) randomized 定义一个并发数区间,运行指定时间,并发增长的周期是一个随机的值;
splitUsers(nbUsers) into(injectionStep) separatedBy(duration) 定义一个周期,执行injectionStep里面的注入,将nbUsers的请求平均分配
splitUsers(nbUsers) into(injectionStep1) separatedBy(injectionStep2) 使用injectionStep2的注入作为周期,分隔injectionStep1的注入,直到用户数达到nbUsers
heavisideUsers(nbUsers) over(duration) 定义一个持续的并发,围绕和海维赛德函数平滑逼近的增长量,持续指定时间(译者解释海维赛德函数` H(x)当x>0时返回1,x< * 0时返回0,x=0时返回0.5。实际操作时,并发数是一个成平滑抛物线形的曲线)
3.5. Reports
http://gatling.io/docs/current/general/reports/ 怎么看测试报告
3.5.1. Use log file to generate report
Generates the reports for the simulation log file located in
<gatling_home>/results/<folderName>
➜ gatling-charts-highcharts-bundle-2.3.0 ll results/test total 648 -rw-r--r-- 1 liyan staff 322K Sep 15 18:29 simulation.log ➜ gatling-charts-highcharts-bundle-2.3.0 bin/gatling.sh -ro test ... Generating reports... ... Reports generated in 0s. Please open the following file: /opt/gatling/gatling-charts-highcharts-bundle-2.3.0/results/test/index.html
4. Reference
https://segmentfault.com/a/1190000008254640 使用Gatling做web压力测试