org.apache.qpid.junit.extensions
Class AsymptoticTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.apache.qpid.junit.extensions.AsymptoticTestCase
All Implemented Interfaces:
junit.framework.Test, InstrumentedTest

public class AsymptoticTestCase
extends junit.framework.TestCase
implements InstrumentedTest

AsymptoticTestCase is an extension of TestCase for writing unit tests to analyze asymptotic time and space behaviour.

ParameterizedTestCases allow tests to be defined which have test methods that take a single int argument. Normal JUnit test methods do not take any arguments. This int argument can be interpreted in any way by the test but it is intended to denote the 'size' of the test to be run. For example, when testing the performance of a data structure for different numbers of data elements held in the data structure the int parameter should be interpreted as the number of elements. Test timings for different numbers of elements can then be captured and the asymptotic behaviour of the data structure with respect to time analyzed. Any non-parameterized tests defined in extensions of this class will also be run.

TestCases derived from this class may also define tear down methods to clean up their memory usage. This is intended to be used in conjunction with memory listeners that report the amount of memory a test uses. The idea is to write a test that allocates memory in the main test method in such a way that it leaves that memory still allocated at the end of the test. The amount of memory used can then be measured before calling the tear down method to clean it up. In the data structure example above, a test will allocate as many elements as are requested by the int parameter and deallocate them in the tear down method. In this way memory readings for different numbers of elements can be captured and the asymptotic behaviour of the data structure with respect to space analyzed.

CRC Card
Responsibilities Collaborations
Store the current int parameter value. TKTestResult and see AsymptoticTestDecorator too.
Invoke parameterized test methods.

Todo:
If possible try to move the code that invokes the test and setup/teardown methods into TKTestResult or AsymptoticTestDecorator rather than this class. This would mean that tests don't have to extend this class to do time and space performance analysis, these methods could be added to any JUnit TestCase class instead. This would be an improvement because existing unit tests wouldn't have to extend a different class to work with this extension, and also tests that extend other junit extension classes could have parameterized and tear down methods too.

Field Summary
(package private)  ThreadLocal<org.apache.qpid.junit.extensions.AsymptoticTestCase.TestMeasurements> threadLocalMeasurement
          Thread local for holding measurements on a per thread basis.
 
Constructor Summary
AsymptoticTestCase(String name)
          Constructs a test case with the given name.
 
Method Summary
 int getN()
          Gets the current value of the integer parameter to be passed to the parameterized test.
 long getTestEndMemory()
          Reports the memory usage at the end of the test.
 long getTestStartMemory()
          Reports the memory usage at the start of the test.
 long getTestTime()
          Reports how long the test took to run.
 void reset()
          Resets the instrumentation values to zero, and nulls any references to held measurements so that the memory can be reclaimed.
protected  void runTest()
          Runs the test method for this test case.
 void setN(int n)
          Sets the current value of the integer parameter to be passed to the parameterized test.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, setName, setUp, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface junit.framework.Test
countTestCases, run
 

Field Detail

threadLocalMeasurement

ThreadLocal<org.apache.qpid.junit.extensions.AsymptoticTestCase.TestMeasurements> threadLocalMeasurement
Thread local for holding measurements on a per thread basis.

Constructor Detail

AsymptoticTestCase

public AsymptoticTestCase(String name)
Constructs a test case with the given name.

Parameters:
name - The name of the test.
Method Detail

getN

public int getN()
Gets the current value of the integer parameter to be passed to the parameterized test.

Returns:
The current value of the integer parameter.

setN

public void setN(int n)
Sets the current value of the integer parameter to be passed to the parameterized test.

Parameters:
n - The new current value of the integer parameter.

getTestTime

public long getTestTime()
Reports how long the test took to run.

Specified by:
getTestTime in interface InstrumentedTest
Returns:
The time in milliseconds that the test took to run.

getTestStartMemory

public long getTestStartMemory()
Reports the memory usage at the start of the test.

Specified by:
getTestStartMemory in interface InstrumentedTest
Returns:
The memory usage at the start of the test.

getTestEndMemory

public long getTestEndMemory()
Reports the memory usage at the end of the test.

Specified by:
getTestEndMemory in interface InstrumentedTest
Returns:
The memory usage at the end of the test.

reset

public void reset()
Resets the instrumentation values to zero, and nulls any references to held measurements so that the memory can be reclaimed.

Specified by:
reset in interface InstrumentedTest

runTest

protected void runTest()
                throws Throwable
Runs the test method for this test case.

Overrides:
runTest in class junit.framework.TestCase
Throws:
Throwable - Any Throwables from the test methods invoked are allowed to fall through.


Licensed to the Apache Software Foundation