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 15E7548EC for ; Wed, 15 Jun 2011 23:07:04 +0000 (UTC) Received: (qmail 73058 invoked by uid 500); 15 Jun 2011 23:07:03 -0000 Delivered-To: apmail-jakarta-jmeter-user-archive@jakarta.apache.org Received: (qmail 73031 invoked by uid 500); 15 Jun 2011 23:07:03 -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 73022 invoked by uid 99); 15 Jun 2011 23:07:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Jun 2011 23:07:03 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,RFC_ABUSE_POST,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of baerrach@gmail.com designates 209.85.210.172 as permitted sender) Received: from [209.85.210.172] (HELO mail-iy0-f172.google.com) (209.85.210.172) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Jun 2011 23:06:55 +0000 Received: by iyn15 with SMTP id 15so892672iyn.31 for ; Wed, 15 Jun 2011 16:06:33 -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:date :message-id:subject:from:to:content-type; bh=TCtL+GtnFjOyv5rhWOrzcBUp+T0ejiwNlW5+9R5wCfQ=; b=fnXKj/9OApyDnclmQ3ghSBjuNeyJV1r0eRo57wqlITIGqt+8Qkccaj6pzJoXQKvfrx 7jVP0Q0l0fbGoK0ZvPGtVgx0MB3e6xvpzTETXfu1tjyd9OQkYqBVFvb2ERb+ZLOKsb3+ 2Kdli7VOalxEf6/hNrqcEotMVjMOXJddScQMw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=GKgOHsegphXm8SnWKyV37J+0dnBLTTyEhaTAljV9Z41lgh2OwE6tBd9rTdC8Goo5R0 qFVmlH3SPb8an3jWCH9u/ef8+GsISNj9E642DHkqja4qYVJd5KRlbZEEO0JJOnjL5eWL sbsmt2qhw7aXSHj6UVeukzmmKYsX2zYJdyzNg= MIME-Version: 1.0 Received: by 10.231.207.71 with SMTP id fx7mr139620ibb.168.1308179193796; Wed, 15 Jun 2011 16:06:33 -0700 (PDT) Received: by 10.231.19.137 with HTTP; Wed, 15 Jun 2011 16:06:33 -0700 (PDT) In-Reply-To: References: Date: Thu, 16 Jun 2011 08:36:33 +0930 Message-ID: Subject: Re: Custom Sampler Tutorial From: Barrie Treloar To: JMeter Users List Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Checked: Checked by ClamAV on apache.org 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