My Profile Photo

庄津津的技术博客


众里寻他千百度,蓦然回首,那人却在灯火阑珊处


Gatling初体验


一日在逛InfoQ的时候,瞥到一眼有国内某技术团队用gatling做大规模的负载测试,由于之前只接触过JMeter,甚至都没听说过Gatling,出于对未知事物的好奇,便去搜索相关的资料。这里有一篇是关于JMeter和Gatling的性能比较,当然这个不是本篇小记的重点啦。

准备工作

以下是笔者的测试环境

  • Jdk1.8
  • Gradle 4.10.3
  • Gatling 3.0
  • Scala 2.13.0-M5

实践

Galting扩展

笔者的想法是把Gatling集成到自己的Gradle项目中去。于是去官网查看相关文档,有一个extensions的章节,里面有官方扩展和第三方扩展的介绍,第三方扩展里面有两个Gradle Plugin,笔者采用第一个,没别的原因,就因为它的star数和issue数比较多,可以点此查看具体的用法。

Gradle配置

只需要在build.gradle中添加如下配置

1
2
3
plugins {
    id "com.github.lkishalmi.gatling" version "3.0.1"
}

测试用例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import org.ukuz.goku.loadtest.scenario.{SetKeyScenario}

/*
 * 模拟类
 */
class CompositeSimulation extends Simulation {
  	var httpProtocol = http.baseUrl("https://foo.com") //这里填要测试的域名
    setUp(
        SetKeyScenario.build.inject(
            //rampUsersPerSec(5).to(50).during(300) //从每秒模拟5个用户到最终每秒50个用户线性过渡,这中间持续300秒
            constantConcurrentUsers(60).during(300) //每秒固定60个用户,持续300秒
        )
   ).protocols(httpProtocol)
}

当然这里面的用户数是要慢慢往上调,要结合底下的报告指标,看看是否已经达到瓶颈,并且对系统进行调优。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import io.gatling.http.Predef.http
import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder

/*
 * 脚本类
 */
object SetKeyScenario {
    def build: ScenarioBuilder = {
        return scenario("Set Key").feed(csv("kv.csv").random) //csv文件中随机取用户数据
                .exec(http("setKv")
                .put("/api/kv/set")
                .header("Cookie", "access_token=${access_token}")
                .queryParam("key", "${key}")
                .queryParam("val", "${val}"))
    }
}

这是比较简单的测试用例。我们接下来就可以来测试了,若是用idea开发,可以双击下图gatlingRun这个任务,便可以负载测试了

Gatling启动任务

在控制台下会打印如下命令

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
Simulation org.ukuz.goku.loadtest.CompositeSimulation started...

================================================================================
2019-xx-xx xx:xx:xx                                           5s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=0      KO=0     )


---- Set Key -------------------------------------------------------------------
          active: 591    / done: 0     
================================================================================

...

Simulation org.ukuz.goku.loadtest.CompositeSimulation completed in xxxx seconds
Parsing log file(s)...
Parsing log file(s) done
Generating reports...

================================================================================
---- Global Information --------------------------------------------------------
...
---- Response Time Distribution ------------------------------------------------
...
---- Errors --------------------------------------------------------------------
> j.g.h.c.i.RequestTimeoutException: Request timeout to foo.com/xx.xx.xx.xx after 60000ms 280 (58.700%)
> j.n.s.SSLException: handshake timed out                           183 (38.365%)
> i.n.c.ConnectTimeoutException: connection timed out: foo.com      14 ( 2.935%)
================================================================================

Reports generated in 0s.
Please open the following

测试完成后可以在build/reports/gatling下看到以日期为文件夹的报告信息

Gatling报告1

到这里算是成功运行了。。。