通过案例带你轻松玩转JMeter连载(50)

2022-02-10 15:54:43 浏览数 (1)

JMeter的运行 JMeter可以在GUI下运行也可以在非GUI下运行。由于在GUI下会占用一定的压测机器的资源,所以JMeter官方不支持在GUI下面运行,打开JMeter.bat,在命令行中会显示如图1内容。

图1 JMeter启动界面

代码语言:javascript复制
Don't use GUI mode for load testing !, only for Test creation and Test debugging.
For load testing, use CLI Mode (was NON GUI):
jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder]
& increase Java Heap to meet your test requirements:
 Modify current env variable HEAP="-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m" in the jmeter batch file
 Check : https://jmeter.apache.org/usermanual/best-practices.html

在非GUI模式下,运行JMeter通常有以下三种方法,分别是“通过JMeter自带工具运行”“通过Apache Ant运行”和“通过Taurus运行”,下面一一来进行介绍。

1通过JMeter自带工具运行 通过JMeter自带工具运行即图1,所建议的方法,其格式如下。

代码语言:javascript复制
jmeter -n -t jmx文件名 -l jtl格式记录文件

比如:

代码语言:javascript复制
jmeter -n -t ebusiness.jmx -l logfile1.jtl

这个命令中最基本的参数介绍如下。

  • -h 帮助:打印出有用的信息并退出。
  • -n 非 GUI 模式【必选】:在非 GUI 模式下运行 JMeter。
  • -t 测试文件要运行的 JMeter 测试脚本文件【必选】。
  • -l 日志文件记录结果的文件【必选】。
  • -r 远程执行:在Jmter.properties文件中指定的所有远程服务器。
  • -H 代理主机:设置 JMeter 使用的代理主机。
  • -P 代理端口:设置 JMeter 使用的代理主机的端口号。

运行结果如下。

代码语言:javascript复制
D:…CLI>jmeter -n -t ebusiness.jmx -l logfile1.jtl
Jun 07, 2021 5:38:23 PM java.util.prefs.WindowsPreferences <init>

WARNING: Could not open/create prefs root node SoftwareJavaSoftPrefs at root 
0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

Creating summariser <summary>

Created the tree successfully using ebusiness.jmx

Starting the test @ Mon Jun 07 17:38:24 CST 2021 (1623058704060)

Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message on port 
4445

summary        1 in 00:00:03 =    0.3/s Avg:    17 Min:    17 Max:    17 Err:     
0 (0.00%) Active: 2 Started: 2 Finished: 0

summary     1792 in 00:00:29 =   61.2/s Avg:   300 Min:     2 Max:  7292 Err:    
19 (1.06%) Active: 20 Started: 20 Finished: 0

summary =   1793 in 00:00:32 =   55.3/s Avg:   300 Min:     2 Max:  7292 Err:    
19 (1.06%)

summary     1877 in 00:00:30 =   62.6/s Avg:   322 Min:     2 Max:  6837 Err:    
15 (0.80%) Active: 20 Started: 20 Finished: 0

summary =   3670 in 00:01:02 =   58.8/s Avg:   312 Min:     2 Max:  7292 Err:    
34 (0.93%)

summary     1663 in 00:00:30 =   55.2/s Avg:   373 Min:     3 Max:  6995 Err:    
16 (0.96%) Active: 20 Started: 20 Finished: 0

summary =   5333 in 00:01:33 =   57.6/s Avg:   331 Min:     2 Max:  7292 Err:    
50 (0.94%)

…

运行产生的logfile1.jtl可以通过JMeter的GUI界面加载到报告中。

2通过Apache Ant运行 Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。由Apache软件基金会所提供。由于JMeter是一个由Java开发的软件,因此给Apache Ant的运行提供了可能。下面为配置Apache Ant运行JMeter的方法。

1)安装并且配置Apache Ant 2)修改%JMETER_HOME%binjmeter.properties

代码语言:javascript复制
jmeter.save.saveservice.output_format=xml

配置build.xml

代码语言:javascript复制
    <?xml version="1.0" encoding="UTF-8"?>
<project name="ant-jmeter-test" default="run" basedir=".">

    <tstamp>

        <format property="time" pattern="yyyyMMddhhmm" />

    </tstamp>

    <!-- 需要改成自己本地的 Jmeter 目录--> 

    <property name="jmeter.home" value="C:apacheapache-jmeter-5.4.1" />

    <!-- jmeter生成jtl格式的结果报告的路径-->

    <property name="jmeter.result.jtl.dir" 
value="C:apacheapache-jmeter-5.4.1bintestreportjtl" />

    <!-- jmeter生成html格式的结果报告的路径-->

    <property name="jmeter.result.html.dir" 
value="C:apacheapache-jmeter-5.4.1bintestreporthtml" />

    <!-- 生成的报告的前缀--> 

    <property name="ReportName" value="TestReport" />

    <property name="jmeter.result.jtlName" 
value="${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" />

    <property name="jmeter.result.htmlName" 
value="${jmeter.result.html.dir}/${ReportName}.html" />

    

    <target name="run">

        <antcall target="test" />

        <antcall target="report" />

    </target>

    

    <target name="test">

        <taskdef name="jmeter" 
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />

        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">

             <!-- 声明要运行的脚本。"*.jmx"指包含此目录下的所有jmeter脚本-->

            <testplans dir="C:apacheapache-jmeter-5.4.1scripts" includes="*.jmx" 
/>

 

             <property name="jmeter.save.saveservice.output_format" value="xml"/>

        </jmeter>

    </target>

    

     <path >

        <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>

        <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>

     </path>

        

    <target name="report">

        <tstamp> <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" 
