Package com.oss.util

Class Benchmark

java.lang.Object
com.oss.util.Benchmark

public class Benchmark extends Object
The Benchmark is an utility class that can be used for timing the coder.encode() and coder.decode() method calls. A Benchmark class instance is initialized with instances of com.oss.asn1.AbstractData and com.oss.asn1.Coder classes. Invoking the run() method on the instance will run two coder.encode() and coder.decode() loops in order to determine average time needed for the specified coder to encode and to decode the data. After running the test the timing values can be obtained with the averageEncodeTiming() and averageDecodeTiming() method calls.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Benchmark(byte[] message, AbstractData type, Coder coder)
    Construct new performance test which will use specified binary encoding of the value of type and Coder object.
    Benchmark(AbstractData value, Coder coder)
    Construct new performance test which will use specified AbstractData object and Coder object.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Returns average decode() CPU time per iteration in milliseconds or NaN if CPU time measurement is not available.
    double
    Returns average decode() timing per iteration in milliseconds.
    double
    Returns average decode() time in user mode per iteration in milliseconds or NaN if user time measurement is not available.
    double
    Returns average encode() CPU time per iteration in milliseconds or NaN if CPU time measurement is not available.
    double
    Returns average encode() timing per iteration in milliseconds.
    double
    Returns average encode() time in user mode per iteration in milliseconds or NaN if user time measurement is not available.
    Returns the exception object indicating an abnormal termination of the decode() timing loop.
    Returns the exception object indicating an abnormal termination of the encode() timing loop.
    int
    Returns the current setting for the number of samples to collect.
    numberOfSamples(int samples)
    Sets the number of samples to collect.
    void
    run()
    Runs encoder and decoder performance tests.
    int
    Returns the current setting for the sampling interval.
    samplingTime(int milliseconds)
    Sets the duration of sampling interval.
    void
    setNumberOfSamples(int samples)
    Deprecated. 
    void
    setSamplingTime(int milliseconds)
    Deprecated. 
    void
    setWarmupTime(int milliseconds)
    Deprecated. 
    boolean
    Reports whether progress messages are enabled.
    verbose(boolean enable)
    Enables progress messages while the benchmark is executing.
    int
    Returns the current setting for the warmup timeout of the performance test loop.
    warmupTime(int milliseconds)
    Sets the warmup timeout for the performance test loop.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Benchmark

      public Benchmark(AbstractData value, Coder coder)
      Construct new performance test which will use specified AbstractData object and Coder object.
      Parameters:
      value - test data value. Times required to encode() and decode() the value will be measured.
      coder - coder to be used for encoding and decoding the value.
    • Benchmark

      public Benchmark(byte[] message, AbstractData type, Coder coder)
      Construct new performance test which will use specified binary encoding of the value of type and Coder object.
      Parameters:
      message - test binary message. Times required to decode() and encode() the value will be measured.
      type - empty instance of the AbstractData that identifies the type of the binary message.
      coder - coder to be used for encoding and decoding the value.
  • Method Details

    • run

      public void run()
      Runs encoder and decoder performance tests. Each test includes running encoder or decoder in a loop for the specified time interval. There are two loops: warmup loop and sampling loop. First the warmup loop is run (by default for 5 seconds) to allow the JVM cache to settle down. Next sampling loop is run (by default for 5 seconds) and number of iterations is counted. The number of iterations is then used to compute average encoding or decoding times. The measured times can be then obtained by calling the averageEncodeTiming() and averageDecodeTiming() methods. The methods will return Double.NaN value in case of encoder or decoder error. The Exception instance which caused encoder or decoder error can be queried by the encodeExitStatus() or decodeExitStatus() methods. The warmup and sampling intervals are settable setWarmupTime() and setSamplingTime() methods. By default run() takes about 20 seconds to execute.
    • samplingTime

      public int samplingTime()
      Returns the current setting for the sampling interval.
      Returns:
      the current setting for the sampling interval (milliseconds).
    • setSamplingTime

      @Deprecated public void setSamplingTime(int milliseconds)
      Deprecated.
      Sets the duration of sampling interval. Shorter durations sacrifice the accuracy. Longer durations result in longer running time. The optimal setting is somewhere between 2000 and 20000 milliseconds. By default, the loop duration of 5000 milliseconds is used. This method has been replaced by the samplingTime(int) method.
      Parameters:
      milliseconds - the duration of the sampling interval in milliseconds.
    • samplingTime

      public Benchmark samplingTime(int milliseconds)
      Sets the duration of sampling interval. Shorter durations sacrifice the accuracy. Longer durations result in longer running time. The optimal setting is somewhere between 2000 and 20000 milliseconds. By default, the loop duration of 5000 milliseconds is used.
      Parameters:
      milliseconds - the duration of the sampling interval in milliseconds.
      Returns:
      this object.
    • warmupTime

      public int warmupTime()
      Returns the current setting for the warmup timeout of the performance test loop.
      Returns:
      the current setting for the warmup timeout of the performance loop (milliseconds).
    • setWarmupTime

      @Deprecated public void setWarmupTime(int milliseconds)
      Deprecated.
      Sets the warmup timeout for the performance test loop. At the start of the loop the JVM spends many CPU cycles to compile the bytecodes to native machine instructions. The impact of these aside expenses on the accuracy of timing is dramatic. The warmup timeout instructs the benchmark to run the test loop for the specified number of milliseconds but avoid collecting any performance data at that time. The warmup timeout gives the JVM a chance to complete the compilation of the bytecodes being profiled before the profiler will start to collect the profiling data. Normally, the setting of 5000 - 10000 milliseconds (5-10 seconds) is enough to let the JVM complete with the compilation of the bytecodes. By default, 5000 milliseconds is used. This method has been replaced by the warmupTime(int) method.
      Parameters:
      milliseconds - the number of milliseconds to "warmup" the timing loop before collecting the data.
    • warmupTime

      public Benchmark warmupTime(int milliseconds)
      Sets the warmup timeout for the performance test loop. At the start of the loop the JVM spends many CPU cycles to compile the bytecodes to native machine instructions. The impact of these aside expenses on the accuracy of timing is dramatic. The warmup timeout instructs the benchmark to run the test loop for the specified number of milliseconds but avoid collecting any performance data at that time. The warmup timeout gives the JVM a chance to complete the compilation of the bytecodes being profiled before the profiler will start to collect the profiling data. Normally, the setting of 5000 - 10000 milliseconds (5-10 seconds) is enough to let the JVM complete with the compilation of the bytecodes. By default, 5000 milliseconds is used.
      Parameters:
      milliseconds - the number of milliseconds to "warmup" the timing loop before collecting the data.
      Returns:
      this object.
    • numberOfSamples

      public int numberOfSamples()
      Returns the current setting for the number of samples to collect.
      Returns:
      the current setting for the number of samples to collect.
    • setNumberOfSamples

      @Deprecated public void setNumberOfSamples(int samples)
      Deprecated.
      Sets the number of samples to collect. Bigger values yield better accuracy but require the profiler to run longer. In most cases the optimal setting is in the range 5..10. This method has been replaced by the numberOfSamples(int) method.
      Parameters:
      samples - the number of samples to collect.
    • numberOfSamples

      public Benchmark numberOfSamples(int samples)
      Sets the number of samples to collect. Bigger values yield better accuracy but require the profiler to run longer. In most cases the optimal setting is in the range 5..10.
      Parameters:
      samples - the number of samples to collect.
      Returns:
      this object.
    • verbose

      public boolean verbose()
      Reports whether progress messages are enabled.
      Returns:
      true if progress messages are enabled.
    • verbose

      public Benchmark verbose(boolean enable)
      Enables progress messages while the benchmark is executing.
      Parameters:
      enable - enables or disables progress messages.
      Returns:
      this object.
    • encodeExitStatus

      public Exception encodeExitStatus()
      Returns the exception object indicating an abnormal termination of the encode() timing loop. If the encode loop has terminated abnormally, say, due to an exception has been thrown, this method returns the exception object, that caused the abnormal termination.
      Returns:
      the exit status of the encode() operation.
    • decodeExitStatus

      public Exception decodeExitStatus()
      Returns the exception object indicating an abnormal termination of the decode() timing loop. If the decode loop has terminated abnormally, say, due to an exception has been thrown, this method returns the exception object, that caused the abnormal termination.
      Returns:
      the exit status of the decode() operation.
    • averageEncodeTiming

      public double averageEncodeTiming()
      Returns average encode() timing per iteration in milliseconds.
      Returns:
      average timing per encode iteration or Double.NaN if encode() failed.
    • averageEncodeCPUTiming

      public double averageEncodeCPUTiming()
      Returns average encode() CPU time per iteration in milliseconds or NaN if CPU time measurement is not available.
      Returns:
      average CPU time per encode iteration or Double.NaN if encode() failed or CPU time measurement is not available.
    • averageEncodeUserTiming

      public double averageEncodeUserTiming()
      Returns average encode() time in user mode per iteration in milliseconds or NaN if user time measurement is not available.
      Returns:
      average timing per encode iteration or Double.NaN if encode() failed or user time measurement is not available.
    • averageDecodeTiming

      public double averageDecodeTiming()
      Returns average decode() timing per iteration in milliseconds.
      Returns:
      average timing per encode iteration or Double.NaN if encode() or decode() failed.
    • averageDecodeCPUTiming

      public double averageDecodeCPUTiming()
      Returns average decode() CPU time per iteration in milliseconds or NaN if CPU time measurement is not available.
      Returns:
      average timing per encode iteration or Double.NaN if encode() or decode() failed or CPU time measurement is not available.
    • averageDecodeUserTiming

      public double averageDecodeUserTiming()
      Returns average decode() time in user mode per iteration in milliseconds or NaN if user time measurement is not available.
      Returns:
      average timing per encode iteration or Double.NaN if encode() or decode() failed or user time measurement is not available.