Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.java.test » [javadoc | source]

    1   // $Header: /home/cvs/jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/test/SleepTest.java,v 1.13 2005/07/12 20:51:07 mstover1 Exp $
    2   /*
    3    * Copyright 2002-2004 The Apache Software Foundation.
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    * 
   17    */
   18   package org.apache.jmeter.protocol.java.test;
   19   
   20   import java.io.Serializable;
   21   import java.util.Iterator;
   22   
   23   import org.apache.jmeter.config.Arguments;
   24   import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
   25   import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
   26   import org.apache.jmeter.samplers.SampleResult;
   27   
   28   /**
   29    * The <code>SleepTest</code> class is a simple example class for a JMeter
   30    * Java protocol client. The class implements the <code>JavaSamplerClient</code>
   31    * interface.
   32    * <p>
   33    * During each sample, this client will sleep for some amount of time. The
   34    * amount of time to sleep is determined from the two parameters SleepTime and
   35    * SleepMask using the formula:
   36    * 
   37    * <pre>
   38    * totalSleepTime = SleepTime + (System.currentTimeMillis() % SleepMask)
   39    * </pre>
   40    * 
   41    * Thus, the SleepMask provides a way to add a random component to the sleep
   42    * time.
   43    * 
   44    * @author <a href="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
   45    * @version $Revision: 1.13 $
   46    */
   47   public class SleepTest extends AbstractJavaSamplerClient implements Serializable {
   48   	/**
   49   	 * The default value of the SleepTime parameter, in milliseconds.
   50   	 */
   51   	public static final long DEFAULT_SLEEP_TIME = 1000;
   52   
   53   	/**
   54   	 * The default value of the SleepMask parameter.
   55   	 */
   56   	public static final long DEFAULT_SLEEP_MASK = 0x3ff;
   57   
   58   	/**
   59   	 * The base number of milliseconds to sleep during each sample.
   60   	 */
   61   	private long sleepTime;
   62   
   63   	/**
   64   	 * A mask to be applied to the current time in order to add a random
   65   	 * component to the sleep time.
   66   	 */
   67   	private long sleepMask;
   68   
   69   	/**
   70   	 * Default constructor for <code>SleepTest</code>.
   71   	 * 
   72   	 * The Java Sampler uses the default constructor to instantiate an instance
   73   	 * of the client class.
   74   	 */
   75   	public SleepTest() {
   76   		getLogger().debug(whoAmI() + "\tConstruct");
   77   	}
   78   
   79   	/**
   80   	 * Do any initialization required by this client. In this case,
   81   	 * initialization consists of getting the values of the SleepTime and
   82   	 * SleepMask parameters. It is generally recommended to do any
   83   	 * initialization such as getting parameter values in the setupTest method
   84   	 * rather than the runTest method in order to add as little overhead as
   85   	 * possible to the test.
   86   	 * 
   87   	 * @param context
   88   	 *            the context to run with. This provides access to
   89   	 *            initialization parameters.
   90   	 */
   91   	public void setupTest(JavaSamplerContext context) {
   92   		getLogger().debug(whoAmI() + "\tsetupTest()");
   93   		listParameters(context);
   94   
   95   		sleepTime = context.getLongParameter("SleepTime", DEFAULT_SLEEP_TIME);
   96   		sleepMask = context.getLongParameter("SleepMask", DEFAULT_SLEEP_MASK);
   97   	}
   98   
   99   	/**
  100   	 * Perform a single sample. In this case, this method will simply sleep for
  101   	 * some amount of time. Perform a single sample for each iteration. This
  102   	 * method returns a <code>SampleResult</code> object.
  103   	 * <code>SampleResult</code> has many fields which can be used. At a
  104   	 * minimum, the test should use <code>SampleResult.sampleStart</code> and
  105   	 * <code>SampleResult.sampleEnd</code>to set the time that the test
  106   	 * required to execute. It is also a good idea to set the sampleLabel and
  107   	 * the successful flag.
  108   	 * 
  109   	 * @see org.apache.jmeter.samplers.SampleResult#sampleStart()
  110   	 * @see org.apache.jmeter.samplers.SampleResult#sampleEnd()
  111   	 * @see org.apache.jmeter.samplers.SampleResult#setSuccessful(boolean)
  112   	 * @see org.apache.jmeter.samplers.SampleResult#setSampleLabel(String)
  113   	 * 
  114   	 * @param context
  115   	 *            the context to run with. This provides access to
  116   	 *            initialization parameters.
  117   	 * 
  118   	 * @return a SampleResult giving the results of this sample.
  119   	 */
  120   	public SampleResult runTest(JavaSamplerContext context) {
  121   		SampleResult results = new SampleResult();
  122   
  123   		try {
  124   			// Record sample start time.
  125   			results.sampleStart();
  126   
  127   			// Generate a random value using the current time.
  128   			long start = System.currentTimeMillis();
  129   			long sleep = getSleepTime() + (start % getSleepMask());
  130   
  131   			results.setSampleLabel("Sleep Test: time = " + sleep);
  132   
  133   			// Execute the sample. In this case sleep for the
  134   			// specified time.
  135   			Thread.sleep(sleep);
  136   
  137   			results.setSuccessful(true);
  138   		} catch (InterruptedException e) {
  139   			getLogger().warn("SleepTest: interrupted.");
  140   			results.setSuccessful(true);
  141   		} catch (Exception e) {
  142   			getLogger().error("SleepTest: error during sample", e);
  143   			results.setSuccessful(false);
  144   		} finally {
  145   			results.sampleEnd();
  146   		}
  147   
  148   		if (getLogger().isDebugEnabled()) {
  149   			getLogger().debug(whoAmI() + "\trunTest()" + "\tTime:\t" + results.getTime());
  150   			listParameters(context);
  151   		}
  152   
  153   		return results;
  154   	}
  155   
  156   	/**
  157   	 * Do any clean-up required by this test. In this case no clean-up is
  158   	 * necessary, but some messages are logged for debugging purposes.
  159   	 * 
  160   	 * @param context
  161   	 *            the context to run with. This provides access to
  162   	 *            initialization parameters.
  163   	 */
  164   	public void teardownTest(JavaSamplerContext context) {
  165   		getLogger().debug(whoAmI() + "\tteardownTest()");
  166   		listParameters(context);
  167   	}
  168   
  169   	/**
  170   	 * Provide a list of parameters which this test supports. Any parameter
  171   	 * names and associated values returned by this method will appear in the
  172   	 * GUI by default so the user doesn't have to remember the exact names. The
  173   	 * user can add other parameters which are not listed here. If this method
  174   	 * returns null then no parameters will be listed. If the value for some
  175   	 * parameter is null then that parameter will be listed in the GUI with an
  176   	 * empty value.
  177   	 * 
  178   	 * @return a specification of the parameters used by this test which should
  179   	 *         be listed in the GUI, or null if no parameters should be listed.
  180   	 */
  181   	public Arguments getDefaultParameters() {
  182   		Arguments params = new Arguments();
  183   		params.addArgument("SleepTime", String.valueOf(DEFAULT_SLEEP_TIME));
  184   		params.addArgument("SleepMask", "0x" + (Long.toHexString(DEFAULT_SLEEP_MASK)).toUpperCase());
  185   		return params;
  186   	}
  187   
  188   	/**
  189   	 * Dump a list of the parameters in this context to the debug log.
  190   	 * 
  191   	 * @param context
  192   	 *            the context which contains the initialization parameters.
  193   	 */
  194   	private void listParameters(JavaSamplerContext context) {
  195   		if (getLogger().isDebugEnabled()) {
  196   			Iterator argsIt = context.getParameterNamesIterator();
  197   			while (argsIt.hasNext()) {
  198   				String name = (String) argsIt.next();
  199   				getLogger().debug(name + "=" + context.getParameter(name));
  200   			}
  201   		}
  202   	}
  203   
  204   	/**
  205   	 * Generate a String identifier of this test for debugging purposes.
  206   	 * 
  207   	 * @return a String identifier for this test instance
  208   	 */
  209   	private String whoAmI() {
  210   		StringBuffer sb = new StringBuffer();
  211   		sb.append(Thread.currentThread().toString());
  212   		sb.append("@");
  213   		sb.append(Integer.toHexString(hashCode()));
  214   		return sb.toString();
  215   	}
  216   
  217   	/**
  218   	 * Get the value of the sleepTime field.
  219   	 * 
  220   	 * @return the base number of milliseconds to sleep during each sample.
  221   	 */
  222   	private long getSleepTime() {
  223   		return sleepTime;
  224   	}
  225   
  226   	/**
  227   	 * Get the value of the sleepMask field.
  228   	 * 
  229   	 * @return a mask to be applied to the current time in order to add a random
  230   	 *         component to the sleep time.
  231   	 */
  232   	private long getSleepMask() {
  233   		return sleepMask;
  234   	}
  235   }

Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.java.test » [javadoc | source]