/></tstamp>    

        <xslt               

              classpat 

              force="true"

              

              in="${jmeter.result.jtlName}"

              out="${jmeter.result.htmlName}"

              style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" />  

        

        <!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 -->

        <copy todir="${jmeter.result.html.dir}">

            <fileset dir="${jmeter.home}/extras">

                <include name="collapse.png" />

                <include name="expand.png" />

            </fileset>

        </copy>

    </target>    

    

</project>

其中 “C:apacheapache-jmeter-5.4.1”为JMETER 的安装目录,即%JMETER_HOME%。 “C:apacheapache-jmeter-5.4.1scripts”为jmx文件所在位置 运行Ant

代码语言:javascript复制
C:apacheapache-jmeter-5.4.1scripts>ant
Buildfile: C:apacheapache-jmeter-5.4.1scriptsbuild.xml



run:



test:

   [jmeter] Executing test plan: C:apacheapache-jmeter-5.4.1scriptslogin.jmx ==> 
C:apacheapache-jmeter-5.4.1bintestreportjtlTestReport202108190413.jtl

   [jmeter] 八月 19, 2021 4:13:42 下午 java.util.prefs.WindowsPreferences <init>

   [jmeter] 警告: Could not open/create prefs root node SoftwareJavaSoftPrefs at root 
0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

   [jmeter] Creating summariser <summary>

   [jmeter] Created the tree successfully using 
C:apacheapache-jmeter-5.4.1scriptslogin.jmx

   [jmeter] Starting standalone test @ Thu Aug 19 16:13:53 CST 2021 (1629360833026)

   [jmeter] Waiting for possible Shutdown/StopTestNow/HeapDump/ThreadDump message 
on port 4447

   [jmeter] summary      152 in 00:00:06 =   27.4/s Avg:    31 Min:    11 Max:   255 
Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0

   [jmeter] summary     1215 in 00:00:30 =   40.6/s Avg:    23 Min:    10 Max:   347 
Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0

   [jmeter] summary =   1367 in 00:00:35 =   38.6/s Avg:    24 Min:    10 Max:   347 
Err:     0 (0.00%)

   [jmeter] summary     1203 in 00:00:25 =   48.7/s Avg:    20 Min:     9 Max:    62 
Err:     0 (0.00%) Active: 0 Started: 1 Finished: 1

   [jmeter] summary =   2570 in 00:01:00 =   42.7/s Avg:    22 Min:     9 Max:   347 
Err:     0 (0.00%)

   [jmeter] Tidying up ...    @ Thu Aug 19 16:14:54 CST 2021 (1629360894732)

   [jmeter] ... end of run



report:

     [xslt] Processing 
C:apacheapache-jmeter-5.4.1bintestreportjtlTestReport202108190413.jtl to 
C:apacheapache-jmeter-5.4.1bintestreporthtmlTestReport.html

     [xslt] Loading stylesheet 
C:apacheapache-jmeter-5.4.1extrasjmeter-results-detail-report_21.xsl

     [copy] Copying 2 files to C:apacheapache-jmeter-5.4.1bintestreporthtml



BUILD SUCCESSFUL

Total time: 1 minute 19 seconds



C:apacheapache-jmeter-5.4.1scripts>

根据配置。 C:apacheapache-jmeter-5.4.1bintestreportjtl 产生jtl格式的报告文件。 C:apacheapache-jmeter-5.4.1bintestreporthtml产生html格式的报告文件。如图2所示。

图2 Apache Ant运行JMeter HTML测试报告

3通过Taurus运行 Taurus工具是一个开源测试自动化框架,提供简单的基于YAML的配置格式。要通过Taurus运行,首先要配置好python环境。然后通过pip3 install bzt命令安装Taurus插件。接下来配置blaze_exist_jmeter_config.yml文件。

代码语言:javascript复制
execution: 
  - scenario: simple



scenarios: 

   simple: 

        script: ebusiness.jmx



modules: 

    jmeter: 

        download-link: 
https://mirrors.tuna.tsinghua.edu.cn/apache//jmeter/binaries/apache-jmeter-{version}.zip

        version: 5.2.1

其中ebusiness.jmx为将运行的jmx文件。 最后运行bzt blaze_exist_jmeter_config.yml即可。

代码语言:javascript复制
D:DOCUMENT培训与演讲培训软件性能测试JMeter ScriptTaurus>bzt 
blaze_exist_jmeter_config.yml
17:54:16 INFO: Taurus CLI Tool v1.14.2

17:54:16 INFO: Starting with configs: ['blaze_exist_jmeter_config.yml']

17:54:16 INFO: Configuring...

17:54:16 INFO: Artifacts dir: D:DOCUMENT培训与演讲培训软件性能测试JMeter 
ScriptTaurus2021-06-07_17-54-16.816972

17:54:16 INFO: Preparing...

17:54:18 WARNING: There is newer version of Taurus 1.15.3 available, consider 
upgrading. What's new: http://gettaurus.org/docs/Changelog/

17:54:21 INFO: 3 obsolete CookieManagers are found and fixed

17:54:27 INFO: Starting...

17:54:27 INFO: Waiting for results...

17:54:27 INFO: Did not mute console logging

17:54:28 INFO: Waiting for finish...

得到如图3报告图。

图3 Taurus运行JMeter测试界面

3 总结 本章学习了运行JMeter的三种命令很方式,分别是:通过JMeter自带工具运行、通过Apache Ant运行和通过Taurus运行。

0 人点赞