gatling系列教程(翻译)-第三节(快速开始)

在本章,我们将使用gatling来加载测试一个云托管web服务器,并向你介绍DSL基本元素。

安装gatling:将gatling解压至指定文件夹即可

提示:Windows用户:我们建议您不要将Gatling放在程序文件夹中,因为可能存在权限问题。

为了运行Gatling,您需要安装JDK。 Gatling需要JDK8,但我们建议您使用最新版本。

有关操作系统(OS)的安装和调整的所有详细信息,请参阅操作部分。

提示:

Gatling launch scripts and Gatling maven plugin honor JAVA_HOME env var if it’s set. OS, like OSX, have their own tricky way of discovering which version of Java to run, so you might end up running a different version than the one java -version tells you. If you get strange errors such as Unsupported major.minor version 51.0 and you were expecting to run a JDK8, you might want to explicitly set JAVA_HOME.

编码:

   Gatling的默认编码为UTF-8。 如果你想使用其他的,你必须:

   在使用记录器时选择正确的编码
   在gatling.conf文件中配置正确的编码。
   它将用于编译您的模拟,构建您的请求和您的响应。
   确保您的文本编辑器编码已正确配置为匹配。

关于scala:

Gatling模拟脚本是用Scala编写的,但不要惊慌! 您可以使用Gatling的所有基本功能,而不必了解Scala。 在大多数情况下,DSL将覆盖您的大部分需求,您将能够构建您的方案。

如果您有兴趣了解Scala的更多信息,那么我们建议您看看Twitter的Scala School

测试用例
    本页将引导您了解大部分Gatling HTTP功能。 您将了解模拟,情景,馈线,录音机,循环等。

测试应用
   在本教程中,我们将使用名为Computer-Database的应用程序部署在URL:http://computer-database.gatling.io。

此应用程序是用于管理计算机模型的简单CRUD应用程序,并且是2.3版之前的Play Framework示例。

脚本
    为了测试此应用程序的性能,我们将创建代表用户浏览时真正发生的情况。

这是我们认为真正的用户对应用程序的看法:
    用户到达应用程序。
    用户搜索“macbook”。
    用户打开相关型号之一。
    用户回到主页。
    用户遍历页面。
    用户创建一个新模型。

Basics
Using the Recorder
To ease the creation of the scenario, we will use the Recorder, a tool provided with Gatling that allows you to record your actions on a web application and export them as a Gatling scenario.

基本

使用录制器

为了简单的创建场景,我们使用录制器,Gatling提供的一个工具,可以在Web应用程序中记录您的操作,并将其作为Gatling场景导出。

This tool is launched with a script located in the bin directory:

  • On Linux/Unix:

    $GATLING_HOME/bin/recorder.sh
  • On Windows:

    %GATLING_HOME%\bin\recorder.bat

Once launched, the following GUI lets you configure how requests and responses will be recorded.

设置以下选项:
存储位置
BasicSimulation名称
关注重定向?检查
自动参考检查
黑名单首选过滤策略
黑色列表过滤器中的*。css,。* \。js和。* \ ico

121116_njqX_3613710.png

配置录制器后,您只需启动录制器并配置浏览器即可使用Gatling Recorder的代理。

有关录制器和浏览器配置的更多信息,请查看录制器参考页面。

录制场景

   输入“搜索”标签。
   访问网站:http://computer-database.gatling.io
   搜索名为“macbook”的模型。
   选择'Macbook pro'。
   输入“浏览”标签。
   回到主页。
   通过单击下一步按钮迭代模型页面几次。
   输入“编辑”标签。
   单击添加新计算机。
   填表格。
   单击创建此计算机。

尝试充当真正的用户,不要立即从一个页面跳到另一个页面,而不用花时间阅读。 这将使您的场景更贴近实际用户的行为。

完成播放场景后,单击记录器界面中的停止。

模拟将在您的Gatling安装的文件夹user-files / simulations / computerdatabase中以名称BasicSimulation.scala生成。

Gatling场景解释
这里是生产的产出:

package 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
}

What does it mean?

  1. The optional package.
  2. The required imports.
  3. The class declaration. Note that it extends Simulation.
  4. The common configuration to all HTTP requests.

val是用于定义常量值的关键字。 类型未定义,并由Scala编译器推断。

  1. The baseURL that will be prepended to all relative urls.
  2. Common HTTP headers that will be sent with all the requests.
  3. The scenario definition.
  4. A HTTP request, named request_1. This name will be displayed in the final reports.
  5. The url this request targets with the GET method.
  6. Some pause/think time.

持续时间单位默认为秒,例如 pause(5)相当于暂停(5秒)。

  1. Where one sets up the scenarios that will be launched in this Simulation.
  2. Declaring to inject into scenario named scn one single user.
  3. Attaching the HTTP configuration declared above.
  4. Note

    For more details regarding Simulation structure, please check out Simulation reference page.

    Running Gatling

    Launch the second script located in the bin directory:

  5. On Linux/Unix:

    $GATLING_HOME/bin/gatling.sh
  6. On Windows:

    %GATLING_HOME%\bin\gatling.bat
  7. You should see a menu with the simulation examples:

    Choose a simulation number:
       [0] computerdatabase.BasicSimulation

    When the simulation is done, the console will display a link to the HTML reports.

    Note

    If Gatling doesn’t work as expected, see our FAQ or ask on our Google Group.

    Going Further

    When you’re ready to go further, please check out the Advanced Tutorial.

转载于:https://my.oschina.net/u/3613710/blog/1554797

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值