Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)

上三篇文章中,我们从windows下Web自动化测试工具的安装再到将Web自动化测试工具RobotFramework+Selenium+Chome容器化,最后利用容器化后的自动化工具与Jenkins Docker Cloud集成,并在Pipeline中实现Web的自动化测试。
三篇文章链接见下:
Devops关键工具及技术(六)—基于Pipeline的Web自动化测试(Windows下自动化测试工具安装)
Devops关键工具及技术(六)—基于Pipeline的Web自动化测试(Web自动化测试工具容器化)
Devops关键工具及技术(六)—基于Pipeline的Web自动化测试(Web自动化测试的流水线集成)

有了自动化测试、自然性能测试也需要成为Pipeline的一部分。性能测试的工具常见的主要有Jmeter和LoadRunner。我们将选用Jmeter作为我们的主角。因为它是免费的。由于在Windows下安装Jmeter比较简单,所以我们在Jmeter性能测试会由两种文章来介绍。分别是Jmeter性能测试容器化、Jmeter性能测试的流水线集成。

本篇我们将先介绍Jmeter性能测试的流水线集成

1、性能测试节点

我们将会参考Devops关键工具及技术(一)—Jenkins 容器化和Devops关键工具及技术(六)—基于Pipeline的Web自动化测试(Web自动化测试的流水线集成)的增加Slave和Robot节点方式来将Jmeter性能测试节点加入到Jenkins中,目的为了将Jmeter性能测试成为流水线的一部分。
在此之前我们需要将Jmeter测试进行容器化,具体可以参考Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试(Jmeter容器化)。

  • JenkinsMaster上配置Jmeter性能测试节点的信息

在做配置之前我们也还是需要对该加入进来的节点做一下配置,具体的操作可以从上面提到的两篇文章中获知,这里不做过多讲解。可参考下面的截图
在这里插入图片描述
Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)_第1张图片
Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)_第2张图片

2、Pipeline流水线的集成

基于之前Web自动化测试的流水线,我们加入Jmeter性能测试的Stage。在此之前我们的Pipeline里面加入了Checkout Code、Mvn Build、Sonar、Bash Deploy、RobotFramework等Stage,这次我们在后面加上Jmeter性能测试的Stage。如下:

  • 性能测试脚本

脚本可以在这里找到:https://github.com/zbbkeepgoing/springboot-demo/blob/master/jmeter/demo.jmx



  
    
      
      false
      true
      false
      
        
      
      
    
    
      
        continue
        
          false
          10
        
        2
        0
        false
        
        
      
      
        
          
            
          
          192.168.88.130
          8080
          http
          
          /
          GET
          true
          false
          true
          false
          
          
          
        
        
        
          
            
          
          192.168.88.130
          8080
          http
          
          /rightaway
          GET
          true
          false
          true
          false
          
          
          
        
        
        
          
            
          
          192.168.88.130
          8080
          http
          
          /sleep
          GET
          true
          false
          true
          false
          
          
          
        
        
        
          false
          
            saveConfig
            
              
              true
              true
              true
              
              true
              true
              true
              true
              false
              true
              true
              false
              false
              false
              true
              false
              false
              false
              true
              0
              true
              true
              true
              true
              true
              true
            
          
          
        
        
        
          false
          
            saveConfig
            
              
              true
              true
              true
              
              true
              true
              true
              true
              false
              true
              true
              false
              false
              false
              true
              false
              false
              false
              true
              0
              true
              true
              true
              true
              true
              true
            
          
          
        
        
        
          false
          
            saveConfig
            
              
              true
              true
              true
              
              true
              true
              true
              true
              false
              true
              true
              false
              false
              false
              true
              false
              false
              false
              true
              0
              true
              true
              true
              true
              true
              true
            
          
          
        
        
      
    
  

  • Pipeline内容

内容也可以在Github中找到
https://github.com/zbbkeepgoing/pipeline-sample

