在好例子网,分享、交流、成长!
您当前所在位置:首页Others 开发实例一般编程问题 → jmeter源码介绍 二次开发参考

jmeter源码介绍 二次开发参考

一般编程问题

下载此实例
  • 开发语言:Others
  • 实例大小:0.25M
  • 下载次数:3
  • 浏览次数:44
  • 发布时间:2021-03-04
  • 实例类别:一般编程问题
  • 发 布 人:好学IT男
  • 文件格式:.pdf
  • 所需积分:2
 

实例介绍

【实例简介】
jmeter可以通过二次开发实现自定义协议的性能测试。文档介绍了jmeter的代码结构和使用方法
How to write a plugin for JMeter Introduction from peter lin On more than one occasion, users have complained JMeter's developer documentation is out of date and not very usefuL. In an effort to make it easier for developers, I decided to write a simple step-by-step tutorial. When I mentioned this to mike, he had some ideas about what the tutorial should cover Before we dive into the tutorial, Id like to say writing a plugin isn t necessarily easy, even for someone with several years of java experience. The first extension I wrote for JMeter was a simple utility to parse Http access logs and produce requests in Xml format It wasn't really a plugin, since it was a stand alone command line utility. My first real plugin for JMeter was the webservice sampler. I was working on a NET project and needed to stress test a webservice Most of the commercial tools out there for testing nEt webservices suck and cost too much Rather than fork over several hundred dollars for a lame testing tool, or a couple thousand dollars for a good one, I decided it was easier and cheaper to write a plugin for JMeter After a two weeks of coding on my free time, I had a working prototype using Apache Soap driver submitted it back to jMeter and mike asked me if i want to be a committer i had contributed patches to Jakarta JSTL and tomcat in the past, so I considered it an honor. Since then, Ive improved the access log sampler tremendously and made it much more usefu as since then written the access log sampler, Tomcat 5 monitor and distribution graph. Mike ha Introduction from mike stover One of my primary goals in designing JMeter was to make it easy to write plugins to enhance as many of JMeter's features as possible. Part of the benefit of being open-source is that a lot of people could potentially lend their efforts to improve the application. I made a conscious decision to sacrifice some simplicity in the code to make plugin writing a way of life for a JMeter developer While some folks have successfully dug straight into the code and made improvements to JMeter, a real tutorial on how to do this has been lacking. I tried a long time ago to write some documentation about it, but most people did not find it useful. Hopefully, with Peter's help, this attempt will be more successfu Basic structure of jmeter JMeter is organized by protocols and functionality. This is done so that developers can build new jars for a single protocol without having to build the entire application Well go into the details of building JMeter later in the tutorial. Since most of the jmeter developers use eclipse, the article will use eclipse directory as a reference point Root directory-/eclipse/workspacejakarta- jmeter/ Folders X Name 白er Gbi bin build to CVS acvs O testees docs O cVs extras ib CVs src test demos xdocs eee extending classpath sonore localising 回 project usermanual build, bat 日 extras 回buid,sh a cVs jbuild.xml 由 eter Ant Task file 回 eclipse classpath 回 ecipse readme a cvs fD-csy. xsl 口ey fb-exdudes, xml ot a INSTALLING olcD, bat src t-0 components 回 LICENSE core NOTICE CVS 回 README exampes velocity. og functions t-o htmlparser + orphan : otocol test src 日 xdocs CVS demos +压中压国 images arising stylesheets usermanual 25objects(Disk free space: 39.0 GB) iakarta-imeter bin-contains the bat and sh files for starting Jmeter It also contains apache JMeterjar and properties file docs -directory contains the JMeter documentation files extras - ant related extra files lib-contains the required jar files for Jmeter lib/ext- contains the core jar files for jmeter and the protocols src-contains subdirectory for each protocol and component test-unit test related directory xdocs- Xml files for documentation JMeter generates documentation from Xml As the tutorial progresses, an explanation of the subdirectories will be provided. For now, lets focus on"" directory. From the screen capture, we see the following directories Src components-contains non-protocol-specific components like visualizers, assertions, etc core -the core code of JMeter including all core interfaces and abstract classes examples-example sampler demonstrating how to use the new bean framework functions standard functions used by all components htmlparser -a snapshot of HtmlParser, donated by HtmlParser project on sourceforge jorphan-utility classes providing common utility functions monitor-tomcat 5 monitor components protocol -contains the different protocols JMeter supports Within"protocol directory, are the protocol specific components Protocol ftp-components for load testing ttp servers http-componentsforloadtestingwebservers java- components for load testing java components jdbc - components for load testing database servers using jdbc ndi-components for load testing jndi Idap-components for load testing ldap servers mail -components for load testing mail servers tcp- components for load testing TCP services AsageneralruleallsamplersrelatedtoHttpwillresideinhttpdirectory.Theexceptiontothe rule is the Tomcat5 monitor. It is separate, because the functionality of the monitor is slightly different than stress or functional testing. It may eventually be reorganized, but for now it is in its own directory. In terms of difficulty, writing visualizers is probably one of the harder plugins to write Jmeter Gui- TestElement Contract When writing any Jmeter component, there are certain contracts you must be aware of-ways a Jmeter component is expected to behave if it will run properly in the jmeter environment. This section describes the contract that the GuI part of your component must fulfill GUI code in Jmeter is strictly separated from Test Element code. Therefore, when you write a component, there will be a class for the Test Element, and another for the gul presentation. The GUI presentation class is stateless in the sense that it should never hang onto a reference to the Test Element( there are exceptions to this though a gui element should extend the appropriate abstract class provided AbstractSamplerGui AbstractAssertiongu AbstractConfiqGui AbstractControllergui AbstractpostProcessorGui AbstractPreProcessorgui Abstractⅵisua1izer AbstractTimerGui These abstract classes provide so much plumbing work for you that not extending them, and instead implementing the interfaces directly is hardly an option. If you have some burning need to not extend these classes, then you can join me in IRC Where I can convince you otherwise: - So, you've extended the appropriate gui class, what's left to do? Follow these steps Implement getResourceLabel() This method should return the name of the resource that represents the title/name of the component. The resource will have to be entered into Meters messages. properties file (and possibly translations as well 2. Create your gui. Whatever style you like, layout your gui. Your class ultimately extends Jpanel, so your layout must be in your classs own ContentPane. Do not hook up gui elements to your Testelement class via actions and events. Let swings internal model hang onto all the data as much as you possibly can 1. Some standard gui stuff should be added to all Jmeter gui components call setBorder(makeBorder())for your class. This will give it the standard Jmeter bordel 2. add the title pane via makeTitlePanel(). Usually this is the first thing added to your gui, and should be done in a Box vertical layout scheme, or with Jmeter's verticalLayout class. Here is an example init( method private void init () setlayout (new BorderLayout ())i setBorder(makebordero)i Boxbox= Box. createverticalBox()i box. add(makeTitlePanel()) box. add(make SourcePanel())i add (box, Borderlayout. NorTH)i add(makeParameter Panel(), BorderLayout CENTER)i 3. Implement public void configure(TestElement el) 1. Be sure to call super configure(e). This will populate some of the data for you, like the name of the element 2. Use this method to set data into your gui elements. Example public void configure (TestElement el super configure(el)i useHeaders. setselected(el. getPropertyAsBoolean (RegexExtractor, USEHEADERS)) useBody. setselected(!el. getPropertyAsBoolean (RegexExtractor. USEHEADERS)) regexField. setText(el. getPropertyAsstring (RegexExtractor. REGEX))i templateField setText(el. getPropertyAsstring (RegexExtractor. TEMPLATE))i defaultField. setText(el. getPropertyAsstring (RegexExtractor. DEFAULT))i matchNumberField. setText(el. getPropertyAsstring (RegexExtractor MATCH NUM)) refNameField. setText(el. getPropertyAsString (RegexExtractor. REFNAME ))i 4. implement public void modifyTestElement(TestElement e). This is where you move the data from your gui elements to the TestElement. It is the logical reverse of the previous method 1. Call super, configureTestElement(e). This will take care of some default data for 2. Example public void modifyTestElement (TestElement e super. configureTestElement(e)i e setProperty(new Booleanproperty (RegexExtractor USEHEADERS, useHeaders. isSelected())) e setProperty (RegexExtractor MATCH NUMBER, matchNumberField getText if(e instanceof RegexExtractor) RegexExtract egex =(RegexExtractor)e regex. setRefName(refNameField getText ())i regex. setRegex (regexField getText())i regex. setTemplate(templateField getText())i regex. setDefaultvalue(defaultField getText())i 5. implement public TestE-ement createTestElement () This method should create a new instance of your TestElement class, and then pass it to the modifyTestElement (TestElement)method you made above oub1CT∈stE1 enent createrestE⊥eent() RegexExtractor extractor new RegexExtractor( modifyfestElement (extractor) return extractor The reason you cannot hold onto a reference for your Test Element is because Jmeter reuses instance of gui class objects for multiple Test Elements. This saves a lot of memory. It also makes it incredibly easy to write the gui part of your new component. You still have to struggle with the layout in Swing, but you dont have to worry about creating the right events and actions for getting the data from the gui elements into the Testelement where it can do some good. Jmeter knows when to call your configure, and modify TestElement methods where you can do it in a very straightforward way Writing Visualizers is somewhat of a special case, however Writing a Visualizer Of the component types, visualizers require greater depth in Swing than something like controllers, functions or samplers. You can find the full source for the distribution graph in components/orglapache/jmeter/visualizers/". the distribution graph visualizer is divided into two classes Distribution GraphVisualizer-visualizer which Jmeter instantiates Distribution Graph Jcomponent which draws the actual graph The easiest way to write a visualizer is to do the following 1. extend org. apache. jmeter visualizers. gui. AbstractVisualizer 2. implement any additional interfaces need for call back and event notification For example the Distribution GraphVisualizer implements the following interfaces agevisualIZ 2. ItemListener -according to the comments in the class, ItemListener is out of date and isn't used anymore 3. GraphListener 4. Clearable AbstractVisualizer provides some common functionality, which most visualizers like " graph results"use. The common functionality provided by the abstract class includes configure test elements- this means it create a new resultcollector, sets the file and sets the error log create the stock menu update the test element when changes are made create a file panel for the log file create the title panel In some cases, you may not want to display the menu for the file textbox. In that case, you can override the " init("method. Here is the implementation for Distribution GraphVisualizer 2 Initialize the gul 4 private void init( this setLayout(new BorderLayout() 8 // MAIN PANEL Border margin new Empty Border(10, 10, 5, 10); 10 this. setBorder(margin) 13 /Set up the graph with header, footer, Y axis and graph display 14 JPanel graphPanel new JPanel(new Borderlayout( 15 graph PaneL. add(create Graph(, BorderLayout CENTER 6 graphPanel add(create GraphInfoPanelo, borderLayout SOUTH) 17 8 Add the main panel and the graph this add(make TitlePanel(, BorderLayout. NORTH) 20 this add(graphPanel, BorderLayout CENTER); The first thing the init method does is create a new BorderLayout. Depending on how you want to layout the widgets, you may want to use a different layout manager. Keep mind using different layout managers is for experts The second thing the init method does is create a border. If you want to increase or decrease the border, change the four integer values. Each integer value represents pixels. If you want your visualizer to have no border skip lines 9 and 11. ine 15 calls "create GraphPanel, " which is responsible for configuring and adding the distribution graph to the visualizer. 1 private Component create GraphPanelo 2{ 3 graphPanel new JPanel0 4 graphPanel setBorder(BorderFactory create Bevelborder BevelBorder. LOWERED, Color light Gray, Color, dark Gray)) 56789 graphPanel. add(graph) graphPanel. setBackground(Color. white) return graphPanel At line 6, the graph component is added to the graph panel. The constructor is where a new instance of Distribution Graph is created public Distribution GraphVisualizer( model new Sampling StatCalculator(Distribution"); graph new Distribution Graph(model); graph setBackground(Color. white) The constructor of Distribution Graph Visualizer is responsible for creating the model and the graph. Every time a new result is complete, the engine passes the result to all the listeners by calling add(sample Result res). The visualizer passes the new sampleresult to the model 1 public synchronized void add(sample Result res) 2345 model. addSample(res); update Gui(model. get Currentsample() In the case of the Distribution GraphVisualizer, theadd' method doesn t acutally update the graph. Instead, it calls"update Gui "in line four. public synchronized void update Gui(Sample s // We have received one more sample if (delay = counter) update Guio else counter++: Unlike GraphVisualizer, the distribution graph attempts to show how the results clump; therefore the Distribution GraphVisualizer delays the update. The default delay is 10 sampleresults 1 public synchronized void update Guio 3 if (graph. getWidth( <10R 4 graph. setPreferredSize(new Dimension(getWidth(-40, getHeight()-160)) 5} graphPanel. updateUIO 7 graph repaint( 8} Lines 3 to 4 are suppose to resize the graph, if the user resizes the window or drags the divider Line 6 updates the panel containing the graph Line 7 triggers the update of the Distribution Graph Before we cover writing graphcs, there are couple of important methods visualizer must implement public String getLabelResourceo return" distribution graph title The label resource retrieves the name of the visualizer from the properties file. The file is located in"/org/apache/jmeter/resources". It's best not to hardcode the name of the visualizer Message. properties file is organized alphabetically, so adding a new entry is easy public synchronized void clear( this graph. clear(; model. clear(; repaint(; Every component in Jmeter should implement logic for"clear("method. If this isnt done, the component will not clear the Ul or model when the user tries to clear the last results and run a public JComponent getPrintable Component(X return this.graphPanel The last method visualizers should implement is "getPrintable Component(. The method is responsible for returning the Jcomponent that can be saved or printed. This feature was recently added so that users can save a screen capture of any given visualizer GraphListener Visualizers should implement Graph Listener. This is done to make it simpler to add new Sample instances to listeners. As a general rule, if the a custom graph does not plot every single sample it does not need to implement the interface public interface GraphListener id updateGui( Sample s) public void update Gui(; The important method in the interface is"update Gui (sample s).From Distribution GraphVisualizer, we see it calls graph repaint( to refresh the graph. In most cases the implementation of update Gui(sample s) should do just that ItemListener 【实例截图】
【核心代码】

标签:

实例下载地址

jmeter源码介绍 二次开发参考

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警