I have developed a webcrawler that uses Apache-JMeter.
I have created a recording controller to record request/response as jmx
tree.
To record jmx I have two options either I integrate JMeter api in my code or use JMeter GUI, I choose both for testing. So I run the apache-jmeter from code and gui to record the request/response side-by-side. Below are the jmx results of Code-Base-JMeter and GUI-Base-JMeter for one request
Code-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>
Differences between recordings
1) HTTPSamplerProxy
tree ends before the header section in GUI-Base-JMeter recoding, while HTTPSamplerProxy
tree ends at last of Code-Base-JMeter
2) HeaderManager
is tag in GUI-Base-JMeter while in Code-Base-JMeter its property-value of elementProp
tag.
3) HeaderManager
is sibling of HTTPSamplerProxy
in GUI-Base-JMeter while elementProp
tag (of HeaderManager
property-value) is child of HTTPSamplerProxy
.
Basically whole layout is different in both recordings. I want Code-Base-JMeter recording same as GUI-Base-JMeter recoding.
I am using Apache-JMeter 3.2 (both for GUI and code)
Below is the code
To Initialize
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));
}
Start JMeter
this.proxy.startProxy();
Stop/Record 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();
}