How to Write a plugin for JMeter
Authors
Mike Stover, Peter Lin
Revision 1.1 November 2005
Copyright Apache
Table of Contents
Basic structure of JMeter.................................................................................................3
Writing a Visualizer........................................................................................................ 5
GraphListener..................................................................................................................8
ItemListener.....................................................................................................................8
Writing Custom Graphs.................................................................................................. 8
Making a TestBean Plugin For Jmeter..........................................................................10
Building JMeter.............................................................................................................14
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, I'd 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.
I 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, I've
written the access log sampler, Tomcat 5 monitor and distribution graph. Mike has since then
improved the access log sampler tremendously and made it much more useful.
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 successful.
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. We'll 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/workspace/jakarta-jmeter/
jakarta-jmeter
• bin – contains the .bat and .sh files for starting Jmeter. It also contains ApacheJMeter.jar 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 “src” 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 ftp servers
• http – components for load testing web servers
• java – components for load testing java components
• jdbc – components for load testing database servers using jdbc
• jndi – components for load testing jndi
• ldap – components for load testing LDAP servers
• mail – components for load testing mail servers
• tcp – components for load testing TCP services
As a general rule, all samplers related to HTTP will reside in “http” directory. The exception to the
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 GUI 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).
- 1
- 2
- 3
- 4
- 5
- 6
前往页