《解读 JMeter JMX 脚本》
1 简介
.jmx 是 JMeter 的测试计划文件,本质是 XML。理解其中关键元素节点,可以直接手写或修改脚本,而不必全部依赖 GUI。
2 关键节点含义
2.1 TestPlan / ThreadGroup
| 节点 | 含义 |
|---|---|
<TestPlan testname="..."> |
测试计划的名字 |
<ThreadGroup testname="..."> |
线程组名字 |
<stringProp name="ThreadGroup.num_threads"> |
并发数 |
<stringProp name="ThreadGroup.duration"> |
持续时间(配合 scheduler) |
<intProp name="LoopController.loops"> |
循环次数 |
请求总数 = 并发数 × 循环次数。
2.2 HTTPSampler(HTTP 请求)
| 节点 | 含义 |
|---|---|
<stringProp name="HTTPSampler.domain"> |
域名 |
<stringProp name="HTTPSampler.port"> |
端口 |
<stringProp name="HTTPSampler.protocol"> |
协议 |
<stringProp name="HTTPSampler.path"> |
路径 |
<stringProp name="HTTPSampler.method"> |
方法(GET/POST) |
<boolProp name="HTTPSampler.use_keepalive"> |
是否使用长连接 |
<stringProp name="HTTPSampler.connect_timeout"> |
连接超时 |
<stringProp name="HTTPSampler.response_timeout"> |
响应超时 |
2.3 定时/头信息
| 节点 | 含义 |
|---|---|
<stringProp name="throughput"> |
Constant Throughput Timer 每分钟请求数(如 2400 即 QPS=40) |
<intProp name="calcMode"> |
计算模式,1 表示基于所有活跃线程(all active threads) |
<stringProp name="Header.name"> / Header.value |
请求头的名字与值,如 x-cf-trace=1 |
示例中 throughput=2400、calcMode=1,即所有活跃线程合计每分钟 2400 次请求,折合 QPS 40。
3 典型 JMX 骨架
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="3.2" jmeter="3.3 r1808647">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="hello-service-load" enabled="true">
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="hello-load-test" enabled="true">
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" testclass="LoopController">
<intProp name="LoopController.loops">-1</intProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">20</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">true</boolProp>
<stringProp name="ThreadGroup.duration">300</stringProp>
</ThreadGroup>
<hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
<stringProp name="HTTPSampler.domain">example.com</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.path">/api/hello</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
</HTTPSamplerProxy>
<hashTree/>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="" elementType="Header">
<stringProp name="Header.name">x-request-id</stringProp>
<stringProp name="Header.value">1</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<hashTree/>
</hashTree>
</hashTree>
<WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="WorkBench" enabled="true">
<boolProp name="WorkBench.save">true</boolProp>
</WorkBench>
<hashTree/>
</hashTree>
</jmeterTestPlan>
4 常见问题
4.1 URI 中多个参数被转义为 &
如果请求 URI 含多个参数,如 image?session=123&pub=456,JMeter 会把 & 写成 XML 实体 &:
<stringProp name="HTTPSampler.path">image?session=123&pub=456</stringProp>
这是 XML 的合法写法,无需手工改回。
阅读 —
·
全站 —