KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > java > test > SleepTest


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/test/SleepTest.java,v 1.11.2.1 2004/04/29 14:44:12 sebb 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 JavaDoc;
21 import java.util.Iterator JavaDoc;
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
30  * JMeter Java protocol client. The class implements the
31  * <code>JavaSamplerClient</code> interface.
32  * <p>
33  * During each sample, this client will sleep for some amount of
34  * time. The amount of time to sleep is determined from the
35  * two parameters SleepTime and SleepMask using the formula:
36  * <pre>
37  * totalSleepTime = SleepTime + (System.currentTimeMillis() % SleepMask)
38  * </pre>
39  * Thus, the SleepMask provides a way to add a random component
40  * to the sleep time.
41  *
42  * @author <a HREF="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
43  * @version $Revision: 1.11.2.1 $
44  */

45 public class SleepTest
46     extends AbstractJavaSamplerClient
47     implements Serializable JavaDoc
48 {
49     /**
50      * The default value of the SleepTime parameter, in milliseconds.
51      */

52     public static final long DEFAULT_SLEEP_TIME = 1000;
53
54     /**
55      * The default value of the SleepMask parameter.
56      */

57     public static final long DEFAULT_SLEEP_MASK = 0x3ff;
58
59     /**
60      * The base number of milliseconds to sleep during each sample.
61      */

62     private long sleepTime;
63
64     /**
65      * A mask to be applied to the current time in order to add a
66      * random component to the sleep time.
67      */

68     private long sleepMask;
69
70     /**
71      * Default constructor for <code>SleepTest</code>.
72      *
73      * The Java Sampler uses the default constructor to instantiate
74      * an instance of the client class.
75      */

76     public SleepTest()
77     {
78         getLogger().debug(whoAmI() + "\tConstruct");
79     }
80
81     /**
82      * Do any initialization required by this client. In this case,
83      * initialization consists of getting the values of the SleepTime
84      * and SleepMask parameters. It is generally recommended to do
85      * any initialization such as getting parameter values in the
86      * setupTest method rather than the runTest method in order to
87      * add as little overhead as possible to the test.
88      *
89      * @param context the context to run with. This provides access
90      * to initialization parameters.
91      */

92     public void setupTest(JavaSamplerContext context)
93     {
94         getLogger().debug(whoAmI() + "\tsetupTest()");
95         listParameters(context);
96
97         sleepTime = context.getLongParameter("SleepTime", DEFAULT_SLEEP_TIME);
98         sleepMask = context.getLongParameter("SleepMask", DEFAULT_SLEEP_MASK);
99     }
100
101     /**
102      * Perform a single sample. In this case, this method will
103      * simply sleep for some amount of time.
104      * Perform a single sample for each iteration. This method
105      * returns a <code>SampleResult</code> object.
106      * <code>SampleResult</code> has many fields which can be
107      * used. At a minimum, the test should use
108      * <code>SampleResult.sampleStart</code> and
109      * <code>SampleResult.sampleEnd</code>to set the time that
110      * the test required to execute. It is also a good idea to
111      * set the sampleLabel and the successful flag.
112      *
113      * @see org.apache.jmeter.samplers.SampleResult#sampleStart()
114      * @see org.apache.jmeter.samplers.SampleResult#sampleEnd()
115      * @see org.apache.jmeter.samplers.SampleResult#setSuccessful(boolean)
116      * @see org.apache.jmeter.samplers.SampleResult#setSampleLabel(String)
117      *
118      * @param context the context to run with. This provides access
119      * to initialization parameters.
120      *
121      * @return a SampleResult giving the results of this
122      * sample.
123      */

124     public SampleResult runTest(JavaSamplerContext context)
125     {
126         SampleResult results = new SampleResult();
127
128         try
129         {
130             // Record sample start time.
131
results.sampleStart();
132             
133             // Generate a random value using the current time.
134
long start = System.currentTimeMillis();
135             long sleep = getSleepTime() + (start % getSleepMask());
136
137             results.setSampleLabel(
138                     "Sleep Test: time = " + sleep);
139             
140             // Execute the sample. In this case sleep for the
141
// specified time.
142
Thread.sleep(sleep);
143
144             results.setSuccessful(true);
145         }
146         catch (InterruptedException JavaDoc e)
147         {
148             getLogger().warn("SleepTest: interrupted.");
149             results.setSuccessful(true);
150         }
151         catch (Exception JavaDoc e)
152         {
153             getLogger().error("SleepTest: error during sample", e);
154             results.setSuccessful(false);
155         }
156         finally
157         {
158             results.sampleEnd();
159         }
160
161         if (getLogger().isDebugEnabled())
162         {
163             getLogger().debug(
164                 whoAmI() + "\trunTest()" + "\tTime:\t" + results.getTime());
165             listParameters(context);
166         }
167
168         return results;
169     }
170
171     /**
172      * Do any clean-up required by this test. In this case no
173      * clean-up is necessary, but some messages are logged for
174      * debugging purposes.
175      *
176      * @param context the context to run with. This provides access
177      * to initialization parameters.
178      */

179     public void teardownTest(JavaSamplerContext context)
180     {
181         getLogger().debug(whoAmI() + "\tteardownTest()");
182         listParameters(context);
183     }
184
185     /**
186      * Provide a list of parameters which this test supports. Any
187      * parameter names and associated values returned by this method
188      * will appear in the GUI by default so the user doesn't have
189      * to remember the exact names. The user can add other parameters
190      * which are not listed here. If this method returns null then
191      * no parameters will be listed. If the value for some parameter
192      * is null then that parameter will be listed in the GUI with
193      * an empty value.
194      *
195      * @return a specification of the parameters used by this
196      * test which should be listed in the GUI, or null
197      * if no parameters should be listed.
198      */

199     public Arguments getDefaultParameters()
200     {
201         Arguments params = new Arguments();
202         params.addArgument("SleepTime", String.valueOf(DEFAULT_SLEEP_TIME));
203         params.addArgument(
204             "SleepMask",
205             "0x" + (Long.toHexString(DEFAULT_SLEEP_MASK)).toUpperCase());
206         return params;
207     }
208
209     /**
210      * Dump a list of the parameters in this context to the debug log.
211      *
212      * @param context the context which contains the initialization
213      * parameters.
214      */

215     private void listParameters(JavaSamplerContext context)
216     {
217         if (getLogger().isDebugEnabled())
218         {
219             Iterator JavaDoc argsIt = context.getParameterNamesIterator();
220             while (argsIt.hasNext())
221             {
222                 String JavaDoc name = (String JavaDoc) argsIt.next();
223                 getLogger().debug(name + "=" + context.getParameter(name));
224             }
225         }
226     }
227
228     /**
229      * Generate a String identifier of this test for debugging
230      * purposes.
231      *
232      * @return a String identifier for this test instance
233      */

234     private String JavaDoc whoAmI()
235     {
236         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
237         sb.append(Thread.currentThread().toString());
238         sb.append("@");
239         sb.append(Integer.toHexString(hashCode()));
240         return sb.toString();
241     }
242
243     /**
244      * Get the value of the sleepTime field.
245      *
246      * @return the base number of milliseconds to sleep during
247      * each sample.
248      */

249     private long getSleepTime()
250     {
251         return sleepTime;
252     }
253
254     /**
255      * Get the value of the sleepMask field.
256      *
257      * @return a mask to be applied to the current time in order
258      * to add a random component to the sleep time.
259      */

260     private long getSleepMask()
261     {
262         return sleepMask;
263     }
264 }
265
Popular Tags