jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From E S <electric.or.sh...@gmail.com>
Subject Re: Custom Sampler Tutorial
Date Thu, 16 Jun 2011 16:17:06 GMT
Barrie,

Thanks for sharing your code. I am also currently using the Http Request
sampler, but I don't know of an easy way to simulate the required use cases
with that sampler in conjunction with other elements like timers. Instead of
repeating the whole problem, I'll just point you to another thread in which
I'm already discussing the issue (and why I'm leaning toward a custom
sampler).

http://mail-archives.apache.org/mod_mbox/jakarta-jmeter-user/201106.mbox/%3CBANLkTikOEj_k-fcigkOhP4RggUkg3JCKVA@mail.gmail.com%3E

That's just my original post. There are follow ups as well, but I couldn't
get it to include all those in one link.

Dave

On Wed, Jun 15, 2011 at 6:06 PM, Barrie Treloar <baerrach@gmail.com> wrote:

> On Wed, Jun 15, 2011 at 3:41 PM, Bruce Ide <flyingrhenquest@gmail.com>
> 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 =
> Hierarchy.getDefaultHierarchy().getLoggerFor(<YOUR_CLASS>.class.getName());
>
> * 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
> to "${YOUR_PARAMETER_NAME}"
> e.g.
>  /**
>   * 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();
>    result.setDataEncoding(ENCODING);
>    result.setDataType(SampleResult.TEXT);
>    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)
> {
>    result.setSamplerData(requestData);
>    result.sampleStart();
>  }
>
>  /**
>   * 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 is
>   * 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) {
>    result.sampleEnd();
>    result.setSuccessful(true);
>    result.setResponseCode("OK");
>    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>
> to
>   * reason.
>   *
>   * @param result
>   *          the sample result to change
>   * @param reason
>   *          the failure reason
>   */
>  protected void sampleResultFailed(SampleResult result, String reason) {
>    result.sampleEnd();
>    result.setSuccessful(false);
>    result.setResponseCode(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;
>          break;
>        }
>      }
>      result.setSuccessful(isOk);
>      result.sampleEnd();
>    }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message