jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Barrie Treloar <>
Subject Re: Custom Sampler Tutorial
Date Wed, 15 Jun 2011 23:06:33 GMT
On Wed, Jun 15, 2011 at 3:41 PM, Bruce Ide <> wrote:
> I've written a couple of data elements, a sampler and a post-processor. Most
> of what I picked up came from reading the jmeter source code itself. That's
> probably the best place to start. I'm still not clear on how some of the UI
> stuff works, but that's more due to me not being terribly familiar with
> swing than because of jmeter.

The reason there are not tons of examples is because most people are using HTTP.
Any other testing and you are in the minority.

As Bruce says,  its hack away.
What type of Custom Sampler are you writing?

If its a JavaSamplerClient, then here are some notes from my hacking.

* Logging is available by

import org.apache.log.Logger;
  private static final Logger LOGGER =

* Threading model is one "client" per Thread.
  If you need state between threads you need to really think hard
about how to implement it.
  Our problem was we are modelling the a thick Java client that
connects over UDP.
  So something needs to start up a UDP listen port and receive
messages and pass them on to the correct sampler.
  We *BROKE* the JMeter threading model and made the ThreadGroup the
"client".  But we did this knowing the limitations.
  Your mileage may vary...

* getDefaultParameters() defines the complete and final set of
parameters available.
  The UI has an "add" button, but this doesn't actually add a new
parameter to the class.  There is JIRA about removing that button.
  If you want to add a new parameter to your test you must add it to
the Arguments returned by getDefaultParameters().
  My personal preference is to set the default value of the parameter
   * See class Javadoc.
   * @JMeterParameter
  public static final String PARAMETER_TIMEOUT = "timeout";

  public Arguments getDefaultParameters() {
    Arguments arguments = new Arguments();
    arguments.addArgument(PARAMETER_TIMEOUT, "${TIMEOUT}");

  I also define a constant for the parameter in my Java sampler class
because you will need it to extract the value of the the
JavaSamplerContext in runTest()

  I document in the class javadoc what all the parameters mean and
acceptable values.

* create your own factory method for instantiating SampleResults
  I found I was setting the same values all the time, so a factory
method saved some duplication.

   * Use UTF-8 for encoding of strings
  public static final String ENCODING = "UTF-8";

  public SampleResult newSampleResult() {
    SampleResult result = new SampleResult();
    return result;

* create helper methods for start, success and failure.
   * Start the sample request and set the <code>samplerData</code> to the
   * requestData.
   * @param result
   *          the sample result to update
   * @param requestData
   *          the request to set as <code>samplerData</code>
  protected void sampleResultStart(SampleResult result, String requestData) {

   * Set the sample result as <code>sampleEnd()</code>,
   * <code>setSuccessful(true)</code>, <code>setResponseCode("OK")</code>
and if
   * the response is not <code>null</code> then
   * <code>setResponseData(response.toString(), ENCODING)</code> otherwise it
   * marked as not requiring a response.
   * @param result
   *          sample result to change
   * @param response
   *          the successful result message, may be null.
  protected void sampleResultSuccess(SampleResult result,
      String response) {
    if (response != null) {
      result.setResponseData(response, ENCODING);
    else {
      result.setResponseData("No response required", ENCODING);

   * Mark the sample result as <code>sampleEnd</code>,
   * <code>setSuccessful(false)</code> and the <code>setResponseCode</code>
   * reason.
   * @param result
   *          the sample result to change
   * @param reason
   *          the failure reason
  protected void sampleResultFailed(SampleResult result, String reason) {

   * Equivalent to
   * <code>sampleResultFailed(result, "Exception raised: " + cause)</code>
   * @param result
   *          the result to modify
   * @param cause
   *          the cause of the failure
  protected void sampleResultFailed(SampleResult result, Exception cause) {
    sampleResultFailed(result, "Exception raised: " + cause);

* If your test has multiple results, you will need to manually create
a parent container SampleResult and determine the success/failure
based on the children.
  (Subresults are inserted via result.addSubResult())

  Something like (NOTE: this assumes there is only a depth of 1 for
the children)

  public SampleResult runTest(JavaSamplerContext context) {
    } finally {
      SampleResult[] subResults = result.getSubResults();
      boolean isOk = true;
      for (SampleResult sampleResult : subResults) {
        if (!sampleResult.isSuccessful()) {
          isOk = false;

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message