Jmeter+Maven+Gitlab-ci持续集成简单实践

Jmeter+Maven+Gitlab-ci持续集成实践
前提:jmeter脚本已开发完毕,gitlab环境已搭建完毕,使用的docker镜像文件已打好
一、新建Maven工程
1、先新建maven工程,目录结构如下:
在这里插入图片描述
说明:src/main/resources目录为测试报告模板,src/test/jmeter目录下为jmeter脚本和csv文件
2、pom.xml文件配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.travelsky</groupId>
  <artifactId>mda</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mda</name>
  <url>http://maven.apache.org</url>

        
	<!-- maven项目编码设置 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
	<!-- 测试结果存放路径 -->
    <jmeter.result.jtl.dir>${project.build.directory}\jmeter\results</jmeter.result.jtl.dir>
    <!-- 测试报告存放路径 -->
    <jmeter.result.html.dir>${project.build.directory}\jmeter\html</jmeter.result.html.dir>
    <ReportName>TestReport</ReportName>
  </properties>

  <dependencies>

    
  </dependencies>
  
  
  <build>
       <plugins>      
        <plugin>
        <!-- 核心插件,用来执行jmx脚本,注意版本号,2.1.0可以使用用jmeter3.1生成的脚本。最新的2.2.0使用jmeter3.2生成的脚本 -->
               <groupId>com.lazerycode.jmeter</groupId>
               <artifactId>jmeter-maven-plugin</artifactId>
               <version>2.1.0</version>           
                 <configuration>
                    <!-- 设置jmeter生成结果文件格式-->
                    <resultsFileFormat>xml</resultsFileFormat>
                    <!-- 设置忽略失败是否停止运行-->
                    <ignoreResultFailures>true</ignoreResultFailures>
                    <!--设置结果是否有时间戳-->
                    <testResultsTimestamp>false</testResultsTimestamp>
                    <encoding>UTF-8</encoding> <!-- 项目的编码 -->
                </configuration>
              <executions>
                   <execution>
                       <id>jmeter-tests</id>
                       <phase>verify</phase>
                       <!--脚本所在的文件夹 -->
                       <goals>
                           <goal>jmeter</goal>
                       </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
            <!--根据xsl模版把jtl文件转换成html,官网地址: http://www.mojohaus.org/xml-maven-plugin/examples/transform-saxon.html-->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>xml-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>transform</goal>
                        </goals>
                    </execution>
                </executions> 
            <configuration>
                    <transformationSets>
                    <!-- 可以根据不同的模版,同事生成多个报告-->
                        <transformationSet>
                            <dir>${jmeter.result.jtl.dir}</dir>
                            <stylesheet>src/test/resources/jmeter-results-shanhe-me.xsl</stylesheet>
                            <outputDir>${jmeter.result.html.dir}</outputDir>
                           <!-- 把jtl格式转传承html -->
                            <fileMappers>
                                <fileMapper
                                    implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
                                    <targetExtension>html</targetExtension>
                                </fileMapper>
                            </fileMappers>
                        </transformationSet>
                    </transformationSets>
                </configuration>
                <!-- using XSLT 2.0 -->
                 <dependencies>
                   <dependency>
                   <groupId>net.sf.saxon</groupId>
                   <artifactId>saxon</artifactId>
                   <version>8.7</version>
                   </dependency>
               </dependencies>
            </plugin>
            
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source><!-- 源代码开发版本 -->
                <target>1.8</target><!-- java编译版本 -->
                <encoding>UTF-8</encoding> <!-- 项目的编码 -->
            </configuration>
        	</plugin>
        	
        	   	<!--maven-dependency-plugin是处理与依赖相关的插件,本次测试jmeter需要用的依赖配置在configuration-->
        	<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>	 
								<artifactItem>
								    <groupId>org.apache.jmeter.protocol.ssh.sampler</groupId>
								    <artifactId>ApacheJMeter_ssh</artifactId>
								    <version>1.1.1-SNAPSHOT</version>
								    <outputDirectory>target/jmeter/lib/ext</outputDirectory> 
								</artifactItem>
								
								<artifactItem>
								     <groupId>com.jcraft</groupId>
									 <artifactId>jsch</artifactId>
									 <version>0.1.50</version>
								     <outputDirectory>target/jmeter/lib/ext</outputDirectory>
								</artifactItem>
								
								<artifactItem>
									<groupId>com.google.code.gson</groupId>
								    <artifactId>gson</artifactId>
								    <version>2.8.5</version>
						            <outputDirectory>target/jmeter/lib/ext</outputDirectory>
								     
								</artifactItem>
								
								<artifactItem>
                                    <groupId>junit</groupId>
                                    <artifactId>junit</artifactId>
                                    <version>3.8.1</version>
                                    <outputDirectory>target/jmeter/lib/ext</outputDirectory>
                                </artifactItem>
                                
                                <artifactItem>
                                    <groupId>org.postgresql</groupId>
    								<artifactId>postgresql</artifactId>
    								<version>42.2.5</version>
                                    <outputDirectory>target/jmeter/lib/ext</outputDirectory>
                                </artifactItem>
                            </artifactItems>                  
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            </plugins>
        </build>
  
</project>

3、执行maven工程,run as->Maven build->Goals输入verify,确认后几开始构建,构建过程以及构建结果如下图:
构建过程图:
在这里插入图片描述
构建结果图:
在这里插入图片描述
在这里插入图片描述
4、测试报告查看,在构建完成后,在target/jmeter/html目录下会生成测试报告,如下图
在这里插入图片描述
二、集成到gitlab-ci
前提:本次gitlab环境以及docker镜像使用均是已有的
1、首先创建project,然后将整个工程上传至根目录,我们知道使用.gitlab-ci.yml来配置我们构建的项目,同时我们需要上传settings.xml文件至pom.xml的目录,项目结构图如下:
在这里插入图片描述
进入项目后:
在这里插入图片描述
2、.gitlab-ci.yml文件配置
在这里插入图片描述
本次用到.gitlab-ci.yml配置很简单,需要了解更多请详细了解.gitlab-ci.yml用法。本次配置:
Stages:定义一个工作场景阶段build,默认是test
Image:需要使用的docker镜像
Only:定义job所引用的git分支,此处表示打tags的时候会自动触发构建
Script:执行的shell脚本或者linux指令
3、然后我们在Repository->Tags,新建tag,即可看到项目正在构建中,我们选择CI/CD->Pipelines,即可看到正在构建中的stage,可进入job的日志查看,执行完成后显示Job success即构建成功
在这里插入图片描述
在这里插入图片描述
三、总结
本次gitlab以及使用的docker镜像均是已有的,所以本次jmeter+maven+gitlab-ci使用较为简单,可学习maven整合jmeter脚本,但是gitlab-ci集成maven只是皮毛,可用来了解学习入门,然后就是继续努力。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值