pipeline {
    agent none 
    stages {
       stage('Preparation') { 
            agent { node { label 'master' } }
            steps {
               checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]])
            }
        }
         
        stage('Build') { 
            agent { node { label 'master' } }
            steps {
                dir(env.WORKSPACE){
                  sh "mvn clean install"
                  junit allowEmptyResults: true, keepLongStdio: true, testResults: 'target/surefire-reports/*.xml'
                  sh "mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar"
                }
            }
        }
        stage('Sonarqube') { 
            agent { node { label 'master' } }
            steps {
                dir(env.WORKSPACE){
                  sh "mvn sonar:sonar -Dsonar.host.url=http://192.168.88.130:9000 -Dsonar.login=65607ba9d0f54590cf55fe8e60134fb5e87c557d"
                }
            }
        }
        stage('Deploy') { 
            agent { node { label 'master' } }
            steps {
                dir(env.WORKSPACE){
                   sh 'sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible'
                   sh 'sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible'
                   sh 'sshpass -p 1qaz@WSX ssh -o StrictHostKeychecking=no [email protected] "bash /opt/ansible/deploy.sh"'
                   sh 'sleep 8s'
                }
            }
        }
        stage('Robot Framework') { 
            agent { node { label 'robot' } }
            steps {
               dir(env.WORKSPACE){
	                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]])
	                sh "pybot -d /opt/workspace/CI+Sonar+Sh+Robot+Jmeter robot/demo.robot"
	                step([$class: 'RobotPublisher',
	                  disableArchiveOutput: false,
	                  logFileName: 'log.html',
	                  otherFiles: '',
	                  outputFileName: 'output.xml',
	                  outputPath: '.',
	                  passThreshold: 40,
	                  reportFileName: 'report.html',
	                  unstableThreshold: 0]);
	            }
            }
        }
		stage('Jmeter') { 
            agent { node { label 'jmeter' } }
            steps {
                sh "rm -rf  /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/*"    //上次掉目标的目录,如果后续执行命令输出文件时,文件继续存在将报错,无法生成文件
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]])   //拉取最新代码,因为脚本在我们的repo中
                sh "mkdir -p /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/output"   //创建Jmeter产生的输出目录
                sh "jmeter.sh -n -t /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/demo.jmx  -l /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/demo.jtl -j /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/demo.log -e -o /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/output"    //执行性能测试的命令行
                step([$class: 'ArtifactArchiver', artifacts: 'jmeter/*,jmeter/output/*'])    //将Jmeter生成的文件都进行归档,直接提供在Jenkins Build后的页面查看
                perfReport "jmeter/demo.jtl"    //生成Jmeter的报告(该报告是Jenkins通过performance插件进行分析出来的结果。Jmeter自身的报告还需要看output中的文件)
            }
        }
    }
}
  • 新建Pipeline
    Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)_第3张图片

  • 执行Pipeline

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
......
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] checkout
......
Commit message: "Update deploy.sh"
 > git rev-list --no-walk 1aa03a20b88565b775a23a759fde2701cabe8592 # timeout=10
