1. Overview
1.概述
In this tutorial, we’ll integrate a load test within a Jenkins pipeline using Gatling. First of all, let’s make sure we’re familiar with the concepts of Gatling.
在本教程中,我们将使用Gatling在Jenkins管道中集成一个负载测试。首先,让我们确保熟悉Gatling.的概念。
2. Create a Gatling Project With Maven
2.用Maven创建一个Gatling项目
Our goal is to insert a load test with Gatling into a Jenkins CI/CD pipeline. In order to automate this verification step, we can pack the tool using Maven.
我们的目标是将Gatling的负载测试插入Jenkins的CI/CD流水线中。为了实现这一验证步骤的自动化,我们可以使用Maven打包该工具。
2.1. Dependencies
2.1. 依赖性
Gatling provides a plugin – the Gatling Maven Plugin – which allows us to launch load tests with Gatling during the Maven build phase of the project. In this way, it’s possible to integrate the load test into any Continuous Integration tool.
Gatling提供了一个插件–Gatling Maven Plugin,它允许我们在项目的Maven构建阶段用Gatling启动负载测试。通过这种方式,可以将负载测试集成到任何持续集成工具中。
So, let’s integrate Gatling into a sample Maven project. First, we need the following dependencies in our pom.xml file:
那么,让我们把Gatling集成到一个Maven项目样本中。首先,我们需要在pom.xml文件中加入以下依赖项:
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
<version>3.3.1</version>
</dependency>
In addition to the previous dependencies, we need to specify the gatling-maven-plugin in the plugin section of the pom.xml:
除了前面的依赖,我们还需要在pom.xml的插件部分指定gatling-maven-plugin:
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<simulationClass>org.baeldung.${SimulationClass}</simulationClass>
</configuration>
</plugin>
The SimulationClass value represents the simulation class used to perform the load test. The Gatling version and Gatling Maven Plugin version don’t have to be the same. Here, we can find the latest version of Gatling, while at the following link we can find the latest version of the Gatling Maven Plugin.
SimulationClass值代表用于执行负载测试的模拟类。Gatling版本和Gatling Maven插件版本不一定相同。在这里,我们可以找到Gatling的最新版本,而在以下链接中,我们可以找到Gatling Maven Plugin的最新版本。
2.2. Creating a Scenario
2.2.创建一个场景
A simulation consists of a scenario, which can contain the execution of several requests.
模拟由一个场景组成,它可以包含几个请求的执行。
Simulations are written in Scala, using Gatling’s DSL, which is simple and intuitive.
模拟是用Scala编写的,使用Gatling的DSL,它简单而直观。
2.3. Running the Scenario
2.3.运行该方案
Once we’ve written the code necessary to execute the load test, we can build the project and run the simulation:
一旦我们编写了执行负载测试所需的代码,我们可以构建项目并运行模拟:。
mvn clean package
mvn gatling:test
Inside the generated target folder, we can find the report of the load test performed by Gatling.
在生成的目标文件夹内,我们可以找到由Gatling执行的负载测试报告。
3. Integrate Gatling With Jenkins
3.将Gatling与Jenkins相结合
Integrating Gatling into a Jenkins pipeline allows us to perform load tests during its execution.
将Gatling集成到Jenkins管道中,允许我们在其执行过程中进行负载测试。
In this way, we can verify that the changes made in the code we’re releasing don’t introduce significant drops in performance.
通过这种方式,我们可以验证我们正在发布的代码所做的改变不会带来性能上的显著下降。
This increases reliability and confidence in the new code to be issued.
这增加了对即将发布的新代码的可靠性和信心。。
3.1. Create the Jenkinsfile
3.1.创建Jenkinsfile
Within our project, we create a Jenkinsfile instructing it how to run Gatling:
在我们的项目中,我们创建了一个Jenkinsfile,指示它如何运行Gatling:。
pipeline {
agent any
stages {
stage("Build Maven") {
steps {
sh 'mvn -B clean package'
}
}
stage("Run Gatling") {
steps {
sh 'mvn gatling:test'
}
post {
always {
gatlingArchive()
}
}
}
}
}
The script is divided into two stages:
脚本分为两个阶段:
- Build the project (Maven)
- Run and archive our scenarios (Gatling)
Next, we commit and push the test code to our source management system. Once configured, Jenkins will be able to read and execute the newly created script.
接下来,我们提交并推送测试代码到我们的源代码管理系统。一旦配置好,Jenkins将能够读取并执行新创建的脚本。
3.2. Create the Pipeline
3.2.创建管道
Using this JenkinsFile, we’ll create our own pipeline. Creating a pipeline in Jenkins is simple.
使用这个JenkinsFile,我们将创建我们自己的管道。在Jenkins中创建一个管道很简单。
Let’s first navigate to the Jenkins home page, and click on New Item, select Pipeline and give it a meaningful name. To learn more about creating a pipeline in Jenkins, we can visit our tutorial dedicated to the topic.
让我们首先导航到Jenkins主页,点击新项目,选择管道并给它起一个有意义的名字。要了解有关在Jenkins中创建管道的更多信息,我们可以访问我们专门针对该主题的教程.。
Let’s configure the newly created pipeline. In the Pipeline section, we select the source of the script.
让我们来配置新创建的管道。在Pipeline部分,我们选择脚本的来源。
In particular, let’s select from the drop-down menu Pipeline script from SCM, set the URL of our repository, set the credentials needed to retrieve the source code, supply the branch from which to receive the script, and finally indicate the path where to find our newly-created Jenkinsfile.
特别是,让我们从下拉菜单中选择从SCM输送脚本,设置我们仓库的URL,设置检索源代码所需的证书,提供接收脚本的分支,最后指出找到我们新创建的Jenkinsfile的路径。
The window should appear as follows:
窗口应该显示如下:
3.3. Gatling Jenkins Plugin
3.3. Gatling Jenkins插件
Before running the newly created pipeline, we need to install the Gatling Jenkins Plugin.
在运行新创建的管道之前,我们需要安装Gatling Jenkins Plugin。
The plugin allows us to:
该插件允许我们:
- Obtain and publish detailed reports at each pipeline run
- Keep track of each simulation and provide trend graphs
The gatlingArchive() command inserted into the pipeline is part of this plugin, and it’s the one that allows us to enable the reports just mentioned.
插入管道的gatlingArchive()命令是这个插件的一部分,是它让我们能够启用刚才提到的报告。
Let’s install the plugin and restart Jenkins.
让我们安装该插件并重新启动Jenkins。
At this point, the pipeline with our load test is ready to be run.
在这一点上,带有我们负载测试的管道已经准备好运行。
3.4. Separate a Load Generation
3.4.分离出一个负载生成
Generating a load of calls to perform the test is quite an expensive operation in terms of resources. For this reason, it’s not a good idea to perform these operations on the master Jenkins node that’s running the pipeline.
生成负载调用以执行测试,就资源而言是相当昂贵的操作。由于这个原因,在运行管道的主Jenkins节点上执行这些操作并不是一个好主意。
We’ll use a Jenkins slave node to perform some of the steps in the pipeline.
我们将使用一个Jenkins从属节点来执行管道中的一些步骤。
Suppose we’ve correctly configured one or more slave nodes on Jenkins; the agent any command inserted into the newly-created pipeline allows us to allocate an executor node and run the specified code on that node.
假设我们已经在Jenkins上正确配置了一个或多个从属节点;在新创建的管道中插入的agent any命令允许我们分配一个执行器节点并在该节点上运行指定代码。
4. Run the Pipeline
4.运行管道
It’s time to run our pipeline.
是时候运行我们的管道了。
From the Jenkins home, we select the newly created pipeline and click Build Now. We then wait for the pipeline to run. At the end of the execution, we should see a chart similar to this:
从Jenkins主页,我们选择新创建的管道并点击Build Now。然后我们等待管道的运行。在执行结束时,我们应该看到一个类似这样的图表:。
5. View the Result
5.查看结果
In the execution page of our pipeline, we can see how a graph has been generated, which shows the average response times generated by our load test. The graph is generated by the Gatling Jenkins Plugin. It will contain the data of the last 15 builds, so as to give immediate evidence of the performance trend of our releases.
在我们管道的执行页面中,我们可以看到一个图表是如何生成的,它显示了我们的负载测试所产生的平均响应时间。该图表是由Gatling Jenkins插件生成的。它将包含过去15次构建的数据,以便对我们发布的性能趋势提供直接证据。
If we click on the Gatling button in the menu on the left, inside the execution page of our pipeline, we’ll see graphs showing the trend of our last 15 builds.
如果我们点击左侧菜单中的Gatling按钮,在我们管道的执行页面内,我们将看到显示我们过去15次构建的趋势的图表。
In particular, we’ll have information about:
特别是,我们会有以下信息:
- Average response time
- 95th percentile of the response time, and
- Percentage of requests in “KO” (that is, “not okay”)
At the bottom of the page, after the graphs just mentioned, we’ll find links to the Gatling reports generated for each of the builds made.
在页面底部,在刚才提到的图表之后,我们会发现链接到为每个构建生成的加特林报告。
By clicking on the links, we can view, directly within Jenkins, the Gatling reports that have been generated:
通过点击链接,我们可以直接在Jenkins内查看已经生成的Gatling报告:。
6. Conclusion
6.结语
In this tutorial, we’ve seen how to insert load tests performed with Gatling into a Jenkins pipeline. We started by explaining how to generate a load test with Gatling, how to create a Jenkinsfile to run it, and how to integrate it into a Jenkins pipeline.
在本教程中,我们已经看到了如何将用Gatling进行的负载测试插入到Jenkins管道中。我们首先解释了如何用Gatling生成一个负载测试,如何创建一个Jenkinsfile来运行它,以及如何将其集成到Jenkins管道中。
Finally, we’ve shown how the Gatling Jenkins Plugin is useful to generate Gatling reports directly inside our Jenkins deployment.
最后,我们展示了Gatling Jenkins插件是如何在我们的Jenkins部署中直接生成Gatling报告的。
To learn more about how to structure a test scenario to monitor the performance of a website, let’s visit another Gatling tutorial of ours.
要了解更多关于如何构建测试场景以监测网站性能的信息,让我们访问我们的另一个Gatling教程。
As always, the full source code of the tutorial is available over on GitHub.
一如既往,该教程的完整源代码可在GitHub上获得。。