001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.jmeter.protocol.java.test;
019:
020: import java.io.Serializable;
021: import java.util.Iterator;
022:
023: import org.apache.jmeter.config.Arguments;
024: import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
025: import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
026: import org.apache.jmeter.samplers.SampleResult;
027:
028: /**
029: * The <code>SleepTest</code> class is a simple example class for a JMeter
030: * Java protocol client. The class implements the <code>JavaSamplerClient</code>
031: * interface.
032: * <p>
033: * During each sample, this client will sleep for some amount of time. The
034: * amount of time to sleep is determined from the two parameters SleepTime and
035: * SleepMask using the formula:
036: *
037: * <pre>
038: * totalSleepTime = SleepTime + (System.currentTimeMillis() % SleepMask)
039: * </pre>
040: *
041: * Thus, the SleepMask provides a way to add a random component to the sleep
042: * time.
043: *
044: * @author <a href="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
045: * @version $Revision: 493789 $
046: */
047: public class SleepTest extends AbstractJavaSamplerClient implements
048: Serializable {
049: /**
050: * The default value of the SleepTime parameter, in milliseconds.
051: */
052: public static final long DEFAULT_SLEEP_TIME = 1000;
053:
054: /**
055: * The default value of the SleepMask parameter.
056: */
057: public static final long DEFAULT_SLEEP_MASK = 0x3ff;
058:
059: /**
060: * The base number of milliseconds to sleep during each sample.
061: */
062: private long sleepTime;
063:
064: /**
065: * A mask to be applied to the current time in order to add a random
066: * component to the sleep time.
067: */
068: private long sleepMask;
069:
070: /**
071: * Default constructor for <code>SleepTest</code>.
072: *
073: * The Java Sampler uses the default constructor to instantiate an instance
074: * of the client class.
075: */
076: public SleepTest() {
077: getLogger().debug(whoAmI() + "\tConstruct");
078: }
079:
080: /**
081: * Do any initialization required by this client. In this case,
082: * initialization consists of getting the values of the SleepTime and
083: * SleepMask parameters. It is generally recommended to do any
084: * initialization such as getting parameter values in the setupTest method
085: * rather than the runTest method in order to add as little overhead as
086: * possible to the test.
087: *
088: * @param context
089: * the context to run with. This provides access to
090: * initialization parameters.
091: */
092: public void setupTest(JavaSamplerContext context) {
093: getLogger().debug(whoAmI() + "\tsetupTest()");
094: listParameters(context);
095:
096: sleepTime = context.getLongParameter("SleepTime",
097: DEFAULT_SLEEP_TIME);
098: sleepMask = context.getLongParameter("SleepMask",
099: DEFAULT_SLEEP_MASK);
100: }
101:
102: /**
103: * Perform a single sample. In this case, this method will simply sleep for
104: * some amount of time. Perform a single sample for each iteration. This
105: * method returns a <code>SampleResult</code> object.
106: * <code>SampleResult</code> has many fields which can be used. At a
107: * minimum, the test should use <code>SampleResult.sampleStart</code> and
108: * <code>SampleResult.sampleEnd</code>to set the time that the test
109: * required to execute. It is also a good idea to set the sampleLabel and
110: * the successful flag.
111: *
112: * @see org.apache.jmeter.samplers.SampleResult#sampleStart()
113: * @see org.apache.jmeter.samplers.SampleResult#sampleEnd()
114: * @see org.apache.jmeter.samplers.SampleResult#setSuccessful(boolean)
115: * @see org.apache.jmeter.samplers.SampleResult#setSampleLabel(String)
116: *
117: * @param context
118: * the context to run with. This provides access to
119: * initialization parameters.
120: *
121: * @return a SampleResult giving the results of this sample.
122: */
123: public SampleResult runTest(JavaSamplerContext context) {
124: SampleResult results = new SampleResult();
125:
126: try {
127: // Record sample start time.
128: results.sampleStart();
129:
130: // Generate a random value using the current time.
131: long start = System.currentTimeMillis();
132: long sleep = getSleepTime() + (start % getSleepMask());
133:
134: results.setSampleLabel("Sleep Test: time = " + sleep);
135:
136: // Execute the sample. In this case sleep for the
137: // specified time.
138: Thread.sleep(sleep);
139:
140: results.setSuccessful(true);
141: } catch (InterruptedException e) {
142: getLogger().warn("SleepTest: interrupted.");
143: results.setSuccessful(true);
144: } catch (Exception e) {
145: getLogger().error("SleepTest: error during sample", e);
146: results.setSuccessful(false);
147: } finally {
148: results.sampleEnd();
149: }
150:
151: if (getLogger().isDebugEnabled()) {
152: getLogger().debug(
153: whoAmI() + "\trunTest()" + "\tTime:\t"
154: + results.getTime());
155: listParameters(context);
156: }
157:
158: return results;
159: }
160:
161: /**
162: * Do any clean-up required by this test. In this case no clean-up is
163: * necessary, but some messages are logged for debugging purposes.
164: *
165: * @param context
166: * the context to run with. This provides access to
167: * initialization parameters.
168: */
169: public void teardownTest(JavaSamplerContext context) {
170: getLogger().debug(whoAmI() + "\tteardownTest()");
171: listParameters(context);
172: }
173:
174: /**
175: * Provide a list of parameters which this test supports. Any parameter
176: * names and associated values returned by this method will appear in the
177: * GUI by default so the user doesn't have to remember the exact names. The
178: * user can add other parameters which are not listed here. If this method
179: * returns null then no parameters will be listed. If the value for some
180: * parameter is null then that parameter will be listed in the GUI with an
181: * empty value.
182: *
183: * @return a specification of the parameters used by this test which should
184: * be listed in the GUI, or null if no parameters should be listed.
185: */
186: public Arguments getDefaultParameters() {
187: Arguments params = new Arguments();
188: params.addArgument("SleepTime", String
189: .valueOf(DEFAULT_SLEEP_TIME));
190: params.addArgument("SleepMask", "0x"
191: + (Long.toHexString(DEFAULT_SLEEP_MASK)).toUpperCase());
192: return params;
193: }
194:
195: /**
196: * Dump a list of the parameters in this context to the debug log.
197: *
198: * @param context
199: * the context which contains the initialization parameters.
200: */
201: private void listParameters(JavaSamplerContext context) {
202: if (getLogger().isDebugEnabled()) {
203: Iterator argsIt = context.getParameterNamesIterator();
204: while (argsIt.hasNext()) {
205: String name = (String) argsIt.next();
206: getLogger().debug(
207: name + "=" + context.getParameter(name));
208: }
209: }
210: }
211:
212: /**
213: * Generate a String identifier of this test for debugging purposes.
214: *
215: * @return a String identifier for this test instance
216: */
217: private String whoAmI() {
218: StringBuffer sb = new StringBuffer();
219: sb.append(Thread.currentThread().toString());
220: sb.append("@");
221: sb.append(Integer.toHexString(hashCode()));
222: return sb.toString();
223: }
224:
225: /**
226: * Get the value of the sleepTime field.
227: *
228: * @return the base number of milliseconds to sleep during each sample.
229: */
230: private long getSleepTime() {
231: return sleepTime;
232: }
233:
234: /**
235: * Get the value of the sleepMask field.
236: *
237: * @return a mask to be applied to the current time in order to add a random
238: * component to the sleep time.
239: */
240: private long getSleepMask() {
241: return sleepMask;
242: }
243: }
|