......
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ mvn clean install
......
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
......
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 31.154 s
[INFO] Finished at: 2018-10-20T09:27:22+00:00
[INFO] Final Memory: 29M/69M
[INFO] ------------------------------------------------------------------------
[Pipeline] junit
Recording test results
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar
......
[Pipeline] { (Sonarqube)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ mvn sonar:sonar -Dsonar.host.url=http://192.168.88.130:9000 -Dsonar.login=65607ba9d0f54590cf55fe8e60134fb5e87c557d
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
......
[INFO] Task total time: 22.811 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.828 s
[INFO] Finished at: 2018-10-20T09:28:02+00:00
[INFO] Final Memory: 32M/154M
[INFO] ------------------------------------------------------------------------
......
[Pipeline] { (Deploy)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ sshpass -p 1qaz@WSX ssh -o StrictHostKeychecking=no [email protected] bash /opt/ansible/deploy.sh
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ sleep 8s
......
[Pipeline] { (Robot Framework)
[Pipeline] node
Still waiting to schedule task
robot-0000nio3k3235 is offline
Running on robot-0000nio3k3235 on 192.168.88.130robot in /opt/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] dir
Running in /opt/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] checkout
......
Commit message: "Update deploy.sh"
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ pybot -d /opt/workspace/CI+Sonar+Sh+Robot+Jmeter robot/demo.robot
==============================================================================
Demo                                                                          
==============================================================================
Demo                                                                  | PASS |
------------------------------------------------------------------------------
Baidu                                                                 | PASS |
------------------------------------------------------------------------------
Demo                                                                  | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output:  /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/output.xml
Log:     /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/log.html
Report:  /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/report.html
[Pipeline] step
Robot results publisher started...
-Parsing output xml:
Done!
-Copying log files to build dir:
Done!
-Assigning results to build:
Done!
-Checking thresholds:
Done!
Done publishing Robot results.
......
[Pipeline] { (Jmeter)
[Pipeline] node
Running on jmeter-0000nkxischdk on 192.168.88.130jmeter in /opt/workspace/CI+Sonar+Sh+Robot+Jmeter
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ rm -rf /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/*
[Pipeline] checkout
Cloning the remote Git repository
Cloning repository https://github.com/zbbkeepgoing/springboot-demo.git
......
Checking out Revision 1aa03a20b88565b775a23a759fde2701cabe8592 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 1aa03a20b88565b775a23a759fde2701cabe8592
Commit message: "Update deploy.sh"
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ mkdir -p /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/output
[Pipeline] sh
[CI+Sonar+Sh+Robot+Jmeter] Running shell script
+ jmeter.sh -n -t /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/demo.jmx -l /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/demo.jtl -j /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/demo.log -e -o /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/output
Oct 20, 2018 9:32:29 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
Creating summariser 
Created the tree successfully using /opt/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter/demo.jmx
Starting the test @ Sat Oct 20 09:32:31 UTC 2018 (1540027951645)
Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445
summary +      1 in 00:00:01 =    1.1/s Avg:   182 Min:   182 Max:   182 Err:     0 (0.00%) Active: 2 Started: 2 Finished: 0
......
Tidying up ...    @ Sat Oct 20 09:33:50 UTC 2018 (1540028030668)
... end of run
[Pipeline] archiveArtifacts
Archiving artifacts
[Pipeline] perfReport
Performance: Recording JMeterCsv reports 'jmeter/demo.jtl'
......
Performance: File demo.jtl reported 0.0% of errors [SUCCESS]. Build status is: SUCCESS
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
  • 查看及获取Jmeter测试报告

有三个地方我们可以查看以及获取报告:
1、Jenkins构建结果中:
Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)_第4张图片
Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)_第5张图片
2、上图中点击上次成功的成品。可以下载原报告进行查看
Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)_第6张图片
Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[二](Jmeter性能测试的流水线集成)_第7张图片
3、在宿主机的目录下

[root@localhost jmeter]# ll
total 40
-rw-r--r--. 1 root root 10762 Oct 20 17:32 demo.jmx
-rw-r--r--. 1 root root  6810 Oct 20 17:33 demo.jtl
-rw-r--r--. 1 root root 17438 Oct 20 17:33 demo.log
drwxr-xr-x. 4 root root    79 Oct 20 17:33 output
[root@localhost jmeter]# pwd
/opt/jmeter/workspace/CI+Sonar+Sh+Robot+Jmeter/jmeter
[root@localhost jmeter]# cd output/
[root@localhost output]# ls
content  index.html  README.TXT  sbadmin2-1.0.7
[root@localhost output]# ll
total 28
drwxr-xr-x. 5 root root    40 Oct 20 17:33 content
-rw-r--r--. 1 root root  9465 Oct 20 17:33 index.html
-rw-r--r--. 1 root root 15437 Oct 20 17:33 README.TXT
drwxr-xr-x. 5 root root    89 Oct 20 17:33 sbadmin2-1.0.7
[root@localhost output]# 

以上就是我们Jmeter性能测试集成Pipeline的所有内容。

你可能感兴趣的:(Devops)