我们在工作中做自动化测试平台开发时,会对jmeter进行二次开发,使它平台化使用,会碰到一个问题 业务用户配置的参数化文件的路径一般是在window下面配置的一个,但是我们使用jmeter的分布式压测时, 需要对jmx文件上传到平台,并分发到其他的linux的jmeter压力测试机去,那么路径就对不上了。你又不能要求业务人员上传的时候注意要修改路径,真的是 很蛋疼,业务用户比咱们研发人员牛逼多了,你就得适配它。那么我们需要在程序的某个环节去替换它。 举个栗子,如下jmx文件
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.3">
<org.apache.jorphan.collections.HashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
<stringProp name="TestPlan.comments"></stringProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments">
<collectionProp name="Arguments.arguments"/>
</elementProp>
</TestPlan>
<org.apache.jorphan.collections.HashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="回放流量" enabled="true">
<intProp name="ThreadGroup.num_threads">500</intProp>
<intProp name="ThreadGroup.ramp_time">1</intProp>
<longProp name="ThreadGroup.delay">0</longProp>
<longProp name="ThreadGroup.duration">0</longProp>
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<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">1</stringProp>
</elementProp>
</ThreadGroup>
<org.apache.jorphan.collections.HashTree>
<ConstantThroughputTimer guiclass="TestBeanGUI" testclass="ConstantThroughputTimer" testname="常数吞吐量定时器" enabled="true">
<intProp name="calcMode">1</intProp>
<doubleProp>
<name>throughput</name>
<value>1200.0</value>
<savedValue>0.0</savedValue>
</doubleProp>
</ConstantThroughputTimer>
<org.apache.jorphan.collections.HashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments">
<elementProp name="" elementType="HTTPArgument">
<boolProp name="HTTPArgument.always_encode">false</boolProp>
<stringProp name="Argument.value">${body}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<stringProp name="HTTPSampler.domain">localhost</stringProp>
<stringProp name="HTTPSampler.port">8088</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.path">/mongo/insert</stringProp>
<stringProp name="HTTPSampler.method">POST</stringProp>
<stringProp name="HTTPSampler.contentEncoding">UTF-8</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.postBodyRaw">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>
<org.apache.jorphan.collections.HashTree>
<ResultCollector guiclass="SummaryReport" 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>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<org.apache.jorphan.collections.HashTree/>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="body请求体" enabled="true">
<stringProp name="filename">/Users/liufei/Downloads/jmter/my_replay_data1037568383157994444.csv</stringProp>
<stringProp name="fileEncoding">UTF-8</stringProp>
<boolProp name="ignoreFirstLine">false</boolProp>
<boolProp name="quotedData">true</boolProp>
<boolProp name="recycle">false</boolProp>
<boolProp name="stopThread">false</boolProp>
<stringProp name="variableNames"></stringProp>
<stringProp name="shareMode">shareMode.all</stringProp>
<stringProp name="delimiter">,</stringProp>
</CSVDataSet>
<org.apache.jorphan.collections.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-Type</stringProp>
<stringProp name="Header.value">application/json</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<org.apache.jorphan.collections.HashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testname="察看结果树" enabled="true">
<boolProp name="ResultCollector.error_logging">true</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>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename">/Users/liufei/Downloads/jmter/replay_result.log</stringProp>
</ResultCollector>
<org.apache.jorphan.collections.HashTree/>
</org.apache.jorphan.collections.HashTree>
</org.apache.jorphan.collections.HashTree>
</org.apache.jorphan.collections.HashTree>
</org.apache.jorphan.collections.HashTree>
</jmeterTestPlan>
业务用户人员在本机编写jmx时配置的参数文件如下
<stringProp name="filename">D:///Users/liufei/Downloads/jmter/my_replay_data1037568383157994444.csv</stringProp>
我们上传到平台后需要适配为
<stringProp name="filename">/jmx/my_replay_data1037568383157994444.csv</stringProp>
那么我们需要对它进行修改,这个jmx的结构我们可以通过xml的dom方式处理,但是太复杂了, 现在有一种简洁的处理方式
File jmxFile=new File("test.jmx");
HashTree hashTree=SaveService.loadTree(jmxFile);
SearchByClass<CSVDataSet> csvDataSetSearch = new SearchByClass<>(CSVDataSet.class);
hashTree.traverse(csvDataSetSearch);
Collection<CSVDataSet> csvDataSets = csvDataSetSearch.getSearchResults();
for (CSVDataSet csvDataSet : csvDataSets) {
String filename = csvDataSet.getProperty("filename").getStringValue();
if(filename.equalsIgnoreCase("D:///Users/liufei/Downloads/jmter/my_replay_data1037568383157994444.csv")){
csvDataSet.getProperty("filename").setObjectValue("/jmx/my_replay_data1037568383157994444.csv");
}
}
|