JMeter + Maven in Jenkins

Environment prepares

  1. Write the JMeter scripts.
  2. Install Java, Jenkins, Maven environment in system.

Run JMeter scripts by Maven

  1. Create the Maven project and organize directory.
  2. copy the JMeter scripts into the src/test/jmeter directory. It's the default directory set by jmeter-maven-plugin which used to run script.
  3. copy the Property file into the src/test/jmeter directory. You can edit it to override JMeter's default properties.
  4. copy the resources to src/test/resources directory.

That's the screenshot:

  1. Edit the Pom.xml
    It's is the whole content in Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>testops.top</groupId>
    <artifactId>Jmeter</artifactId>
    <version>1.0</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Specify resources source dir-->
        <jmeter.resources.source.dir>${project.basedir}/src/test/resources</jmeter.resources.source.dir>
        <!-- Specify the resources output directory after build -->
        <jmeter.resources.output.dir>${project.build.directory}/jmeter/html</jmeter.resources.output.dir>
        <!-- Specify testFiles source dir-->
        <jmeter.testFiles.source.dir>${project.basedir}/src/test/jmeter</jmeter.testFiles.source.dir>
        <!-- JTL directory of jmeter test result -->
        <jmeter.result.jtl.dir>${project.build.directory}/jmeter/results</jmeter.result.jtl.dir>
        <!-- The stylesheet used for converting JTL to HTML-->
        <jmeter.result.html.dir>${project.build.directory}/jmeter/html</jmeter.result.html.dir>
        <jmeter.result.html.stylesheetFile>${jmeter.result.html.dir}/jmeter-results-detail-report_21.xsl
        </jmeter.result.html.stylesheetFile>

    </properties>
    <build>
        <plugins>
            <plugin>
                <!-- copy resources to the output directory -->
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${jmeter.resources.output.dir}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${jmeter.resources.source.dir}</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>3.4.0</version>
                <executions>
                    <!-- Generate JMeter configuration -->
                    <execution>
                        <id>configuration</id>
                        <goals>
                            <goal>configure</goal>
                        </goals>
                    </execution>
                    <!-- Run JMeter tests -->
                    <execution>
                        <id>jmeter-tests</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                    <!--                     Fail build on errors in test-->
                    <execution>
                        <id>jmeter-check-results</id>
                        <goals>
                            <goal>results</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- Specify testFiles directory -->
                    <testFilesDirectory>${jmeter.testFiles.source.dir}</testFilesDirectory>
                    <!-- The JTL results directory -->
                    <resultsDirectory>${jmeter.result.jtl.dir}</resultsDirectory>
                    <!-- The Jar Extensions In Jmeter -->
                    <jmeterExtensions>
                        <artifact>kg.apc:jmeter-plugins-functions:2.1</artifact>
                        <artifact>MysqL:mysql-connector-java:8.0.23</artifact>
                    </jmeterExtensions>
                    <!-- The plugin uses some broken dependencies
                         An alternative is to set this to true and use excludedArtifacts, see below
                    -->
                    <downloadExtensionDependencies>false</downloadExtensionDependencies>
                    <!-- To set <generateReports> is used to make <resultsFileFormat> useful when it is "xml" -->
                    <generateReports>false</generateReports>
                    <resultsFileFormat>xml</resultsFileFormat>
                </configuration>
            </plugin>
            <plugin>
                <!-- Current plugin convert jlt in xml style to html -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>xml-maven-plugin</artifactId>
                <version>1.0.2</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>transform</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <transformationSets>
                        <transformationSet>
                            <dir>${jmeter.result.jtl.dir}</dir>
                            <stylesheet>${jmeter.result.html.stylesheetFile}</stylesheet>
                            <outputDir>${jmeter.result.html.dir}</outputDir>
                            <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>
        </plugins>
    </build>
</project>

If you can't learn why it is or how to edit, you sould learn the follow kNowledges:

  • Maven Plugin: xml-maven-plugin, jmeter-maven-plugin, maven-resources-plugin
  1. Run scripts by mvn
mvn clean verify
  1. Read the Html Project


相关文章

Jmeter:Authenticationcredentialswerenotprovided上次在使...
初次使用jmeter时,结果树中返回的数据为未转码内容,如下:...
jmeter默认语言设置: 1、临时设置:进入options--ChooseLa...
第一步:打开jmeter工具。 第二步:点击鼠标右击,点击添加...
在之前的博文中,Jmeter二次开发——基于Java请求,已介绍了...
打开虚拟机然后用远程连接工具SSH连接到数据库将serveragent...