我开发了一个使用Apache-JMeter的webcrawler。
我创建了一个录制控制器,将请求/响应记录为jmx
树。
要记录jmx我有两个选择:我在代码中集成JMeter api或使用JMeter GUI,我选择它们进行测试。所以我从代码和gui运行apache-jmeter来并排记录请求/响应。以下是针对一个请求的Code-Base-JMeter和GUI-Base-JMeter的jmx结果
代码-BASE-JMeter的
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testname="19 /rural-property-for-sale/yorkshire/north-yorkshire/holmeonspaldingmoor-26186193/" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.implementation">HttpClient4</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.use_keepalive">false</boolProp>
<stringProp name="HTTPSampler.domain">www.uklandandfarms.co.uk</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<stringProp name="HTTPSampler.port">0</stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.path">/rural-property-for-sale/yorkshire/north-yorkshire/holmeonspaldingmoor-26186193/</stringProp>
<elementProp name="HTTPSampler.header_manager" elementType="HeaderManager" guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="Cache-Control" elementType="Header">
<stringProp name="Header.name">Cache-Control</stringProp>
<stringProp name="Header.value">no-cache</stringProp>
</elementProp>
<elementProp name="Accept-Encoding" elementType="Header">
<stringProp name="Header.name">Accept-Encoding</stringProp>
<stringProp name="Header.value">gzip</stringProp>
</elementProp>
<elementProp name="Pragma" elementType="Header">
<stringProp name="Header.name">Pragma</stringProp>
<stringProp name="Header.value">no-cache</stringProp>
</elementProp>
<elementProp name="User-Agent" elementType="Header">
<stringProp name="Header.name">User-Agent</stringProp>
<stringProp name="Header.value">Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1</stringProp>
</elementProp>
<elementProp name="Accept" elementType="Header">
<stringProp name="Header.name">Accept</stringProp>
<stringProp name="Header.value">text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.image_parser">true</boolProp>
</HTTPSamplerProxy>
GUI-BASE-JMeter的
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="32 /" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">www.uklandandfarms.co.uk</stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.protocol">http</stringProp>
<stringProp name="HTTPSampler.contentEncoding">utf-8</stringProp>
<stringProp name="HTTPSampler.path">/</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.image_parser">true</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
<stringProp name="HTTPSampler.implementation">HttpClient4</stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
</HTTPSamplerProxy>
<hashTree>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="Accept-Language" elementType="Header">
<stringProp name="Header.name">Accept-Language</stringProp>
<stringProp name="Header.value">en-GB,en;q=0.5</stringProp>
</elementProp>
<elementProp name="Upgrade-Insecure-Requests" elementType="Header">
<stringProp name="Header.name">Upgrade-Insecure-Requests</stringProp>
<stringProp name="Header.value">1</stringProp>
</elementProp>
<elementProp name="Accept-Encoding" elementType="Header">
<stringProp name="Header.name">Accept-Encoding</stringProp>
<stringProp name="Header.value">gzip, deflate</stringProp>
</elementProp>
<elementProp name="User-Agent" elementType="Header">
<stringProp name="Header.name">User-Agent</stringProp>
<stringProp name="Header.value">Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0</stringProp>
</elementProp>
<elementProp name="Accept" elementType="Header">
<stringProp name="Header.name">Accept</stringProp>
<stringProp name="Header.value">text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8</stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<hashTree/>
</hashTree>
录音之间的差异
1)HTTPSamplerProxy
树在GUI-Base-JMeter重新编码的标题部分之前结束,而HTTPSamplerProxy
树在Code-Base-JMeter的最后结束
2)HeaderManager
是GUI-Base-JMeter中的标记,而在Code-Base-JMeter中是elementProp
标记的属性值。
3)HeaderManager
是GUI-Base-JMeter中HTTPSamplerProxy
的兄弟,而elementProp
标签(HeaderManager
属性 - 值)是HTTPSamplerProxy
的孩子
基本上整个布局在两个录音中都是不同的。我希望Code-Base-JMeter记录与GUI-Base-JMeter重新编码相同。
我正在使用Apache-JMeter 3.2(用于GUI和代码)
以下是代码
初始化
public void init(Integer port) throws Exception {
JMeterUtils.setJMeterHome(Constants.JMETER_HOME);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
JMeterUtils.initLocale();
LoopController loopController = new LoopController();
loopController.setEnabled(true);
loopController.setLoops(1);
RecordingController rc = new RecordingController();
rc.setName("Recording Controller");
TestPlan testPlan = new TestPlan();
ThreadGroup threadGroup1 = new ThreadGroup();
threadGroup1.setSamplerController(loopController);
threadGroup1.addTestElement(rc); // Target
ListedHashTree testPlanTree = new ListedHashTree();
testPlanTree.add(testPlan);
testPlanTree.add(threadGroup1, testPlan);
this.treeModel = new JMeterTreeModel(new Object());
JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
treeModel.addSubTree(testPlanTree, root);
proxy = new ProxyControl();
proxy.setNonGuiTreeModel(treeModel);
proxy.setTarget(treeModel.getNodeOf(threadGroup1));
proxy.setGroupingMode(2); // GROUPING_IN_SIMPLE_CONTROLLERS = 2
// proxy.setGroupingMode(3); // GROUPING_STORE_FIRST_ONLY = 3
proxy.setSamplerTypeName("2"); // SAMPLER_TYPE_HTTP_SAMPLER_HC4 = "2"
proxy.setRegexMatch(Boolean.TRUE);
proxy.setPort(port); // Global Settings -> Port
proxy.setCaptureHttpHeaders(Boolean.TRUE);
proxy.setSamplerFollowRedirects(Boolean.TRUE);
// proxy.setSamplerRedirectAutomatically(Boolean.TRUE);
proxy.setSamplerDownloadImages(Boolean.TRUE);
treeModel.addComponent(proxy, (JMeterTreeNode) root.getChildAt(1));
}
启动JMeter
this.proxy.startProxy();
停止/录制JMeter
/**
* The method stop() is use to stop JMeter recording controller proxy.
*
* @return The tree that is recorded till now
*/
public String stop() {
OutputStream out = null;
try {
this.proxy.stopProxy();
if (this.treeModel == null) {
return null;
}
out = new ByteArrayOutputStream();
HashTree tree = treeModel.getTestPlan();
JMeter.convertSubTree(tree);
SaveService.saveTree(tree, out);
} catch (Exception e) {
return null;
}
return out.toString();
}
答案 0 :(得分:0)
警告:这不是一个合法的解决方案/答案,它只是一个解决方法,因为没有人给我任何解决方案。
我创建一个xslt表达式将Code-Base-JMeter-Jmx转换为Gui-Base-JMeter-Jmx
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:apply-templates />
<hashTree>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<xsl:copy-of select="HTTPSamplerProxy/elementProp[@elementType = 'HeaderManager']/collectionProp[@name='HeaderManager.headers']" />
</HeaderManager>
<hashTree />
</hashTree>
</xsl:template>
<xsl:template match="HTTPSamplerProxy/elementProp[@elementType = 'HeaderManager']" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
现在,如果在JMeter的下一个版本中他们更改了jmx / xml输出,那么我必须再次更改我的xslt以满足更改,这根本不是一个好方法。
所以我仍然渴望拥有合法的解决方案。