uk.co.thebadgerset.junit.extensions.example.TimingExampleTestPerf Maven / Gradle / Ivy
Go to download
JUnit Toolkit enhances JUnit with performance testing, asymptotic behaviour analysis, and concurrency testing.
/* Copyright Rupert Smith, 2005 to 2007, all rights reserved. */
package uk.co.thebadgerset.junit.extensions.example;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.log4j.Logger;
import uk.co.thebadgerset.junit.extensions.AsymptoticTestCase;
import uk.co.thebadgerset.junit.extensions.TestThreadAware;
import uk.co.thebadgerset.junit.extensions.TimingController;
import uk.co.thebadgerset.junit.extensions.TimingControllerAware;
import uk.co.thebadgerset.junit.extensions.util.ParsedProperties;
import uk.co.thebadgerset.junit.extensions.util.TestContextProperties;
import uk.co.thebadgerset.junit.extensions.util.TestUtils;
/**
* TimingExampleTest implements some short pauses that test invocations can try out as dummy test material.
* The pause time per test, or per setup method, or per thread setup can be configured by setting properties.
*
* CRC Card
* Responsibilities Collaborations
* Pause a specified number of times.
*
*
* @author Rupert Smith
*/
public class TimingExampleTestPerf extends AsymptoticTestCase implements TestThreadAware
{
/** Used for logging. */
private static final Logger log = Logger.getLogger(TimingExampleTestPerf.class);
/** The test pause property name. */
private static final String SLEEP_TIME_PROPNAME = "testPause";
/** The default time to sleep in the test wait loop. */
private static final long SLEEP_TIME_DEFAULT = 100;
/** The setup pause property name. */
private static final String SLEEP_TIME_SETUP_PROPNAME = "setupPause";
/** The default time to sleep during the setup method. */
private static final long SLEEP_TIME_SETUP_DEFAULT = 0;
/** The per thread setup pause property name. */
private static final String SLEEP_TIME_SETUP_THREAD_PROPNAME = "setupThreadPause";
/** The default time to sleep during the setup per thread. */
private static final long SLEEP_TIME_SETUP_THREAD_DEFAULT = 0;
/** Holds the default test parameters. */
private ParsedProperties defaults = new ParsedProperties();
/** Holds the time to sleep in the test wait loop. */
private Long testSleepTime = defaults.setPropertyIfNull(SLEEP_TIME_PROPNAME, SLEEP_TIME_DEFAULT);
/** Holds the time to sleep during the setup method. */
private Long setupSleepTime = defaults.setPropertyIfNull(SLEEP_TIME_SETUP_PROPNAME, SLEEP_TIME_SETUP_DEFAULT);
/** Holds the time to sleep during the thread setup method. */
private Long threadSetupSleepTime =
defaults.setPropertyIfNull(SLEEP_TIME_SETUP_THREAD_PROPNAME, SLEEP_TIME_SETUP_THREAD_DEFAULT);
/** Used to read the tests configurable properties through. */
ParsedProperties testProps = TestContextProperties.getInstance(defaults);
/**
* Creates a new example test case object.
*
* @param name The name of the test.
*/
public TimingExampleTestPerf(String name)
{
super(name);
}
/**
* Compiles all the tests in this class into a suite.
*
* @return The test suite.
*/
public static Test suite()
{
// Build a new test suite
TestSuite suite = new TestSuite("Timing Example Tests");
suite.addTest(new TimingExampleTestPerf("testShortWait"));
// suite.addTest(new TimingExampleTestPerf("testVeryShortWait"));
return suite;
}
/**
* Does n short waits.
*
* @param n The number of waits.
*/
public void testShortWait(int n)
{
log.debug("public void testShortWait(int " + n + "): called");
long sleepTime = testSleepTime;
for (int i = 0; i < n; i++)
{
try
{
Thread.sleep(sleepTime);
}
catch (InterruptedException e)
{ }
}
}
/**
* Loops n times but does very little.
*
* @param n The number of waits.
*/
public void testVeryShortWait(int n)
{
log.debug("public void testVeryShortWait(int " + n + ")");
for (int i = 0; i < n; i++)
{
// Execute a few instructions, but nothing that takes very long...
Math.pow(Math.E, i);
}
}
/**
* Setup that can be configured to pause for a short time.
*/
protected void setUp()
{
testSleepTime = testProps.getPropertyAsLong(SLEEP_TIME_PROPNAME);
setupSleepTime = testProps.getPropertyAsLong(SLEEP_TIME_SETUP_PROPNAME);
TestUtils.pause(setupSleepTime);
}
/**
* Called when a test thread is created.
*/
public void threadSetUp()
{
threadSetupSleepTime = testProps.getPropertyAsLong(SLEEP_TIME_SETUP_THREAD_PROPNAME);
TestUtils.pause(threadSetupSleepTime);
}
/**
* Called when a test thread is destroyed.
*/
public void threadTearDown()
{ }
}