In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to configure HashTree in JMeter engine. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
I. Preface
You can see that the following is a screenshot of the configuration of the JMeter console, which is a standard menu form; the menu form is actually similar to the "tree" data structure, while HashTree is actually a tree data structure
The jmx file we exported in the JMeter console is a xml-structured data, which is actually generated by HashTree, which we'll talk about later.
II. The usage of HashTree
First of all, it is introduced by the HashTree class, which is a collection class; it has the function of Map structure, and it is a tree structure.
/ * This class is used to create a tree structure of objects. Each element in the * tree is also a key to the next node down in the tree. It provides many ways * to add objects and branches, as well as many ways to retrieve. *
* HashTree implements the Map interface for convenience reasons. The main * difference between a Map and a HashTree is that the HashTree organizes the * data into a recursive tree structure, and provides the means to manipulate * that structure. *
* Of special interest is the {@ link # traverse (HashTreeTraverser)} method, which * provides an expedient way to traverse any HashTree by implementing the * {@ link HashTreeTraverser} interface in order to perform some operation on the * tree, or to extract information from the tree. * * @ see HashTreeTraverser * @ see SearchByClass * / public class HashTree implements Serializable, Map, Cloneable {}
HashTree methods commonly used in JMeter (the following figure is configured as an example)
/ / ListedHashTree is the inheritance class of HashTree, which can guarantee the sequence of HashTree HashTree tree = new ListedHashTree (); / / TestPlan object, test plan TestPlan plan = new TestPlan (); / / ThreadGroup object, thread group ThreadGroup group = new ThreadGroup (); / / object that creates thread group structure groupTreeHashTree groupTree = new ListedHashTree (); / / indicates HTTP request HTTPSamplerProxy sampler = new HTTPSamplerProxy () in the sampler / / creating the number structure object requested by HTTP samplerTree// calling the put method is equivalent to adding a group (thread group) submenu under the plan menu object, thus forming a tree structure HashTree samplerTree = new ListedHashTree () SamplerTree.put (sampler,new ListedHashTree ()) / / groupTree tree structure add subtree samplerTreegroupTree.put (group,samplerTree) / / tree tree structure as the test plan object, add subtree groupTree, thus forming the hierarchical form of the above figure tree.put (plan, groupTree) / / calling the add method is equivalent to adding a sibling menu tree.add (Object key) under the tree menu object. 3. Introduction to JMeter source code export jmx script file
First of all, all click events in the JMeter console will be monitored and executed by the performaAction method in ActionRouter. Click the export button, and the method shown in the figure will be executed by the Save class through reflection.
The main purpose of executing doAction in the Save class is to get the configured HashTree
When you click Save, it creates an empty file with no content
Save
The doAction method of the class finally calls backupAndSave (e, subTree, fullSave, updateFile), which is to write the created empty file to the xml content
SaveTree method in SaveService, where JMXSAVER is a XStream object, and the corresponding maven coordinates are as follows
Com.thoughtworks.xstream xstream 1.4.15
4. Custom HashTree generate JMeter script
First, maven introduces the following coordinates 5.3
Org.apache.jmeter ApacheJMeter_http ${jmeter.version} org.apache.logging.log4j log4j-slf4j-impl org.apache.jmeter ApacheJMeter_functions ${jmeter.version} Org.apache.jmeter ApacheJMeter_jdbc ${jmeter.version} org.apache.jmeter ApacheJMeter_tcp ${jmeter.version}
First create a sampler and then write it into the data structure of HashTree
Public static ThreadGroup threadGroup;// creates a standard thread group private static void initThreadGroup () {LoopController loopController = new LoopController (); loopController.setName ("LoopController"); loopController.setProperty (TestElement.TEST_CLASS, LoopController.class.getName ()); loopController.setProperty (TestElement.GUI_CLASS, JMeterUtil.readSaveProperties ("LoopControlPanel")); loopController.setEnabled (true); loopController.setLoops (1); ThreadGroup group = new ThreadGroup (); group.setEnabled (true) Group.setName ("ThreadGroup"); group.setProperty (TestElement.TEST_CLASS, JMeterUtil.readSaveProperties ("ThreadGroup")); group.setProperty (TestElement.GUI_CLASS, JMeterUtil.readSaveProperties ("ThreadGroupGui")); group.setProperty (ThreadGroup.ON_SAMPLE_ERROR, "continue"); group.setProperty (ThreadGroup.IS_SAME_USER_ON_NEXT_ITERATION,true); group.setProperty (TestElement.COMMENTS, "); group.setNumThreads (1); group.setRampUp (1) Group.setDelay (0); group.setDuration (0); group.setProperty (ThreadGroup.ON_SAMPLE_ERROR, ThreadGroup.ON_SAMPLE_ERROR_CONTINUE); group.setScheduler (false); group.setSamplerController (loopController); threadGroup = group;}
Create a standard thread group
Public static ThreadGroup threadGroup;// creates a standard thread group private static void initThreadGroup () {LoopController loopController = new LoopController (); loopController.setName ("LoopController"); loopController.setProperty (TestElement.TEST_CLASS, LoopController.class.getName ()); loopController.setProperty (TestElement.GUI_CLASS, JMeterUtil.readSaveProperties ("LoopControlPanel")); loopController.setEnabled (true); loopController.setLoops (1); ThreadGroup group = new ThreadGroup (); group.setEnabled (true) Group.setName ("ThreadGroup"); group.setProperty (TestElement.TEST_CLASS, JMeterUtil.readSaveProperties ("ThreadGroup")); group.setProperty (TestElement.GUI_CLASS, JMeterUtil.readSaveProperties ("ThreadGroupGui")); group.setProperty (ThreadGroup.ON_SAMPLE_ERROR, "continue"); group.setProperty (ThreadGroup.IS_SAME_USER_ON_NEXT_ITERATION,true); group.setProperty (TestElement.COMMENTS, "); group.setNumThreads (1); group.setRampUp (1) Group.setDelay (0); group.setDuration (0); group.setProperty (ThreadGroup.ON_SAMPLE_ERROR, ThreadGroup.ON_SAMPLE_ERROR_CONTINUE); group.setScheduler (false); group.setSamplerController (loopController); threadGroup = group;}
Create a standard test plan
Public static TestPlan testPlan;// creates a standard test plan private static void initTestPlan () {TestPlan plan = new TestPlan (); / / sets the properties and content of the test plan, and finally changes to the attribute and content of the xml tag plan.setProperty (TestElement.NAME, "test plan"); plan.setProperty (TestElement.TEST_CLASS, JMeterUtil.readSaveProperties ("TestPlan")); plan.setProperty (TestElement.GUI_CLASS, JMeterUtil.readSaveProperties ("TestPlanGui")) Plan.setEnabled (true); plan.setComment ("); plan.setFunctionalMode (false); plan.setTearDownOnShutdown (true); plan.setSerialized (false); plan.setProperty (" TestPlan.user_define_classpath ","); plan.setProperty ("TestPlan.user_defined_variables", "); plan.setUserDefinedVariables (new Arguments ()); testPlan = plan;}
Start encapsulating the configuration into a HashTree
/ / create a test plan hashtree object HashTree hashTree = new ListedHashTree (); / / create a thread group threaddGroupTree object HashTree threadGroupTree = new ListedHashTree (); / / HttpRequestConfig is the request header, request body and other information data corresponding to HTTP, and input httpToHashTree static method to obtain the HashTree data structure of the sampler. The source code above has shared HashTree httpConfigTree = XXClass.httpToHashTree (HttpRequestConfig httpRequestData) / / threadGroupTree to add submenu httpConfigTree object threadGroupTree.put (group, httpConfigTree). / / Test Plan hashTree add submenu threadGroupTree object hashTree.put (JMeterTestPlanConfigService.testPlan, threadGroupTree)
After HashTree is written, call the JMeter native method SaveService.saveTree (hashTree,outStream); generate the corresponding xml
If called directly, the generated xml format will form the following figure instead of the JMeter native export jmx form. This file structure will report an error when read by the JMeter console and cannot be recognized.
Later, I read the SaveService source code to understand that the static code block content and attributes will be initialized before generating the xml file.
During the process, the findFile method in JMeterUtils is called to find the saveservice.properties file.
Since static methods in SaveService cannot be overridden, there are two solutions to finding saveservice.properties based on the last call to the findFile method in JMeterUtils
Option 1: it is not recommended to store saveservice.properties in the root directory of the project so that the findFile method can get it, but this is not good, because the file will not be typed when maven is packaged, at least this is the problem with my springboot project.
Option 2: it is recommended to create a temporary file named saveservice.properties, and then read the saveservice.properties configuration into the temporary file in advance, so that you can also find the configuration when calling the findFile method in JMeterUtils, and successfully solve the problems caused by SaveService initialization properties. The specific code is as follows
Private void hashTreeToXML (HashTree hashTree,PressureConfigInfo configInfo) {FileOutputStream outStream = null; File file = new File ("temp.jmx"); File tempFile = null; try {/ / create a temporary saveservice.properties file tempFile = new File ("saveservice.properties"); InputStream is = JMeterUtil.class.getResource ("/ jmeter/saveservice.properties"). OpenStream () / / write configuration file to temporary file FileUtil.writeFromStream (is,tempFile); outStream = new FileOutputStream (file); / / call saveTree to xml SaveService.saveTree (hashTree,outStream) successfully; String xmlContent = FileUtil.readUtf8String (file); configInfo.setFile (xmlContent.getBytes ());} catch (IOException e) {e.printStackTrace () } finally {try {FileUtils.forceDelete (file); FileUtils.forceDelete (tempFile);} catch (IOException e) {e.printStackTrace ();}
The final generated xml file structure is shown in the following figure, which can also be successfully opened and identified through the JMeter console.
This is the end of this article on "how to configure HashTree in JMeter engine". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.