Return-Path: X-Original-To: apmail-jakarta-jmeter-user-archive@www.apache.org Delivered-To: apmail-jakarta-jmeter-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3187168C9 for ; Thu, 16 Jun 2011 16:18:14 +0000 (UTC) Received: (qmail 87753 invoked by uid 500); 16 Jun 2011 16:18:13 -0000 Delivered-To: apmail-jakarta-jmeter-user-archive@jakarta.apache.org Received: (qmail 87712 invoked by uid 500); 16 Jun 2011 16:18:13 -0000 Mailing-List: contact jmeter-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "JMeter Users List" Reply-To: "JMeter Users List" Delivered-To: mailing list jmeter-user@jakarta.apache.org Received: (qmail 87703 invoked by uid 99); 16 Jun 2011 16:18:13 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Jun 2011 16:18:13 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,RFC_ABUSE_POST,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of electric.or.sharp@gmail.com designates 209.85.161.172 as permitted sender) Received: from [209.85.161.172] (HELO mail-gx0-f172.google.com) (209.85.161.172) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Jun 2011 16:18:07 +0000 Received: by gxk19 with SMTP id 19so1211875gxk.31 for ; Thu, 16 Jun 2011 09:17:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=Gk7B/f3mFWnawUH7oaZS9J4GvP8JXCfQQTd25DfGVrc=; b=NjzVmLOG82bezi6oqFhKnQAGWMLSPtXMWogIJpXqH1xcXE08YQ0DtedR+5Wc+F1ntw 2hDmr4vQy6Fl3dqY6zOgCtvMj/kAPduJ0Vd9Hz9ubnC2kMSr5DeSpy362t7z3rWSfuAt KKMUYASbq3vgorGsQmYwQklhVM64TlM0EWWWs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=tMl2YEC67ICK9787sc/gdzeKlJMRNGXqpEIMNlQ2ayvNUC8lnbtMfrNQWqIDAV1q0M r1xwpjmIVhq3hS7uMu4NOHvub5cdN5CCQFTx1j3OnrZDiQM2fogbE0Dw4wpV1V2p7RMF rrJv/q5ElFqozysqPOqP4bcCsis/bwLyHmMAM= Received: by 10.90.249.28 with SMTP id w28mr1399949agh.40.1308241066096; Thu, 16 Jun 2011 09:17:46 -0700 (PDT) MIME-Version: 1.0 Received: by 10.90.34.20 with HTTP; Thu, 16 Jun 2011 09:17:06 -0700 (PDT) In-Reply-To: References: From: E S Date: Thu, 16 Jun 2011 11:17:06 -0500 Message-ID: Subject: Re: Custom Sampler Tutorial To: JMeter Users List Content-Type: multipart/alternative; boundary=0016363b88c4067b6a04a5d69c28 --0016363b88c4067b6a04a5d69c28 Content-Type: text/plain; charset=ISO-8859-1 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 wrote: > 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 = > Hierarchy.getDefaultHierarchy().getLoggerFor(.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 samplerData to the > * requestData. > * > * @param result > * the sample result to update > * @param requestData > * the request to set as samplerData > */ > protected void sampleResultStart(SampleResult result, String requestData) > { > result.setSamplerData(requestData); > result.sampleStart(); > } > > /** > * Set the sample result as sampleEnd(), > * setSuccessful(true), setResponseCode("OK") > and if > * the response is not null then > * setResponseData(response.toString(), ENCODING) 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 sampleEnd, > * setSuccessful(false) and the setResponseCode > 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 > * sampleResultFailed(result, "Exception raised: " + cause) > * > * @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 > > --0016363b88c4067b6a04a5d69c28--