Apache JMeter压力测试快速入门

来源:互联网 发布:电脑淘宝怎么联系卖家 编辑:程序博客网 时间:2024/06/07 15:32

引言

前文《Apache JMeter功能测试快速入门》中,我们在bin目录下点击jmeter.bat启动JMeter时,目光如炬的你一定注意到了命令行窗口中的如下提示:
Don't use GUI mode for load testing, only for Test creation and Test debugging !
For load testing, use NON GUI Mode:
   jmeter -n -t [jmx file] -l [results file] -e -o [Path to output folder]
& adapt Java Heap to your test requirements:
   Modify HEAP="-Xms512m -Xmx512m" in the JMeter batch file

这段提示文字直译过来就是:
不要使用GUI模式进行负载测试,仅用于测试创建和测试调试!
对于负载测试,使用NON GUI模式:
jmeter -n -t [jmx文件] -l [结果文件] -e -o [输出文件夹的路径]
并根据您的测试需求调整Java堆:
在JMeter批处理文件中修改HEAP="-Xms512m -Xmx512m"

以此为指导思想,本回要讲述的主要内容便是JMeter压力测试的几个环节:
(1) 创建用于性能测试的测试计划
(2) 在GUI模式下运行测试计划
(3) 在NON GUI模式(命令行模式)运行测试计划

开始压力测试前,我们先来了解下相关的术语。

术语定义

并发用户数

并发用户数,指系统中同时进行业务操作的用户数,在性能测试工具中,一般通过开启多线程模拟虚拟用户。

TPS

TPS(Transaction Per Second), 即每秒事务数, 是衡量系统性能的一个非常重要的指标。

APDEX

APDEX (Application Performance Index),即性能指数,是用户对应用性能满意度的量化值。它提供了一个统一的测量和报告用户体验的方法,把最终用户的体验和应用性能作为一个完整的指标进行统一度量。
基于“响应性”,APDEX定义了3个用户满意度区间( OneAPM默认定义的T值为0.5秒):
S(满意 ):这样的响应时间让用户感到很愉快,响应时间少于T
T(容忍 ):慢了一点,但还可以接受,继续这一应用过程,响应时间T~4T(JMeter中为3T)
F(失望 ):太慢了,受不了了,用户决定放弃这个应用,响应时间超过4T(JMeter中为3T)

测试说明

测试环境:Win 10/Ubuntu 16.04 + JDK 1.8 +Apache JMeter 3.2
测试脚本:下载测试脚本demo_lt.jmx;或拷贝附录中的脚本代码,另存为demo_lt.jmx
注:
(1) 本测试脚本在前文《Apache JMeter功能测试快速入门》基础上略作修改。
(2) 在Windows下执行JMeter,当并发线程数设置较高时,会产生Address already in use的异常。推荐在Linux下以命令行模式执行测试脚本进行压力测试。
     为简单起见,本示例仍在Win 10下运行。

脚本配置

在Windows下打开apache-jmeter-3.2,双击bin子目录下的jmeter.bat(注意要有JAVA_HOME环境变量),打开demo_lt.jmx。

主要配置有:
(1) 线程组设置

图1 线程组设置

线程属性解释如下
线程数 : 一个线程模拟一个用户 ,50个线程就是模拟50个用户。
Ramp-Up Period: 设置线程需要多长时间全部启动。如果线程数为50,准备时长为5,那么需要平均每秒钟启动10个线程。
循环次数 : 每个线程每个Sampler请求执行的次数 。如果勾选了“永远”,那么所有线程会一直发送请求,直到选择停止运行。

(2) 定时器设置

图2 定时器设置

定时器在每个 Sampler(取样器)之前执行,我们此处设置的是高斯随机定时器,每个线程在请求前按随机时间停顿,上图表示暂停时间分布在300到500毫秒之间。
注:
因为现实中连续两次操作是有时间间隔的,此处设置定时器是为了模拟人连续操作的思考时间。

GUI模式运行

