测试平台

使用 Gatling 进行负载测试

2021-08-13  本文已影响0人  ADADACHAN

使用 Gatling 进行负载测试

1.简述

Gatling 对自定义 Web 应用程序进行负载测试。在本文中,我们将使用 Gatling 压力工具来衡量本网站登台环境的性能。

2.测试场景

让我们首先设置我们的主要使用场景——一个接近可能正在浏览网站的典型用户的场景:

1)进入主页

2)从主页打开文章

3)转到指南/休息

4)转到 REST 类别

5)转到完整档案

6)从档案中打开一篇文章

3.记录场景

$GATLING_HOME/bin/recorder.sh 或者是Windows 环境直接bin/recorder.bat

注意:GATLING_HOME 是您的 Gatling 安装目录。

Gatling Recorder 有两种模式:HTTP 代理和 HAR 转换器。

我们在之前的教程中详细讨论了 HTTP 代理模式 - 现在让我们看看 HAR 转换器选项。

4.HAR 转换器

HAR 是 HTTP Archive 的缩写 - 这是一种基本上记录有关浏览会话的完整信息的格式。

我们可以从浏览器获取 HAR 文件,然后使用 Gatling Recorder 将其转换为模拟。

我们将在 Chrome 开发者工具的帮助下创建我们的 HAR 文件:

1)菜单 -> 更多工具 -> 开发者工具

2)转到网络选项卡

3)确保选中保留日志

4)完成网站导航后,右键单击要导出的请求

5)然后,选择全部复制为 HAR

6)将它们粘贴到文件中,然后从 Gatling 记录器导入

根据您的喜好调整 Gatling 记录器后,单击开始。请注意,输出文件夹默认为 GATLING_HOME/user-files-simulations

5.模拟

classRestSimulationextendsSimulation{

    val httpProtocol = http.baseURL("http://www.taobao.com")

    val scn = scenario("RestSimulation")

      .exec(http("home").get("/"))

      .pause(23)

      .exec(http("article_1").get("/spring-rest-api-metrics"))

      .pause(39)

      .exec(http("rest_series").get("/rest-with-spring-series"))

      .pause(60)

      .exec(http("rest_category").get("/category/rest/"))

      .pause(26)

      .exec(http("archive").get("/full_archive"))

      .pause(70)

      .exec(http("article_2").get("/spring-data-rest-intro"))

    setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)

}

6.运行负载测试

执行bin/gating.bat 或者是gating.sh

Gatling 工具将扫描 GATLING_HOME/user-files-simulations 并列出所有找到的模拟供我们选择。

运行模拟后,结果如下所示:

Simulation computerdatabase.advanced.AdvancedSimulationStep01 started...

================================================================================

2021-08-13 11:14:26                                          5s elapsed

---- Requests ------------------------------------------------------------------

> Global                                                  (OK=5      KO=0    )

> Home                                                    (OK=2      KO=0    )

> Home Redirect 1                                          (OK=1      KO=0    )

> Search                                                  (OK=1      KO=0    )

> Select                                                  (OK=1      KO=0    )

---- Scenario Name -------------------------------------------------------------

[--------------------------------------------------------------------------]  0%

          waiting: 0      / active: 1      / done: 0

================================================================================

================================================================================

2021-08-13 11:14:31                                          10s elapsed

---- Requests ------------------------------------------------------------------

> Global                                                  (OK=9      KO=0    )

> Home                                                    (OK=2      KO=0    )

> Home Redirect 1                                   (OK=2      KO=0    )

> Search                                                  (OK=1      KO=0    )

> Select                                                  (OK=1      KO=0    )

> Page 1                                                  (OK=1      KO=0    )

> Page 2                                                  (OK=1      KO=0    )

> Page 3                                                  (OK=1      KO=0    )

---- Scenario Name -------------------------------------------------------------

[--------------------------------------------------------------------------]  0%

          waiting: 0      / active: 1      / done: 0

================================================================================

================================================================================

2021-08-13 11:14:36                                          15s elapsed

---- Requests ------------------------------------------------------------------

> Global                                                  (OK=10    KO=0    )

> Home                                                    (OK=2      KO=0    )

> Home Redirect 1                                  (OK=2      KO=0    )

> Search                                                  (OK=1      KO=0    )

> Select                                                  (OK=1      KO=0    )

> Page 1                                                  (OK=1      KO=0    )

> Page 2                                                  (OK=1      KO=0    )

> Page 3                                                  (OK=1      KO=0    )

> Page 4                                                  (OK=1      KO=0    )

---- Scenario Name -------------------------------------------------------------

[--------------------------------------------------------------------------]  0%

          waiting: 0      / active: 1      / done: 0

================================================================================

================================================================================

2021-08-13 11:14:38                                          17s elapsed

---- Requests ------------------------------------------------------------------

> Global                                                  (OK=13    KO=0    )

> Home                                                    (OK=2      KO=0    )

> Home Redirect 1                                   (OK=2      KO=0    )

> Search                                                  (OK=1      KO=0    )

> Select                                                  (OK=1      KO=0    )

> Page 1                                                  (OK=1      KO=0    )

> Page 2                                                  (OK=1      KO=0    )

> Page 3                                                  (OK=1      KO=0    )

> Page 4                                                  (OK=1      KO=0    )

> Form                                                      (OK=1      KO=0    )

> Post                                                       (OK=1      KO=0    )

> Post Redirect 1                                      (OK=1      KO=0    )

---- Scenario Name -------------------------------------------------------------

[##########################################################################]100%

          waiting: 0      / active: 0      / done: 1

================================================================================

Simulation computerdatabase.advanced.AdvancedSimulationStep01 completed in 17 seconds

Parsing log file(s)...

Parsing log file(s) done

Generating reports...

================================================================================

---- Global Information --------------------------------------------------------

> request count                                        13 (OK=13    KO=0    )

> min response time                               299 (OK=299    KO=-    )

> max response time                              605 (OK=605    KO=-    )

> mean response time                           324 (OK=324    KO=-    )

> std deviation                                          81 (OK=81    KO=-    )

> response time 50th percentile                        300 (OK=300    KO=-    )

> response time 75th percentile                        301 (OK=301    KO=-    )

> response time 95th percentile                        424 (OK=424    KO=-    )

> response time 99th percentile                        569 (OK=569    KO=-    )

> mean requests/sec                                  0.722 (OK=0.722  KO=-    )

---- Response Time Distribution ------------------------------------------------

> t < 800 ms                                            13 (100%)

> 800 ms < t < 1200 ms                                  0 (  0%)

> t > 1200 ms                                            0 (  0%)

> failed                                                0 (  0%)

================================================================================

可以查看出的报告

7.结论

gatling 使用scala的语言进行编写,语法还是需要看的。

您的计算机上安装 Scala 3,通过 sbt 或通过 Coursier。

上一篇 下一篇

猜你喜欢

热点阅读