org.apache.jmeter.protocol.java.test
public class: SleepTest [javadoc |
source]
java.lang.Object
org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient
org.apache.jmeter.protocol.java.test.SleepTest
All Implemented Interfaces:
Serializable, JavaSamplerClient
The
SleepTest
class is a simple example class for a JMeter
Java protocol client. The class implements the
JavaSamplerClient
interface.
During each sample, this client will sleep for some amount of time. The
amount of time to sleep is determined from the two parameters SleepTime and
SleepMask using the formula:
totalSleepTime = SleepTime + (System.currentTimeMillis() % SleepMask)
Thus, the SleepMask provides a way to add a random component to the sleep
time.
Field Summary |
---|
public static final long | DEFAULT_SLEEP_TIME | The default value of the SleepTime parameter, in milliseconds. |
public static final long | DEFAULT_SLEEP_MASK | The default value of the SleepMask parameter. |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from org.apache.jmeter.protocol.java.test.SleepTest Detail: |
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("SleepTime", String.valueOf(DEFAULT_SLEEP_TIME));
params.addArgument("SleepMask", "0x" + (Long.toHexString(DEFAULT_SLEEP_MASK)).toUpperCase());
return params;
}
Provide a list of parameters which this test supports. Any parameter
names and associated values returned by this method will appear in the
GUI by default so the user doesn't have to remember the exact names. The
user can add other parameters which are not listed here. If this method
returns null then no parameters will be listed. If the value for some
parameter is null then that parameter will be listed in the GUI with an
empty value. |
public SampleResult runTest(JavaSamplerContext context) {
SampleResult results = new SampleResult();
try {
// Record sample start time.
results.sampleStart();
// Generate a random value using the current time.
long start = System.currentTimeMillis();
long sleep = getSleepTime() + (start % getSleepMask());
results.setSampleLabel("Sleep Test: time = " + sleep);
// Execute the sample. In this case sleep for the
// specified time.
Thread.sleep(sleep);
results.setSuccessful(true);
} catch (InterruptedException e) {
getLogger().warn("SleepTest: interrupted.");
results.setSuccessful(true);
} catch (Exception e) {
getLogger().error("SleepTest: error during sample", e);
results.setSuccessful(false);
} finally {
results.sampleEnd();
}
if (getLogger().isDebugEnabled()) {
getLogger().debug(whoAmI() + "\trunTest()" + "\tTime:\t" + results.getTime());
listParameters(context);
}
return results;
}
Perform a single sample. In this case, this method will simply sleep for
some amount of time. Perform a single sample for each iteration. This
method returns a SampleResult object.
SampleResult has many fields which can be used. At a
minimum, the test should use SampleResult.sampleStart and
SampleResult.sampleEnd to set the time that the test
required to execute. It is also a good idea to set the sampleLabel and
the successful flag. |
public void setupTest(JavaSamplerContext context) {
getLogger().debug(whoAmI() + "\tsetupTest()");
listParameters(context);
sleepTime = context.getLongParameter("SleepTime", DEFAULT_SLEEP_TIME);
sleepMask = context.getLongParameter("SleepMask", DEFAULT_SLEEP_MASK);
}
Do any initialization required by this client. In this case,
initialization consists of getting the values of the SleepTime and
SleepMask parameters. It is generally recommended to do any
initialization such as getting parameter values in the setupTest method
rather than the runTest method in order to add as little overhead as
possible to the test. |
public void teardownTest(JavaSamplerContext context) {
getLogger().debug(whoAmI() + "\tteardownTest()");
listParameters(context);
}
Do any clean-up required by this test. In this case no clean-up is
necessary, but some messages are logged for debugging purposes. |