Back to Performance Test

See Also ScalaApache JMeterApacheBenchwrk

Gatling

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

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/

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 定义虚拟用户的操作

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


CategoryTool

MainWiki: Gatling (last edited 2017-09-12 00:14:14 by twotwo)