点击工具栏中的启动按钮,或按Ctrl+R,运行测试计划。在导航栏点击“Aggregate Graph”,结果如下图所示。

图3. Aggregate Graph

Aggregate Graph主要字段说明如下
Label: 定义的取样器名称
Samples: 表示这次测试中一共发出了多少个请求
Average: 请求的平均响应时间
Min: 请求的最小响应时间
Max: 请求的最大响应时间
Error%: 错误的请求的数量/请求的总数
Throughput:每秒完成的请求数
KB/Sec: 每秒从服务器端接收到的数据量

命令行运行

(1) 运行
打开命令行窗口,切换到JMeter的bin目录,执行如下命令:
jmeter.bat -n -t demo_lt.jmx -l demo_lt.jtl -e -o C:\lt
注:
此处指定结果文件为demo_lt.jmx,统计目录为C:\lt

(2) 查看运行结果
运行JMeter,在导航栏中点击“ 查看结果树”,主界面中点击“浏览”,选择生成的结果文件demo_lt.jtl,效果如下图所示:


图4 查看结果树

(3) 查看统计结果
打开目录C:\lt,点击该目录下的index.html,可以看到详细的统计结果。下面两张图分别截取了性能指标和综合统计信息部分。


图5 性能指标


图6 综合统计信息

附录

A.1 demo_lt.jmx

