Try using jmeter-maven-plugin

Prerequisite preparation

1. maven project

2. JMeter, Jenkins, maven, jdk have been installed

Environmental requirements:

jmeter>5.6.2

maven>3.9

jdk>1.8

Jenkins?

Note: jmeter-maven-plugin does not need to be downloaded, you can check the relevant address: GitHub – jmeter-maven-plugin/jmeter-maven-plugin: The JMeter Maven Plugin

Create maven project

Open the ij editor, File–>New–>Project, select Maven, click next, enter Groupid and ArtifactId (just fill this in casually for now)

Add jmeter related files

1. In the maven project, if it is a module project, you can find a module, such as the api module, and create a folder in ${project.base.directory}/src/test/ to record jmeter and resources. (Similarly if there is only one module)

jmeter folder content storage source: configuration document under the bin file in the jmeter installation directory + jmeter script (jmeter script required for execution)

Resource directory content storage document source: extras file in the jmeter installation directory

The file type of the resources folder needs to be modified. Right-click on the resources folder-Mark Directory as-test resources Root, as follows

Modify the project’s pom

The contents of the entire pom.xml file are as follows:

<?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>Grit</groupId>
    <artifactId>GritJmeter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!--This path is used to store JMeter’s native test result files, such as csv or jtl-->
        <jmeter.result.jtl.dir>${project.build.directory}\jmeter\results</jmeter.result.jtl.dir>
        <!--This path is used to store the html file of detailed test results converted from the template-->
        <jmeter.result.html.dir>${project.build.directory}\jmeter\html_detail_report</jmeter.result.html.dir>
        <!--This path is used to store the html file of the test results converted from the template-->
        <jmeter.result.html.dir1>${project.build.directory}\jmeter\html_report</jmeter.result.html.dir1>
        <ReportName>TestReport</ReportName>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_core</artifactId>
            <version>5.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>5.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>jorphan</artifactId>
            <version>5.4.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <!--Specify jmeter version number-->
                    <jmeterVersion>5.4.3</jmeterVersion>

                    <!-- JVM settings-->
                    <jMeterProcessJVMSettings>
                        <xms>2048</xms>
                        <xmx>2048</xmx>
                    </jMeterProcessJVMSettings>
                    <!--JMeter can create .jtl (XML format) test results and csv test results. By default this plugin uses csv format. Here you need to switch it to xml format. -->
                    <resultsFileFormat>xml</resultsFileFormat>
                    <!--Whether to generate a result report here needs to be set to false, otherwise an error message "No content is allowed in the preface" will appear during runtime.
                    Because if this item is true, no matter you set the format display of the result file to xml in the .properties configuration file or this pom file, only a csv result file will be generated in the end.
                    The csv format cannot be converted into the html result file we want, and the above error will be reported. -->
                    <generateReports>true</generateReports>
                    <!--Ignore failure-->
                    <ignoreResultFailures>true</ignoreResultFailures>
                    <!--The output timestamp at the end of the report file -->
                    <appendResultsTimestamp>true</appendResultsTimestamp>
                    <!--Specify log level-->
                    <overrideRootLogLevel>error</overrideRootLogLevel>
                    <!-- Add any other Java libraries to JMeter's lib/ext directory -->
                    <jmeterExtensions>
                        <artifact>kg.apc:jmeter-plugins-standard:1.4.0</artifact>
                        <artifact>kg.apc:jmeter-plugins-extras:1.4.0</artifact>
                        <artifact>kg.apc:jmeter-plugins-perfmon:2.1</artifact>
                        <artifact>kg.apc:jmeter-plugins-manager:1.7</artifact>
                    </jmeterExtensions>
                    <junitLibraries>
                        <!-- <artifact>com.lazerycode.junit:junit-test:1.0.0</artifact>-->
                    </junitLibraries>
                    <!-- Specify the path of the test case -->
                    <testFilesDirectory>src\test\jmeter\</testFilesDirectory>
                </configuration>
                <executions>
                    <!-- Generate JMeter configuration -->
                    <execution>
                        <id>configuration</id>
                        <goals>
                            <goal>configure</goal>
                        </goals>
                    </execution>
                    <!-- Run JMeter tests -->
                    <execution>
                        <id>jmeter-tests</id>
                        <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>
            </plugin>

        </plugins>
    </build>
</project>





All content after the properties field is the jmter-related configuration information added this time, which can be copied and used later. In addition, once you get this configuration content, you generally only need to modify the following content:


src\test\jmeter\

Execute
Execute through console command

mvn clean verify

Operation through editor

Executed through actions in LifeCycle in the editor.

If it is not the first time, you need to execute clean and then verify (for the first time, just execute verify directly)

Console log during execution:

