本文参照地址:https://blog.csdn.net/lujinquan90/article/details/92988908
一、创建maven工程
1、电脑已安装好idea 且已搭建号idea 运行环境。
2、打开idea 右键new一个maven工程
3、设置maven项目 maven home路径和工作仓库地址信息。
备注:在cmd下 ,输入mvn -v ,可查看maven的home地址
二、修改maven项目目录结构及其相关配置 (jmeter和maven项目关联)
1、在src/test目录下新建一个jmeter的目录,用来存放jmeter脚本、配置文件、参数化文件。
1.1:将需要运行的jmeter生成的jmx文件放置在maven工程src/test/jmeter目录下。
1.2:将jmeter bin目录下的配置文件 拷贝至maven工程src/test/jmeter目录下
1.3 :修改jmeter.properties 文件中的属性值修改地位比较多 (也可以修改,将这些内容配置在pom.xml,放在<configuration>节点下)
(1)jmeter.save.saveservice.output_format=xml 大概在配置文件414行处
(2)修改一下内容, 大概在配置文件423行处
jmeter.save.saveservice.data_type=true
jmeter.save.saveservice.label=true
jmeter.save.saveservice.response_code=true
# response_data is not currently supported for CSV output
jmeter.save.saveservice.response_data=true
# Save ResponseData for failed samples
jmeter.save.saveservice.response_data.on_error=false
jmeter.save.saveservice.response_message=true
jmeter.save.saveservice.successful=true
jmeter.save.saveservice.thread_name=true
jmeter.save.saveservice.time=true
jmeter.save.saveservice.subresults=true
jmeter.save.saveservice.assertions=true
jmeter.save.saveservice.latency=true
# Only available with HttpClient4
jmeter.save.saveservice.connect_time=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.responseHeaders=true
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.encoding=false
jmeter.save.saveservice.bytes=true
# Only available with HttpClient4
jmeter.save.saveservice.sent_bytes=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.filename=true
jmeter.save.saveservice.hostname=true
jmeter.save.saveservice.thread_counts=true
jmeter.save.saveservice.sample_count=true
jmeter.save.saveservice.idle_time=true
2、在src/test目录下新建一个resources的目录,并将它在项目属性中设置为Test resource用来存放资源文件,比如XSL样式表文件jmeter.results.shanhe.me.xsl、jmeter-results-detail-report.xsl、jmeter-results-detail-report_21.xsl、jmeter-results-report.xsl、jmeter-results-report_21.xsl。(备注设置Test resource可以在界面按Ctrl+Alt+shift+s)
三、修改pom.xml文件中的配置,该文件是maven的引入jar包、插件的重要配置文件。
<?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>jmeterScript</groupId> <artifactId>autotest</artifactId> <version>1.0-SNAPSHOT</version> <!--设置报告生成的路径--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jmeter.result.jtl.dir>${project.build.directory}jmeter esults</jmeter.result.jtl.dir> <jmeter.result.html.dir>${project.build.directory}jmeterhtml</jmeter.result.html.dir> </properties> <build> <plugins> <plugin> <!-- 核心插件,用来执行jmx脚本,版本号对应的jmeter版本可在此地址查询 https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/blob/master/CHANGELOG.md--> <groupId>com.lazerycode.jmeter</groupId> <artifactId>jmeter-maven-plugin</artifactId> <version>2.2.0</version> <configuration> <!-- 设置jmeter生成结果文件格式--> <resultsFileFormat>xml</resultsFileFormat> <!-- 设置忽略失败是否停止运行--> <ignoreResultFailures>true</ignoreResultFailures> <!--设置结果是否有时间戳--> <testResultsTimestamp>false</testResultsTimestamp> <testFilesIncluded> <!-- //指定运行的jmeter脚本 --> <jMeterTestFile>hhbwg.jmx</jMeterTestFile> </testFilesIncluded> <!-- 指定jtl生成目录 --> <resultsDirectory>${jmeter.result.jtl.dir}</resultsDirectory> </configuration> <executions> <execution> <id>jmeter-tests</id> <phase>verify</phase> <!--脚本所在的文件夹 --> <goals> <goal>jmeter</goal> </goals> </execution> </executions> </plugin> <plugin> <!--根据xsl模版把jtl文件转换成html--> <groupId>org.codehaus.mojo</groupId> <artifactId>xml-maven-plugin</artifactId> <version>1.0-beta-3</version> <executions> <execution> <phase>verify</phase> <goals> <goal>transform</goal> </goals> </execution> </executions> <configuration> <transformationSets> <transformationSet> <dir>${jmeter.result.jtl.dir}</dir> <stylesheet>src est esourcesjmeter-results-detail-report_21.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> </plugins> </build> </project>
四、Run->Edit Configurations ,Goals里填入verify (注意verify 在 Jenkins Build中
Goals and options |
需要使用
五、启动程序 (如出现以下信息则说明环境正常,运行结束后target目录)
截图中存在一个Warning: Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor 警告,因为和提供的XSL样式表文件版本有关系
6、打开本地项目路径,/autotest/target/jmeter/html/hhbwg.html 用浏览器打开html 文件。
此时html文件图片未成功加载,解决图片加载问题。
到jmeter安装目录的extras文件夹中,找到collapse.png、expand.png文件,复制到autotest argetjmeterhtml1的文件夹中,然后刷新html报表。
此时已完成搭建好,如果你需要删除不必要的脚本,因为maven项目时会把src/test/jmeter下的测试脚本复制到target/jmeter/testFiles路径下,这2个路径下的脚本都要删除
备注:如果需要直接在pom.xml中配置jmeter的属性,可以使用一下xml内容,将内容放置在放在<configuration>节点下。
<propertiesJMeter> <CookieManager.save.cookies>true</CookieManager.save.cookies> <overrideRootLogLevel>debug</overrideRootLogLevel> <sampleresult.default.encoding>utf-8</sampleresult.default.encoding> <jmeter.save.saveservice.data_type>true</jmeter.save.saveservice.data_type> <jmeter.save.saveservice.label>true</jmeter.save.saveservice.label> <jmeter.save.saveservice.response_code>true</jmeter.save.saveservice.response_code> <jmeter.save.saveservice.response_data>true</jmeter.save.saveservice.response_data> <jmeter.save.saveservice.response_data.on_error>true</jmeter.save.saveservice.response_data.on_error> <jmeter.save.saveservice.response_message>true</jmeter.save.saveservice.response_message> <jmeter.save.saveservice.successful>true</jmeter.save.saveservice.successful> <jmeter.save.saveservice.thread_name>true</jmeter.save.saveservice.thread_name> <jmeter.save.saveservice.time>true</jmeter.save.saveservice.time> <jmeter.save.saveservice.subresults>true</jmeter.save.saveservice.subresults> <jmeter.save.saveservice.assertions>true</jmeter.save.saveservice.assertions> <!--对jmeter.properties的属性值进行部分修改--> <jmeter.save.saveservice.latency>true</jmeter.save.saveservice.latency> <jmeter.save.saveservice.connect_time>true</jmeter.save.saveservice.connect_time> <jmeter.save.saveservice.samplerData>true</jmeter.save.saveservice.samplerData> <jmeter.save.saveservice.responseHeaders>true</jmeter.save.saveservice.responseHeaders> <jmeter.save.saveservice.requestHeaders>true</jmeter.save.saveservice.requestHeaders> <jmeter.save.saveservice.encoding>true</jmeter.save.saveservice.encoding> <jmeter.save.saveservice.bytes>true</jmeter.save.saveservice.bytes> <jmeter.save.saveservice.sent_bytes>true</jmeter.save.saveservice.sent_bytes> <jmeter.save.saveservice.url>true</jmeter.save.saveservice.url> <jmeter.save.saveservice.filename>true</jmeter.save.saveservice.filename> <jmeter.save.saveservice.hostname>true</jmeter.save.saveservice.hostname> <jmeter.save.saveservice.thread_counts>=true</jmeter.save.saveservice.thread_counts> <jmeter.save.saveservice.sample_count>true</jmeter.save.saveservice.sample_count> <jmeter.save.saveservice.idle_time>true</jmeter.save.saveservice.idle_time> </propertiesJMeter>