<?xml version="1.0" encoding="UTF-8"?><jmeterTestPlan version="1.2" properties="3.2" jmeter="3.2 r1790748">  <hashTree>    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="测试计划" enabled="true">      <stringProp name="TestPlan.comments">demo by weichen</stringProp>      <boolProp name="TestPlan.functional_mode">false</boolProp>      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">        <collectionProp name="Arguments.arguments"/>      </elementProp>      <stringProp name="TestPlan.user_define_classpath"></stringProp>    </TestPlan>    <hashTree>      <SetupThreadGroup guiclass="SetupThreadGroupGui" testclass="SetupThreadGroup" testname="线程组" enabled="true">        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">          <boolProp name="LoopController.continue_forever">false</boolProp>          <stringProp name="LoopController.loops">10</stringProp>        </elementProp>        <stringProp name="ThreadGroup.num_threads">50</stringProp>        <stringProp name="ThreadGroup.ramp_time">5</stringProp>        <longProp name="ThreadGroup.start_time">1501545600000</longProp>        <longProp name="ThreadGroup.end_time">1501545600000</longProp>        <boolProp name="ThreadGroup.scheduler">false</boolProp>        <stringProp name="ThreadGroup.duration"></stringProp>        <stringProp name="ThreadGroup.delay"></stringProp>        <stringProp name="TestPlan.comments">mu mu</stringProp>      </SetupThreadGroup>      <hashTree>        <CookieManager guiclass="CookiePanel" testclass="CookieManager" testname="HTTP Cookie 管理器" enabled="true">          <collectionProp name="CookieManager.cookies"/>          <boolProp name="CookieManager.clearEachIteration">false</boolProp>          <stringProp name="CookieManager.policy">rfc2109</stringProp>          <stringProp name="CookieManager.implementation">org.apache.jmeter.protocol.http.control.HC4CookieHandler</stringProp>        </CookieManager>        <hashTree/>        <HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP信息头管理器" enabled="true">          <collectionProp name="HeaderManager.headers">            <elementProp name="" elementType="Header">              <stringProp name="Header.name">apikey</stringProp>              <stringProp name="Header.value">89db1e77a8b94c4017f551c28082eb86</stringProp>            </elementProp>          </collectionProp>        </HeaderManager>        <hashTree/>        <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">          <collectionProp name="Arguments.arguments">            <elementProp name="server" elementType="Argument">              <stringProp name="Argument.name">server</stringProp>              <stringProp name="Argument.value">heweather</stringProp>              <stringProp name="Argument.metadata">=</stringProp>            </elementProp>          </collectionProp>        </Arguments>        <hashTree/>        <ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="服务器" enabled="true">          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">            <collectionProp name="Arguments.arguments"/>          </elementProp>          <stringProp name="HTTPSampler.domain">apis.baidu.com</stringProp>          <stringProp name="HTTPSampler.port"></stringProp>          <stringProp name="HTTPSampler.protocol">http</stringProp>          <stringProp name="HTTPSampler.contentEncoding"></stringProp>          <stringProp name="HTTPSampler.path"></stringProp>          <stringProp name="HTTPSampler.concurrentPool">4</stringProp>          <stringProp name="HTTPSampler.implementation">HttpClient4</stringProp>          <stringProp name="HTTPSampler.connect_timeout"></stringProp>          <stringProp name="HTTPSampler.response_timeout"></stringProp>        </ConfigTestElement>        <hashTree/>        <ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="服务器(测试环境)" enabled="false">          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">            <collectionProp name="Arguments.arguments"/>          </elementProp>          <stringProp name="HTTPSampler.domain">apis.baidu.com</stringProp>          <stringProp name="HTTPSampler.port"></stringProp>          <stringProp name="HTTPSampler.protocol">http</stringProp>          <stringProp name="HTTPSampler.contentEncoding"></stringProp>          <stringProp name="HTTPSampler.path"></stringProp>          <stringProp name="HTTPSampler.concurrentPool">4</stringProp>          <stringProp name="HTTPSampler.implementation">HttpClient4</stringProp>          <stringProp name="HTTPSampler.connect_timeout"></stringProp>          <stringProp name="HTTPSampler.response_timeout"></stringProp>        </ConfigTestElement>        <hashTree/>        <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="响应断言" enabled="true">          <collectionProp name="Asserion.test_strings">            <stringProp name="49586">200</stringProp>          </collectionProp>          <stringProp name="Assertion.test_field">Assertion.response_code</stringProp>          <boolProp name="Assertion.assume_success">false</boolProp>          <intProp name="Assertion.test_type">8</intProp>        </ResponseAssertion>        <hashTree/>        <GaussianRandomTimer guiclass="GaussianRandomTimerGui" testclass="GaussianRandomTimer" testname="高斯随机定时器" enabled="true">          <stringProp name="ConstantTimer.delay">500</stringProp>          <stringProp name="RandomTimer.range">300.0</stringProp>        </GaussianRandomTimer>        <hashTree/>        <GenericController guiclass="LogicControllerGui" testclass="GenericController" testname="百度APIStore天气预报接口" enabled="true"/>        <hashTree>          <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="国内景点天气预报" enabled="true">            <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">              <collectionProp name="Arguments.arguments">                <elementProp name="cityid" elementType="HTTPArgument">                  <boolProp name="HTTPArgument.always_encode">false</boolProp>                  <stringProp name="Argument.value">CN10101010018A</stringProp>                  <stringProp name="Argument.metadata">=</stringProp>                  <boolProp name="HTTPArgument.use_equals">true</boolProp>                  <stringProp name="Argument.name">cityid</stringProp>                </elementProp>              </collectionProp>            </elementProp>            <stringProp name="HTTPSampler.domain"></stringProp>            <stringProp name="HTTPSampler.port"></stringProp>            <stringProp name="HTTPSampler.protocol"></stringProp>            <stringProp name="HTTPSampler.contentEncoding"></stringProp>            <stringProp name="HTTPSampler.path">${server}/pro/attractions</stringProp>            <stringProp name="HTTPSampler.method">GET</stringProp>            <boolProp name="HTTPSampler.follow_redirects">true</boolProp>            <boolProp name="HTTPSampler.auto_redirects">false</boolProp>            <boolProp name="HTTPSampler.use_keepalive">true</boolProp>            <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>            <stringProp name="HTTPSampler.embedded_url_re"></stringProp>            <stringProp name="HTTPSampler.connect_timeout"></stringProp>            <stringProp name="HTTPSampler.response_timeout"></stringProp>          </HTTPSamplerProxy>          <hashTree/>          <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="城市天气高级数据" enabled="true">            <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">              <collectionProp name="Arguments.arguments">                <elementProp name="city" elementType="HTTPArgument">                  <boolProp name="HTTPArgument.always_encode">false</boolProp>                  <stringProp name="Argument.value">苏州</stringProp>                  <stringProp name="Argument.metadata">=</stringProp>                  <boolProp name="HTTPArgument.use_equals">true</boolProp>                  <stringProp name="Argument.name">city</stringProp>                </elementProp>              </collectionProp>            </elementProp>            <stringProp name="HTTPSampler.domain"></stringProp>            <stringProp name="HTTPSampler.port"></stringProp>            <stringProp name="HTTPSampler.protocol"></stringProp>            <stringProp name="HTTPSampler.contentEncoding"></stringProp>            <stringProp name="HTTPSampler.path">${server}/pro/weather</stringProp>            <stringProp name="HTTPSampler.method">GET</stringProp>            <boolProp name="HTTPSampler.follow_redirects">true</boolProp>            <boolProp name="HTTPSampler.auto_redirects">false</boolProp>            <boolProp name="HTTPSampler.use_keepalive">true</boolProp>            <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>            <stringProp name="HTTPSampler.embedded_url_re"></stringProp>            <stringProp name="HTTPSampler.connect_timeout"></stringProp>            <stringProp name="HTTPSampler.response_timeout"></stringProp>          </HTTPSamplerProxy>          <hashTree/>        </hashTree>        <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">          <boolProp name="ResultCollector.error_logging">false</boolProp>          <objProp>            <name>saveConfig</name>            <value class="SampleSaveConfiguration">              <time>true</time>              <latency>true</latency>              <timestamp>true</timestamp>              <success>true</success>              <label>true</label>              <code>true</code>              <message>true</message>              <threadName>true</threadName>              <dataType>true</dataType>              <encoding>false</encoding>              <assertions>true</assertions>              <subresults>true</subresults>              <responseData>false</responseData>              <samplerData>false</samplerData>              <xml>false</xml>              <fieldNames>false</fieldNames>              <responseHeaders>false</responseHeaders>              <requestHeaders>false</requestHeaders>              <responseDataOnError>false</responseDataOnError>              <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>              <assertionsResultsToSave>0</assertionsResultsToSave>              <bytes>true</bytes>              <threadCounts>true</threadCounts>            </value>          </objProp>          <stringProp name="filename"></stringProp>        </ResultCollector>        <hashTree/>        <ResultCollector guiclass="StatGraphVisualizer" testclass="ResultCollector" testname="Aggregate Graph" enabled="true">          <boolProp name="ResultCollector.error_logging">false</boolProp>          <objProp>            <name>saveConfig</name>            <value class="SampleSaveConfiguration">              <time>true</time>              <latency>true</latency>              <timestamp>true</timestamp>              <success>true</success>              <label>true</label>              <code>true</code>              <message>true</message>              <threadName>true</threadName>              <dataType>true</dataType>              <encoding>false</encoding>              <assertions>true</assertions>              <subresults>true</subresults>              <responseData>false</responseData>              <samplerData>false</samplerData>              <xml>false</xml>              <fieldNames>false</fieldNames>              <responseHeaders>false</responseHeaders>              <requestHeaders>false</requestHeaders>              <responseDataOnError>false</responseDataOnError>              <saveAssertionResultsFailureMessage>false</saveAssertionResultsFailureMessage>              <assertionsResultsToSave>0</assertionsResultsToSave>              <bytes>true</bytes>              <threadCounts>true</threadCounts>            </value>          </objProp>          <stringProp name="filename"></stringProp>        </ResultCollector>        <hashTree/>      </hashTree>    </hashTree>    <WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="工作台" enabled="true">      <boolProp name="WorkBench.save">true</boolProp>    </WorkBench>    <hashTree/>  </hashTree></jmeterTestPlan>

原创粉丝点击