javolution.testing
Class TimeContext

Object
  extended by Context
      extended by LogContext
          extended by TestContext
              extended by TimeContext
All Implemented Interfaces:
Serializable, XMLSerializable

public abstract class TimeContext
extends TestContext

This class represents a test context specialized for measuring execution time.

TimeContext implementations may perform assertions based upon the execution time. For example:

 
     class MyTestCase extends TestCase() {
          ...
          protected void validate() {
              long ns = TimeContext.getAverageTime("ns");
              TestContext.assertTrue(ns < 100); // Error if execution time is more than 100 ns.
              ... 
          }
     }

Version:
5.2, August 5, 2007
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Field Summary
static Configurable<Class<? extends TimeContext>> DEFAULT
          Holds the time context default implementation (by default logs average and minimum execution time to System.out).
static Class<? extends TimeContext> REGRESSION
          Holds an implementation which does not perform any logging but raises an AssertionException when an assertion fails, including any timing assertion.
static Configurable<Integer> TEST_DURATION_MS
          Holds the minimum duration of each test execution (default 1000 ms).
 
Fields inherited from class TestContext
CONSOLE
 
Fields inherited from class LogContext
NULL, STANDARD, SYSTEM_OUT
 
Fields inherited from class Context
ROOT
 
Constructor Summary
TimeContext()
           
 
Method Summary
protected  void doRun(TestCase testCase)
          Benchmarks the specified test case and logs the results.
static void enter()
          Enters the DEFAULT time context.
static void exit()
          Exits the current time context.
static long getAverageTime(String unit)
          Returns the average execution time of the latest execution performed or -1 if the current context is not a time context.
 long getAverageTimeInPicoSeconds()
          Returns the average execution time of the latest execution stated in pico-seconds.
static long getMaximumTime(String unit)
          Returns the maximum execution time of the latest execution performed or -1 if the current context is not a time context.
 long getMaximumTimeInPicoSeconds()
          Returns the maximmum execution time of the latest execution stated in pico-seconds.
static long getMinimumTime(String unit)
          Returns the minimum execution time of the latest execution performed or -1 if the current context is not a time context.
 long getMinimumTimeInPicoSeconds()
          Returns the minimum execution time of the latest execution stated in pico-seconds.
 
Methods inherited from class TestContext
assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertException, assertException, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, doAssert, doRun, fail, fail, run, run
 
Methods inherited from class LogContext
debug, debug, debug, enterAction, error, error, error, error, error, error, error, exitAction, getCurrentLogContext, getDefault, info, info, info, isDebugLogged, isErrorLogged, isInfoLogged, isLogged, isWarningLogged, logDebug, logError, logInfo, logMessage, logWarning, warning, warning, warning
 
Methods inherited from class Context
enter, enter, exit, exit, getCurrentContext, getOuter, getOwner, setConcurrentContext, toString
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

REGRESSION

public static final Class<? extends TimeContext> REGRESSION
Holds an implementation which does not perform any logging but raises an AssertionException when an assertion fails, including any timing assertion.


TEST_DURATION_MS

public static final Configurable<Integer> TEST_DURATION_MS
Holds the minimum duration of each test execution (default 1000 ms). The larger the number the more accurate is the average time result; but the longer it takes to run the tests.


DEFAULT

public static final Configurable<Class<? extends TimeContext>> DEFAULT
Holds the time context default implementation (by default logs average and minimum execution time to System.out).

Constructor Detail

TimeContext

public TimeContext()
Method Detail

enter

public static void enter()
Enters the DEFAULT time context.


exit

public static void exit()
Exits the current time context.

Throws:
ClassCastException - if the context is not a time context.

getMinimumTime

public static long getMinimumTime(String unit)
Returns the minimum execution time of the latest execution performed or -1 if the current context is not a time context.

Parameters:
unit - one of "s", "ms", "us", "ns", "ps"
Returns:
the minimum execution time stated in the specified unit.

getAverageTime

public static long getAverageTime(String unit)
Returns the average execution time of the latest execution performed or -1 if the current context is not a time context.

Parameters:
unit - one of "s", "ms", "us", "ns", "ps"
Returns:
the average execution time stated in the specified unit.

getMaximumTime

public static long getMaximumTime(String unit)
Returns the maximum execution time of the latest execution performed or -1 if the current context is not a time context.

Parameters:
unit - one of "s", "ms", "us", "ns", "ps"
Returns:
the maximum execution time stated in the specified unit.

getMinimumTimeInPicoSeconds

public long getMinimumTimeInPicoSeconds()
Returns the minimum execution time of the latest execution stated in pico-seconds.

Returns:
the time in pico-seconds.

getAverageTimeInPicoSeconds

public long getAverageTimeInPicoSeconds()
Returns the average execution time of the latest execution stated in pico-seconds.

Returns:
the time in pico-seconds.

getMaximumTimeInPicoSeconds

public long getMaximumTimeInPicoSeconds()
Returns the maximmum execution time of the latest execution stated in pico-seconds.

Returns:
the time in pico-seconds.

doRun

protected void doRun(TestCase testCase)
              throws Exception
Benchmarks the specified test case and logs the results.

Overrides:
doRun in class TestContext
Parameters:
testCase - the test case being executed if not marked ignored.
Throws:
Exception


Copyright © 2005-2012 Javolution. All Rights Reserved.