1. Overview
1.概述
In this article, we’re going to configure a continuous delivery pipeline using Jenkins and Apache JMeter.
在本文中,我们将使用Jenkins和Apache JMeter配置一个持续交付管道。
We’ll rely on the JMeter article as a great starting-point to first understand the basics of JMeter, as it already has some configured performance tests we can run. And, we’ll use the build output of that project to see the report generated by the Jenkins Performance plugin.
我们将依靠JMeter文章作为一个很好的起点,首先了解JMeter的基础知识,因为它已经有一些我们可以运行的配置的性能测试。而且,我们将使用该项目的构建输出来查看Jenkins 性能插件所生成的报告。
2. Setting up Jenkins
2.设置Jenkins
First of all, we need to download the latest stable version of Jenkins, navigate to the folder where our file is and run it using the java -jar jenkins.war command.
首先,我们需要下载Jenkins的最新稳定版本,导航到我们文件所在的文件夹,并使用java -jar jenkins.war命令运行它。
Keep in mind that we can’t use Jenkins without an initial users setup.
请记住,如果没有初始用户设置,我们就无法使用Jenkins。
3. Installing the Performance Plugin
3.安装性能插件
Let’s install the Performance plugin, which is necessary for running and displaying JMeter tests:
让我们安装Performance插件,它是运行和显示JMeter测试所必需的。
Now, we need to remember to restart the instance.
现在,我们需要记住重新启动实例。
4. Run JMeter Tests With Jenkins
4.用Jenkins运行JMeter测试
Now, let’s go to Jenkins home page and click on “create new jobs”, specify a name, select Freestyle project and click “OK”.
现在,让我们去Jenkins主页,点击 “创建新工作”,指定一个名称,选择Freestyle项目,然后点击 “确定”。
On the Execute shell Sub-tab we write a script to perform the necessary actions after the repository is cloned:
在Execute shellSub-tab上,我们写一个脚本,在版本库被克隆后执行必要的操作。
- Navigate to the desired sub-module
- We compiled it
- We deployed it, knowing that it’s a spring-boot based project
- We wait until the app is available on the port 8989
- And at the end, we just specify both the path of our JMeter script (located inside the resource folder of the jmeter module) to use for performance testing and the path of the resultant file (JMeter.jtl) also in the resource folder
Here is the small corresponding shell script:
下面是相应的小的shell脚本。
cd jmeter
./mvnw clean install -DskipTests
nohup ./mvnw spring-boot:run -Dserver.port=8989 &
while ! httping -qc1 http://localhost:8989 ; do sleep 1 ; done
jmeter -Jjmeter.save.saveservice.output_format=xml
-n -t src/main/resources/JMeter.jmx
-l src/main/resources/JMeter.jtl
As shown in the following image:
如以下图片所示。
After the project is cloned from GitHub, we compiled it, opened on port 8989 and processed the performance tests, we need to make the performance plugin display result in a user-friendly way.
从GitHub上克隆项目后,我们对其进行了编译,在8989端口打开并处理了性能测试,我们需要使性能插件以用户友好的方式显示结果。
We can do that by adding a dedicated Post-build Actions. We need to provide the results source file and configure the action:
我们可以通过添加一个专门的Post-build Actions来做到这一点。我们需要提供结果源文件并配置该动作。
We choose the Standard Mode with the subsequent configs:
我们选择标准模式与后续配置。
Let’s hit Save, on the left menu of Jenkins dashboard click on the button Build Now and wait for it to finish the set of operations we configured up there.
让我们点击Save,在Jenkins仪表板的左边菜单上点击Build Now按钮,等待它完成我们在上面配置的一系列操作。
After it’s finished, we’ll see on the console all outputs of our project. At the end we’ll get either Finished: SUCCESS or Finished: FAILURE:
完成后,我们将在控制台看到我们项目的所有输出。最后,我们会得到Finished:SUCCESS或者Finished:FAILURE。
Let’s go to the Performance Report area accessible via the left side menu.
让我们进入绩效报告区,可通过左侧菜单进入。
Here we’ll have the report of all past builds including the current one to see the difference in term of performance:
在这里,我们将看到所有过去的构建报告,包括当前的构建,以查看性能方面的差异。
Let’s click on the indication just up of the table to have only the result of the last build we just made:
让我们点击表格正上方的指示,只看到我们刚刚做的最后一次构建的结果。
From the dashboard of our project we can get the Performance Trend, which are other graphs showing the last builds results:
从我们项目的仪表盘上,我们可以得到性能趋势,这是显示最后一次构建结果的其他图表。
Note: Applying the same thing to a Pipeline project is also simple as:
注意:将同样的东西应用于管道项目也很简单。
- Create another project (item) from the dashboard and name it JMeter-pipeline for example (General info Tab)
- Select Pipeline as project type
- On the Pipeline Tab, on the definition select Pipeline script and check Use Groovy Sandbox
- In the script area just fill the following lines:
node {
stage 'Build, Test and Package'
git 'https://github.com/eugenp/tutorials.git'
dir('jmeter') {
sh "./mvnw clean install -DskipTests"
sh 'nohup ./mvnw spring-boot:run -Dserver.port=8989 &'
sh "while ! httping -qc1
http://localhost:8989 ; do sleep 1 ; done"
sh "jmeter -Jjmeter.save.saveservice.output_format=xml
-n -t src/main/resources/JMeter.jmx
-l src/main/resources/JMeter.jtl"
step([$class: 'ArtifactArchiver', artifacts: 'JMeter.jtl'])
sh "pid=\$(lsof -i:8989 -t); kill -TERM \$pid || kill -KILL \$pid"
}
}
This script starts by cloning the project, goes in the target module, compile and run it to make sure the app is accessible at http://localhost:8989
这个脚本从克隆项目开始,进入目标模块,编译并运行它,以确保应用程序可以在http://localhost:8989。
Next, we run JMeter tests located in the resource folder, save the results as the build output, and finally, the application is closed.
接下来,我们运行位于资源文件夹中的JMeter测试,将结果保存为构建输出,最后,应用程序被关闭。
5. Conclusion
5.结论
In this quick article, we have settled up a simple continuous delivery environment to run and show Apache JMeter tests in Jenkins in two ways; first via a Freestyle project and second with a Pipeline.
在这篇文章中,我们建立了一个简单的持续交付环境,以两种方式在Jenkins中运行和显示Apache JMeter测试;首先通过Freestyle项目,其次使用Pipeline。
As always, the source code for this article can be found over on GitHub.
像往常一样,本文的源代码可以在GitHub上找到over。