话说
各位读者盆友,早上好,这里接着总结下Jmeter的使用 。
目录
1.如何添加全局变量?
2.Jmeter生成的文档结构剖析
3.总结
开发工具:Jmeter
难度系数:★★☆☆☆
建议用时:1H
1.如何添加全局变量?
当建立一个ThreadsGroup之后,就会新建多个Samples,这多个Samples组成一个场景,比如:首页、视频列表等等,每个Samples一般都是HTTP请求,每个请求都需要去访问主机Ip,按照笔者博客Jmeter-1的做法,都要手动写,这个能否类似Java变量一样剥离出来,然后全局引用?
还有每次HTTP请求,都涉及权限问题,这个Token能够全局共享?而不用每个都携带?
都是可以的。主机Ip,我们通过Defined Variables实现;Token 我们通过Cookie实现。一图胜千言。
其他地方这么引用即可:
同理,Cookie主要是去携带Token,这个全局设置后,就不用管了,因为cookie就是全局的。
因为Token返回的格式,需要定义Content-Type,所以这里要定义 一下HTTP Header
寻找方法类似,这里直接看结果:
2.Jmeter生成的文档结构剖析
当你在Jmeter之中设置一个TestPlan之后,你的每个操作,背后都是用.jmx文档在记录?神马是.jmx格式?就是表示是Jmeter生成的文档,类似xml格式。
我们来看一个比较全面的.jmx文档,包含:全局variables 、Cookie、各种线程。
<jmeterTestPlan version="1.2" properties="4.0" jmeter="4.0 r1823414">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="dzj压测" enabled="true">
<stringProp name="TestPlan.comments">stringProp>
<boolProp name="TestPlan.functional_mode">falseboolProp>
<boolProp name="TestPlan.serialize_threadgroups">falseboolProp>
<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>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="分中心详情线程组" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continuestringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">falseboolProp>
<stringProp name="LoopController.loops">5stringProp>
elementProp>
<stringProp name="ThreadGroup.num_threads">1stringProp>
<stringProp name="ThreadGroup.ramp_time">1stringProp>
<boolProp name="ThreadGroup.scheduler">falseboolProp>
<stringProp name="ThreadGroup.duration">stringProp>
<stringProp name="ThreadGroup.delay">stringProp>
ThreadGroup>
<hashTree>
<Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="host" elementType="Argument">
<stringProp name="Argument.name">hoststringProp>
<stringProp name="Argument.value">za1sde3.duanjia.netstringProp>
<stringProp name="Argument.metadata">=stringProp>
elementProp>
collectionProp>
Arguments>
<hashTree/>
<CookieManager guiclass="CookiePanel" testclass="CookieManager" testname="HTTP Cookie Manager" enabled="true">
<collectionProp name="CookieManager.cookies">
<elementProp name="dzj_token" elementType="Cookie" testname="dzj_token">
<stringProp name="Cookie.value">eyJraWQiOiJSU0ExIiwiYWxnIjoiUlMyNTYifQ.eyJhdF9oYXNoIjoiVkZBSWE3OEVCZUZLUWJ4eGM3b2tIQSIsInN1YiI6IjAwYTY0NzJkLWFkMjYtNDEzYy1iY2I4LWI4MTQyZThmMGM3YiIsImF1ZCI6IlZ0ZVVWUGRvUDFQTUFGM3EiLCJjb2RlIjoiamlhd2VpIiwicHJvdmluY2VfaWQiOjExMDAwMCwicm9sZXMiOlsiT1BFUkFUT1IiLCJTVVBFUl9PUEVSQVRPUiIsIk1TTCIsIkVOVEVSUFJJU0wstringProp>
<stringProp name="Cookie.domain">${host}stringProp>
<stringProp name="Cookie.path">stringProp>
<boolProp name="Cookie.secure">falseboolProp>
<longProp name="Cookie.expires">0longProp>
<boolProp name="Cookie.path_specified">trueboolProp>
<boolProp name="Cookie.domain_specified">trueboolProp>
elementProp>
collectionProp>
<boolProp name="CookieManager.clearEachIteration">falseboolProp>
CookieManager>
<hashTree/>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="" elementType="Header">
<stringProp name="Header.name">Content-TypestringProp>
<stringProp name="Header.value">application/json;charset=UTF-8stringProp>
elementProp>
collectionProp>
HeaderManager>
<hashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="/idp/user/current" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="" elementType="HTTPArgument">
<boolProp name="HTTPArgument.always_encode">falseboolProp>
<stringProp name="Argument.value">stringProp>
<stringProp name="Argument.metadata">=stringProp>
<boolProp name="HTTPArgument.use_equals">trueboolProp>
elementProp>
collectionProp>
elementProp>
<stringProp name="HTTPSampler.domain">${host}stringProp>
<stringProp name="HTTPSampler.port">stringProp>
<stringProp name="HTTPSampler.protocol">stringProp>
<stringProp name="HTTPSampler.contentEncoding">stringProp>
<stringProp name="HTTPSampler.path">/idp/user/currentstringProp>
<stringProp name="HTTPSampler.method">GETstringProp>
<boolProp name="HTTPSampler.follow_redirects">trueboolProp>
<boolProp name="HTTPSampler.auto_redirects">falseboolProp>
<boolProp name="HTTPSampler.use_keepalive">trueboolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">falseboolProp>
<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="/bdc/branch_center_interaction/count" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
<collectionProp name="Arguments.arguments"/>
elementProp>
<stringProp name="HTTPSampler.domain">${host}stringProp>
<stringProp name="HTTPSampler.port">stringProp>
<stringProp name="HTTPSampler.protocol">stringProp>
<stringProp name="HTTPSampler.contentEncoding">stringProp>
<stringProp name="HTTPSampler.path">/bdc/branch_center_interaction/count?branchCenterId=99390121d77945e797a659c41f1f3295stringProp>
<stringProp name="HTTPSampler.method">GETstringProp>
<boolProp name="HTTPSampler.follow_redirects">trueboolProp>
<boolProp name="HTTPSampler.auto_redirects">falseboolProp>
<boolProp name="HTTPSampler.use_keepalive">trueboolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">falseboolProp>
<stringProp name="HTTPSampler.embedded_url_re">stringProp>
<stringProp name="HTTPSampler.connect_timeout">stringProp>
<stringProp name="HTTPSampler.response_timeout">stringProp>
HTTPSamplerProxy>
<hashTree/>
<ResultCollector guiclass="StatVisualizer" testclass="ResultCollector" testname="Aggregate Report" enabled="true">
<boolProp name="ResultCollector.error_logging">falseboolProp>
<objProp>
<name>saveConfigname>
<value class="SampleSaveConfiguration">
<time>truetime>
<latency>truelatency>
<timestamp>truetimestamp>
<success>truesuccess>
<label>truelabel>
<code>truecode>
<message>truemessage>
<threadName>truethreadName>
<dataType>truedataType>
<encoding>falseencoding>
<assertions>trueassertions>
<subresults>truesubresults>
<responseData>falseresponseData>
<samplerData>falsesamplerData>
<xml>falsexml>
<fieldNames>truefieldNames>
<responseHeaders>falseresponseHeaders>
<requestHeaders>falserequestHeaders>
<responseDataOnError>falseresponseDataOnError>
<saveAssertionResultsFailureMessage>truesaveAssertionResultsFailureMessage>
<assertionsResultsToSave>0assertionsResultsToSave>
<bytes>truebytes>
<sentBytes>truesentBytes>
<threadCounts>truethreadCounts>
<idleTime>trueidleTime>
<connectTime>trueconnectTime>
value>
objProp>
<stringProp name="filename">stringProp>
ResultCollector>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">falseboolProp>
<objProp>
<name>saveConfigname>
<value class="SampleSaveConfiguration">
<time>truetime>
<latency>truelatency>
<timestamp>truetimestamp>
<success>truesuccess>
<label>truelabel>
<code>truecode>
<message>truemessage>
<threadName>truethreadName>
<dataType>truedataType>
<encoding>falseencoding>
<assertions>trueassertions>
<subresults>truesubresults>
<responseData>falseresponseData>
<samplerData>falsesamplerData>
<xml>falsexml>
<fieldNames>truefieldNames>
<responseHeaders>falseresponseHeaders>
<requestHeaders>falserequestHeaders>
<responseDataOnError>falseresponseDataOnError>
<saveAssertionResultsFailureMessage>truesaveAssertionResultsFailureMessage>
<assertionsResultsToSave>0assertionsResultsToSave>
<bytes>truebytes>
<sentBytes>truesentBytes>
<threadCounts>truethreadCounts>
<idleTime>trueidleTime>
<connectTime>trueconnectTime>
value>
objProp>
<stringProp name="filename">stringProp>
ResultCollector>
<hashTree/>
hashTree>
hashTree>
hashTree>
jmeterTestPlan>
反正你就明白,这里面记录了所有你在Jmeter界面所进行的所有操作,关键元素总结如下:
TestPlan ——测试计划
ThreadsGroup——测试组
Arguments ——全局变量
CookieManager——Cookie值
HeaderManager ——Http头部信息携带
HTTPSamplerProxy ——HTTP请求
ResultCollector ——结果集
知道这些有卵用?NO!知道这些,你才会知道到时候在哪里可以修改参数,在哪里要把参数设置为property
3.总结
Jmeter-1和Jmeter-2都是最基础的应用,这周我们要搭建Jenkins+Jmeter+Ant组合的框架,实现各种参数可注入、场景可分离等重要功能,后续博客陆续跟上,让我们拭目以待吧。
下期再会!