D:\Software\java\jdk1.8.0_181\bin\java.exe "-Dmaven.multiModuleProjectDirectory=D:\Program Files\GitData\GritJmeter" -Dmaven.home=D:\Software\apache-maven- 3.9.1 -Dclassworlds.conf=D:\Software\apache-maven-3.9.1\bin\m2.conf -javaagent:D:\Software\ideaIU-2018.3.6.win\lib\idea_rt.jar=17454: D:\Software\ideaIU-2018.3.6.win\bin -Dfile.encoding=UTF-8 -classpath D:\Software\apache-maven-3.9.1\boot\plexus-classworlds-2.6.0.jar;D :\Software\apache-maven-3.9.1\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version=2018.3.6 verify
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------< Grit:GritJmeter >---------------- ----------
[INFO] Building GritJmeter 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO]--------------------------------[ jar ]--------------------- --------------------------
[INFO]
[INFO] --- resources:3.3.0:resources (default-resources) @ GritJmeter ---
[INFO] Copying 0 resources
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ GritJmeter ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- jmeter:3.6.0:configure (configuration) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------- --------
[INFO] C O N F I G U R I N G J M E T E R
[INFO] -------------------------------------------------- --------
[INFO]
[INFO] Creating test configuration for execution ID: configuration
[INFO] Building JMeter directory structure...
[INFO] Generating JSON Test config...
[INFO] Configuring JMeter artifacts...
[INFO] Populating JMeter directory...
[INFO] Copying extensions to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib\ext
Downloading dependencies: true
[INFO] Copying junit libraries to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib\junit
Downloading dependencies: true
[INFO] Copying test plan libraries to D:\Program Files\GitData\GritJmeter\target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter\lib
Downloading dependencies: true
[INFO] Configuring JMeter properties...
[INFO]
[INFO] --- resources:3.3.0:testResources (default-testResources) @ GritJmeter ---
[INFO] Copying 6 resources
[INFO]
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ GritJmeter ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.0.0:test (default-test) @ GritJmeter ---
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ GritJmeter ---
[INFO] Building jar: D:\Program Files\GitData\GritJmeter\target\GritJmeter-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- jmeter:3.6.0:jmeter (jmeter-tests) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------- --------
[INFO] P E R F O R M A N C E T E S T S
[INFO] -------------------------------------------------- --------
[INFO]
[INFO] Will generate HTML report in D:\Program Files\GitData\GritJmeter\target\jmeter\reports\cont
[INFO] Executing test: cont.jmx
[INFO] Arguments for forked JMeter JVM: [java, -Xms2048M, -Xmx2048M, -Djava.awt.headless=true, -jar, ApacheJMeter-5.4.3.jar, -d, D:\Program Files\GitData\GritJmeter \target\39016a4e-390a-4b18-8df3-fb914d5aa91f\jmeter, -e, -j, D:\Program Files\GitData\GritJmeter\target\jmeter\logs\cont.jmx.log, -l, D:\Program Files\GitData\GritJmeter\target\jmeter\results\cont-20231017.csv, -n, -o, D:\Program Files\GitData\GritJmeter\target\jmeter\reports\cont, -t, D:\Program Files \GitData\GritJmeter\target\jmeter\testFiles\cont.jmx, -L, ERROR, -Dsun.net.http.allowRestrictedHeaders, true]
[INFO]
[INFO] SLF4J: Class path contains multiple SLF4J bindings.
[INFO] SLF4J: Found binding in [jar:file:/D:/Program Files/GitData/GritJmeter/target/39016a4e-390a-4b18-8df3-fb914d5aa91f/jmeter/lib/log4j-slf4j-impl-2.17.0. jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: Found binding in [jar:file:/D:/Program Files/GitData/GritJmeter/target/39016a4e-390a-4b18-8df3-fb914d5aa91f/jmeter/lib/slf4j-nop-1.7.10.jar! /org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[INFO] SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
[INFO] Creating summariser <summary>
[INFO] Created the tree successfully using D:\Program Files\GitData\GritJmeter\target\jmeter\testFiles\cont.jmx
[INFO] Starting standalone test @ Tue Oct 17 11:19:57 CST 2023 (1697512797293)
[INFO] Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 4445
[INFO] summary + 1 in 00:00:04 = 0.3/s Avg: 3033 Min: 3033 Max: 3033 Err: 0 (0.00%) Active: 1 Started: 1 Finished: 0
[INFO] summary + 13 in 00:00:07 = 1.9/s Avg: 529 Min: 9 Max: 5276 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
[INFO] summary = 14 in 00:00:10 = 1.3/s Avg: 708 Min: 9 Max: 5276 Err: 0 (0.00%)
[INFO] Tidying up... @ Tue Oct 17 11:20:08 CST 2023 (1697512808570)
[INFO] ...end of run
[INFO] Completed Test: D:\Program Files\GitData\GritJmeter\target\jmeter\testFiles\cont.jmx
[INFO]
[INFO]
[INFO] --- jmeter:3.6.0:results (jmeter-check-results) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------- --------
[INFO] S C A N N I N G F O R R E S U L T S
[INFO] -------------------------------------------------- --------
[INFO]
[INFO] Will scan results using format: CSV
[INFO]
[INFO] Parsing results file 'D:\Program Files\GitData\GritJmeter\target\jmeter\results\cont-20231017.csv' as type: CSV
[INFO]
[INFO] -------------------------------------------------- --------
[INFO] P E R F O R M A N C E T E S T R E S U L T S
[INFO] -------------------------------------------------- --------
[INFO]
[INFO] Result (.csv) files scanned: 1
[INFO] Successful requests: 16
[INFO] Failed requests: 0
[INFO] Failures: 0.0% (0.0% accepted)
[INFO]
[INFO] -------------------------------------------------- --------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------- --------------------------
[INFO] Total time: 57.681 s
[INFO] Finished at: 2023-10-17T11:20:12 + 08:00
[INFO] -------------------------------------------------- --------------------------
[INFO] Shutdown detected, destroying JMeter process...
[INFO]

Process finished with exit code 0
via jmeter gui

cd to the maven project code directory and execute the command

mvn jmeter:configure jmeter:gui

The jmeter interface will automatically open

console log

Looking at the log, we can see that the configuration obtained during execution is the default configuration execution ID: default-cli, instead of the execution ID: configuration we configure in the pom.

Microsoft Windows [Version 10.0.17763.4974]
(c) 2018 Microsoft Corporation. all rights reserved.

D:\Program Files\GitData\GritJmeter>mvn jmeter:configure jmeter:gui
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------< Grit:GritJmeter >---------------- ----------
[INFO] Building GritJmeter 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO]--------------------------------[ jar ]--------------------- --------------------------
[INFO]
[INFO] --- jmeter:3.6.0:configure (default-cli) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------- --------
[INFO] C O N F I G U R I N G J M E T E R
[INFO] -------------------------------------------------- --------
[INFO]
[INFO] Creating test configuration for execution ID: default-cli
[INFO] Building JMeter directory structure...
[INFO] Generating JSON Test config...
[INFO] Configuring JMeter artifacts...
[INFO] Populating JMeter directory...
[INFO] Copying extensions to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib\ext
Downloading dependencies: true
[INFO] Copying junit libraries to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib\junit
Downloading dependencies: true
[INFO] Copying test plan libraries to D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb-b55c-13626e70662f\jmeter\lib
Downloading dependencies: true
[INFO] Configuring JMeter properties...
[INFO]
[INFO] --- jmeter:3.6.0:gui (default-cli) @ GritJmeter ---
[INFO]
[INFO] -------------------------------------------------- --------
[INFO] S T A R T I N G J M E T E R G U I
[INFO] -------------------------------------------------- --------
[INFO] Arguments for forked JMeter JVM: [java, -Xms2048M, -Xmx2048M, -jar, ApacheJMeter-5.4.3.jar, -d, D:\Program Files\GitData\GritJmeter\target\caab0619-1b6c-4fcb- b55c-13626e70662f\jmeter, -L, ERROR]
[INFO]

This method executes slower

View report

After the execution is completed, there will be a target directory

Executed through Jenkins
Install Jenkins plug-in

HTML Publisher plugin

Click available plugins on the left, query HTML, select HTML Publisher plugin, and install it.

In the same way, install Performance Plugin and Maven Integration plugin

Configuring Jenkins running tools

Configure maven

Configure jdk

Configure maven

Create executable job

Enter the task name and choose to build a maven project

Select jdk (in the step of configuring the Jenkins running tool, the system-level jdk has already been configured at that time. If you configure multiple ones, you can choose one by yourself. If not, the system-level jdk will be defaulted)

If it is a performance test, you need to add Publish Performance test result report

Click to execute

Run successfully

View report

Problem Record

Question 1:

The testFilesDirectory element value can only be a directory, not a file, otherwise it will prompt that the found type is not a directory type and an error will be reported.

Question 2: pom.xml error message: Element ‘configuration’ cannot have character [children], because the type’s content type is element

Solution: It is found that there is a < in the content of the pom file.

There can only be element nodes under the beans node in the configuration file, and no characters or text can exist.

For example, extra punctuation marks, dots, and possibly spaces.

Question 3: The execution result shows that jmeter-maven-plugin version 3.6 requires maven version 3.5.2

solution:

Question 4: The report viewed on Jenkins is empty

Solution: Jenkins home page – click Manage Jenkins – select Script console

enter

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

Click to run, and then rebuild to view the HTML report.

refer to:

Win10 system Jmeter + maven + Jenkins interface automation environment construction (1) | Conan’s blog records