[TOC]
一.gatling 概述
能生成丰富多彩的报告,包含测试案例中收集的所有指标。该功能似乎比 JMeter 更好
- 比对
- lr,太臃肿,99%的功能用不上。
- ab,安装apache的附属品,windows安装失败。
locust,基于python,号称能发起百万级并发。但是,对比测试结果的时候,跟jmeter和gatling差一个数量级。
下载 : https://gatling.io/download/
1
2bin/目录存放gatling的可执行文件,conf/存放配置,通常保持默认即可,lib/存放gatling本身的依赖,用户不用管,results/存放报告,user-files/是用户最主要使用的目录,用户定义的测试场景相关的代码均存放于此目录下。
zip包解压缩以后已经带有了一个官方的示例文件BasicSimulation.scala,想看看演示效果的直接使用bin/gatling.(bat|sh)启动就可以了。这个演示的场景描述见官方文档。那几个AdvancedSimulationStep其实效果上和BasicSimulation完全一致,只是官方提供了一些参考的DSL写法而已。运行:
sh gatling.sh
Gatling教程系列,见: https://segmentfault.com/a/1190000005057103idea 集成
- 运行:
mvn gatling:execute
- maven-plugin: https://gatling.io/docs/2.3/extensions/maven_plugin/
见:http://www.51testing.com/html/56/n-3723956.html
- 运行:
二.配置(Simulation)
- user-files/simulations/computerdatabase/BasicSimulation.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package computerdatabase // 1
import io.gatling.core.Predef._ // 2
import io.gatling.http.Predef._
import scala.concurrent.duration._
class BasicSimulation extends Simulation { // 3
val httpConf = http // 4
.baseURL("http://computer-database.gatling.io") // 5
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // 6
.doNotTrackHeader("1")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")
val scn = scenario("BasicSimulation") // 7
.exec(http("request_1") // 8
.get("/")) // 9
.pause(5) // 10
setUp( // 11
scn.inject(atOnceUsers(1)) // 12
).protocols(httpConf) // 13
}
1 | 1.可选包. |
参考
https://gatling.io/docs/current/quickstart/#gatling-scenario-explained
https://testerhome.com/topics/3633
三.压测场景
1. 几种压测场景示例:
1 | //600秒跑1000个用户 |
2. 并发场景:
如果需要同时压多台机器,可以使用方法:
1 | .baseURLs("http://10.0.0.1",“http://10.0.0.2") |
3. 其他场景介绍:
1 | setUp( |
1 | 1、`nothingFor(4 seconds)` 在指定的时间段(4 seconds)内什么都不干 |
gatling详细使用 - CSDN博客
https://blog.csdn.net/qq_37023538/article/details/54950827
四.maven集成
- 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47<properties>
<gatling-charts-highcharts.version>2.3.1</gatling-charts-highcharts.version>
<gatling-plugin.version>2.2.4</gatling-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling-charts-highcharts.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- gatling压测 -->
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling-plugin.version}</version>
<configuration>
<!-- 测试脚本 -->
<simulationClass>gatling.GatewaySimulation</simulationClass><!-- src/Test/gatling/GatewaySimulation -->
<!-- 结果输出地址 -->
<resultsFolder>target/gatling/results</resultsFolder>
<!-- 其它 -->
<!--<configFolder>src/test/resources</configFolder>-->
<!--<dataFolder>src/test/resources/data</dataFolder>-->
<!--<bodiesFolder>src/test/resources/bodies</bodiesFolder>-->
</configuration>
<executions>
<execution>
<id>execution-1</id>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<simulationClass>scala/f**s***GatewaySimulation</simulationClass>
</configuration>
</execution>
<!-- Here, can repeat the above execution segment to do another test -->
</executions>
</plugin>
</plugins>
</build>
多测试脚本
1 | <configuration> |
- 仅配置runMultipleSimulations可跑全部模拟
1
2
3<configuration>
<runMultipleSimulations>true</runMultipleSimulations>
</configuration>
Maven生命周期控制
需要在构建中多次运行插件(例如,为了按顺序运行多个模拟).一个解决方案是配置几个execution块,每块有不同的configuration块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24<executions>
<execution>
<id>execution-1</id>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<simulationClass>gatling.GatewayServletSimulation</simulationClass>
<resultsFolder>target/gatling/results</resultsFolder>
</configuration>
</execution>
<execution>
<id>execution-2</id>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<simulationClass>gatling.GatewayFluxSimulation</simulationClass>
<resultsFolder>target/gatling/results</resultsFolder>
</configuration>
</execution>
<!-- Here, can repeat the above execution segment to do another test -->
</executions>运行
1
2
3
4
5mvn gatling:execute
- 测试
mvn gatling:test // bound to test phase
mvn gatling:integration-test // bound to integration-test phase
GETTING STARTED WITH SCALA IN INTELLIJ
https://docs.scala-lang.org/getting-started-intellij-track/getting-started-with-scala-in-intellij.html
五.测试mvc与flux差异
servlet
webFlux
六.可能会遇到的问题
object gatling is not a member of package io
1 | 10:57:26.167 [main][ERROR][ZincCompiler.scala:140] i.g.c.ZincCompiler$ - /Users/liuxiang/Desktop/work/f**s***-zuul/f**s***-gateway/service/src/test/scala/f**s***GatewaySimulation.scala:18: object gatling is not a member of package io |
- 尝试一: 导入scala-SDK. (结果:未解决,
io.gatling.core.Predef._
非scala
内容,而是gatling
内容) - 尝试二: 检查gatling依赖.结果:依赖完整,但无法在compile时不能识别.
Pruning sources from previous analysis, due to incompatible
1 | 03:02:39.500 [main][WARN ][ZincCompiler.scala:141] i.g.c.ZincCompiler$ - Pruning sources from previous analysis, due to incompatible CompileSetup. |
- 诊断: can not be found in the classpath or does not extends Simulation. 未继承Simulation,看起来像没有找到这个类文件.检查target
- 原因: 检查target发现未生成class,删除原target,重新builder即可恢复. 后续变更,如不能实时build,可暂时手动处理(
或Command+Shift+F9
)
相关参考
Gatling Load and Performance testing - Open-source load and performance testing
https://gatling.io/docs/current/quickstart/
翻译: https://testerhome.com/topics/3633
性能测试工具——gatling - 乌鸦不会飞
http://www.bigerhead.com/2016/11/330.html
使用Gatling做web压力测试 - DTeam的团队日志 - SegmentFault 思否
https://segmentfault.com/a/1190000008254640
性能测试之 Gatling - 推酷
https://www.tuicool.com/articles/fiemeyN
jenkins:应用篇(Gatling plugin的使用) - shihuc - 博客园
https://www.cnblogs.com/shihuc/p/5149035.html
https://www.bbsmax.com/A/QW5YXDnYJm/
性能测试Gatling入门教程 | EZLippi-浮生志
https://www.ezlippi.com/blog/2018/01/gatling.html
plugins.jetbrains scala 下载
https://plugins.jetbrains.com/plugin/1347-scala