Jmeter教程 简单的压力测试
Jmeter是一个非常好用的压力测试工具。 Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测试需要的脚本写好。
什么是压力测试
顾名思义:压力测试,就是 被测试的系统,在一定的访问压力下,看程序运行是否稳定/服务器运行是否稳定(资源占用情况)
比如: 2000个用户同时到一个购物网站购物,这些用户打开页面的速度是否会变慢,或者网站是否会奔溃
做压力测试的常用工具
做压力测试,一般要使用工具, 人工是没办法做的。 最常用的工具是LoadRunner, 但是LoadRunner毕竟是收费软件,而且使用上也比较复杂。 现在越来越多的人开始使用Jmeter来做压力测试。 免费, 而且使用上非常简单。
做压力测试的步骤如下:
1. 写脚本 或者录制脚本
2. 使用用户自定义参数
3. 场景设计
4. 使用控制器,来控制 模拟多少用户。
5. 使用监听器, 查看测试结果
本文做压力测试的例子
本文举的实例是: 在一台电脑用Jmeter模拟200个用户,同时去使用bing搜索不同的关键字, 查看页面返回的时间是否在正常范围内。
第一步: 使用CSV Data Set Config 来参数化
首先我们把测试需要用到的2个参数放在txt文件中,
新建一个data.txt文件,输入些数据, 一行有两个数据,用逗号分隔。
启动Jmeter, 先添加一个Thread Group, 然后添加一个CSV Data Set Config (Add -> Config Element -> CSV Data Set Config)
第二步:添加HTTP Request.
我们添加http 请求,发送get 到 http://cn.bing.com/search?q=博客园+小坦克
选择Thread Group 右键 (Add ->Sampler -> HTTP Request), 需要填的数据如下:
第三步: 使用Thread Group, 控制模拟多少用户
选中Thread Group
Number of Threads(users): 一个用户占一个线程, 200个线程就是模拟200个用户
Ramp-Up Period(in seconds): 设置线程需要多长时间全部启动。如果线程数为200 ,准备时长为10 ,那么需要1秒钟启动20个线程。也就是每秒钟启动20个线程。
Loop Count: 每个线程发送请求的次数。如果线程数为200 ,循环次数为10 ,那么每个线程发送10次请求。总请求数为200*10=2000 。如果勾选了“永远”,那么所有线程会一直发送请求,直到选择停止运行脚本。
第四步: 添加Summary Report 用来查看测试结果
选中Thread Group 右键(Add -> Listener -> Summary Report)
第五步: 运行一下
到目前为止, 脚本就全写好了, 我们来运行下, 如何看下测试的结果
附:
1.loadTest.jmx
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.6" jmeter="2.11 r1554548">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></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="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">10</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">200</stringProp>
<stringProp name="ThreadGroup.ramp_time">10</stringProp>
<longProp name="ThreadGroup.start_time">1415933614000</longProp>
<longProp name="ThreadGroup.end_time">1415933614000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
<stringProp name="delimiter">,</stringProp>
<stringProp name="fileEncoding">utf-8</stringProp>
<stringProp name="filename">c:\data.txt</stringProp>
<boolProp name="quotedData">false</boolProp>
<boolProp name="recycle">true</boolProp>
<stringProp name="shareMode">shareMode.all</stringProp>
<boolProp name="stopThread">false</boolProp>
<stringProp name="variableNames">boke,user</stringProp>
</CSVDataSet>
<hashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">cn.bing.com</stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path">/search?q=${boke}+${user}</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>
<boolProp name="HTTPSampler.monitor">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
</HTTPSamplerProxy>
<hashTree/>
<ResultCollector guiclass="SummaryReport" testclass="ResultCollector" testname="Summary Report" 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>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
2.data.txt
博客园,小坦克
博客园,Fish
博客园,Fiddler
博客园,HTTP