How to use Gatling load testing tool

What is load testing

  • load and stress testing. Load testing verifies how the system function under a heavy number of concurrent clients sending requests over a certain period of time. However, the main goal of that type of tests is to simulate the standard traffic similar to that, which may arise on production. Stress testing takes load testing and pushes your app to the limits to see how it handles an extremely heavy load.

  • 使用一些 load testing tool 往往比自己写一个 console application 来 test 要有效,不单单是因为它提供很多可视化的图标可以参考在正常情况下用户的访问情况,还因为它对用户访问处理的算法也更好,可以模拟同时访问的情况而不同我们自己写平行运算。

Gatling

  • 这次我选的是Gatling. 一是因为它是免费的,二是因为它提供很多可视化图表

Gatling directory

  • 文件解压后可以看到这些文件夹,其中 bin 包含程序本身的运行文件。conf 是程序的配置文件 config,results 会有每次测试的报告,一开始应该是空的。user_file 是用户的测试配置文件,里面有两个文件夹,一个是 resources,所有用到的关联文件类似 csv file 都应该放在里面。还有就是 simulations,这个就是每次测试的配置文件,使用 scala 写的。但是也很容易懂。

simulation scala

  • scala 文件就长这个样子,需要注意的就是 baseUrl,feeder file students.csv,get 后面的 url 剩余部分,还有就是 setup 里面的同时访问数量。现在我们没有用到任何 csv file,但是如果有用到就把他放在 resources 文件夹里

  • 然后进入 bin 并运行 gatling.bat

select simulation

  • 选择一个 simulation

console output

  • 我们可以看到 100 个 request 都完成了

Gatling report

  • 图表也非常丰富
  • 接下来我们看看如果模拟 1000 个不同的用户在接下来的 20 秒,因为不需要同时接受 1000 个用户,所以我们更改 setup,使用 rampUser
1
setUp(scn.inject(rampUsers(1000) over (20 seconds)).protocols(httpProtocol))
  • 运行报错,但是用 console 看起来太不方便了。我们更改下 config 让他把 error log 导出到一个文件中

export error log

  • 找到 logback.xml,然后更改成这样,这样每次报错就会生成 gatling.log

  • 打开 log 发现他说:value over is not a member of io.gatling.core.Predef.RampBuilder。 说明没有识别关键词 over,google 一下这句话。发现是因为这个是旧版本的语法了。。。可是官方 guide 居然还没有更新。。。

  • 好吧找到 migration guide,发现 over 被替换成 during 了。。嗯。。真好

migration guide

  • 重新运行一下 gatling.bat,现在行了,还挺像模像样的

gatling console output

  • 现在大部分 request 都小于 800ms

gatling report

  • 细看的话,同时在线人数基本在 50 以上,我们用不到那么多。。。并且将近一半的 request 其实只用了 36ms。。

gatling report 2

  • ok,还有一个问题就是这个 report 中 response time 的上下限太高了,我们平均都不会超过 100 所以要更改一下上下限
  • 同样,还是去 gatling.conf

gatling config

  • 改一下 lowerBound, higherBound, 记住一定要把#去掉,不然这会被算作 comment 而没有实际作用

gatling config guide

gatling report with updated Time Bound

  • 可以看到现在图表中的 response time 变成 100ms 何 200ms 了