Intro to Gatling – 加特林介绍

最后修改: 2016年 6月 8日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Gatling is a load testing tool that comes with excellent support of the HTTP protocol – which makes it a really good choice for load testing any HTTP server.

Gatling是一个负载测试工具,它对HTTP协议有很好的支持 – 这使得它成为任何HTTP服务器负载测试的真正好选择。

This quick guide will show you how to setup a simple scenario for load testing an HTTP server.

本快速指南将向您展示如何设置一个简单的场景,对HTTP服务器进行负载测试。

Gatling simulation scripts are written in Scala, but don’t worry – the tool comes to help us with a GUI allowing us to record the scenario. Once we have finished recording the scenario the GUI create the Scala script representing the simulation.

Gatling的仿真脚本是用Scala写的,但不用担心–该工具会通过GUI来帮助我们,让我们记录场景。一旦我们完成了场景的记录,GUI就会创建代表仿真的Scala脚本。

After running the simulation we have a ready-to-present HTML reports.

运行模拟后,我们就有了一个随时可以呈现的HTML报告

Last but not least, Gatling’s architecture is asynchronous. This kind of architecture lets us implement virtual users as messages instead of dedicated threads, making them very resource cheap. Thus, running thousands of concurrent virtual users is not an issue.

最后但同样重要的是,Gatling的架构是异步的。这种架构让我们可以将虚拟用户作为消息来实现,而不是专门的线程,使它们的资源非常便宜。因此,运行成千上万个并发的虚拟用户不是问题。

It’s also worth noting though that the core engine is actually protocol agnostic, so it’s perfectly possible to implement support for other protocols. For example, Gatling currently also ships JMS support.

值得注意的是,核心引擎实际上是协议无关的,所以完全可以实现对其他协议的支持。例如,Gatling目前也支持JMS

2. Creating a Project Using the Archetype

2.使用原型创建一个项目

Although we can get Gatling bundles as a .zip we choose to use Gatling’s Maven Archetype. This allows us to integrate Gatling and run it into an IDE and make it easy to maintain the project in a version control system. Be careful as Gatling require a JDK8.

A虽然我们可以以.zip的形式获得Gatling捆绑包,但我们选择使用Gatling的Maven Archetype。这使我们能够整合Gatling并将其运行到IDE中,并使我们能够在版本控制系统中轻松维护该项目。请注意,Gatling需要JDK8

From the command line, type:

在命令行中,输入。

mvn archetype:generate

Then, when prompted:

然后,当被提示时。

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains):

Type:

类型。

gatling

You should then see:

然后你应该看到。

Choose archetype:
1: remote -> 
  io.gatling.highcharts:gatling-highcharts-maven-archetype (gatling-highcharts-maven-archetype)

Type:

类型:

1

to select the archetype, then select the version to use (choose the latest version).

来选择原型,然后选择要使用的版本(选择最新版本)。

Select the groupId, artifactId, version and package name for the classes before confirming the archetype creation.

在确认创建原型之前,为类选择 groupIdartifactIdversionpackage名称。

Finish by importing the archetype into an IDE – for example into the Scala IDE (based on Eclipse) or into IntelliJ IDEA.

最后,将原型导入IDE–例如导入Scala IDE(基于Eclipse)或导入IntelliJ IDEA

3. Define a Scenario

3.定义一个场景

Before launching the recorder, we need to define a scenario. It will be a representation of what really happens when users navigate a web application.

在启动记录器之前,我们需要定义一个场景。它将代表用户浏览网络应用时真正发生的情况。

In this tutorial, we will use the application provided by the Gatling’s team for sample purpose and hosted at the URL http://computer-database.gatling.io.

在本教程中,我们将使用Gatling团队提供的应用程序作为样本,并托管在URL http://computer-database.gatling.io

Our simple scenario could be:

我们的简单方案可以是。

  • A user arrives at the application.
  • The user searches for ‘amstrad’.
  • The user opens one of the related models.
  • The user goes back to home page.
  • The user iterates through pages.

4. Configuring the Recorder

4.配置录音机

First of all launch the Recorder class from the IDE. Once launched, the GUI lets you configure how requests and responses will be recorded. Choose the following options:

首先,从IDE中启动Recorder类。一旦启动,GUI让你配置如何记录请求和响应。选择以下选项。

  • 8000 as listening port
  • org.baeldung.simulation package
  • RecordedSimulation class name
  • Follow Redirects? checked
  • Automatic Referers? checked
  • Black list first filter strategy selected
  • .*\.css, .*\.js and .*\.ico in the black list filters

setting

 

Now we have to configure our browser to use the defined port (8000) chosen during the configuration. This is the port our browser must connect to so that the Recorder is able to capture our navigation.

现在我们必须配置我们的浏览器,使其使用配置时选择的定义端口(8000)。这是我们的浏览器必须连接的端口,以便Recorder能够捕获我们的导航。

Here is how to do with Firefox, open the browser Advanced settings, then go to the Network panel and update the connection settings:

以下是使用火狐浏览器的方法,打开浏览器高级设置,然后进入网络面板,更新连接设置。

 

proxy settings

5. Recording the Scenario

5.记录场景

Now that everything is configured we can record the scenario that we have defined above. The step are the following:

现在一切都配置好了,我们可以记录我们上面定义的场景。其步骤如下。

  1. Initiate the recording by clicking the ‘Start’ button
  2. Go to the website: http://computer-database.gatling.io
  3. Search for models with ‘amstrad’ in their name
  4. Select ‘Amstrad CPC 6128′
  5. Go back to home page
  6. Iterates several times through the model pages by clicking on Next button
  7. Click on ‘Stop & save’ button

The Simulation will be generated in the package org.baeldung defined during the configuration under the name RecordedSimulation.scala

仿真将在配置时定义的包org.baeldung中生成,名称为RecordedSimulation.scala

6. Run a Simulation With Maven

6.用Maven运行模拟程序

To run our recorded simulation we need to update our pom.xml:

为了运行我们记录的模拟,我们需要更新我们的pom.xml

<plugin>
    <groupId>io.gatling</groupId>
    <artifactId>gatling-maven-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
        <execution>
            <phase>test</phase>
            <goals><goal>execute</goal></goals>
            <configuration> 
                <disableCompiler>true</disableCompiler> 
            </configuration>
        </execution>
    </executions>
</plugin>

This let us execute the simulation at test phase. To start the test just run:

这让我们在测试阶段执行模拟。要开始测试,只需运行。

mvn test

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

当模拟完成后,控制台将显示HTML报告的路径。

Note: configuration <disableCompiler>true</disableCompiler> is used because we will be using Scala with maven this flag will ensure that we do not end up compiling our simulation twice. More details are available on Gatling docs.

注意:配置<disableCompiler>true</disableCompiler>是因为我们将使用Scala与maven,这个标志将确保我们的模拟不会最终编译两次。更多细节可在Gatling docs上找到。

7. Reviewing the Result

7.审查结果

If we open the index.html at the suggested location the reports look like as follow:

如果我们在建议的位置打开index.html,报告看起来如下。

reports

8. Conclusion

8.结论

In this tutorial we have explored load testing an HTTP server with Gatling. The tools allows us to record a simulation based on a defined scenario with the help of a GUI interface. After the recording is done we can launch our test. The test report will be in a form of HTML resume.

在本教程中,我们已经探索了用Gatling对HTTP服务器进行负载测试。该工具允许我们在GUI界面的帮助下,根据定义的场景录制一个模拟。记录完成后,我们可以启动我们的测试。测试报告将以HTML简历的形式出现。

To build up our example we have chosen to use a maven archetype. This help us to integrate Gatling and run it into an IDE and make it easy to maintain the project in a version control system.

为了建立我们的例子,我们选择了使用一个maven原型。这有助于我们将Gatling集成到IDE中运行,并在版本控制系统中轻松维护该项目

The example code can be found in the GitHub project.

示例代码可以在GitHub项目中找到。