Class Benchmark

java.lang.Object
com.randomnoun.common.timer.Benchmark

public class Benchmark extends Object
An object that performs time benchmarking.

This object contains a reference to all benchmark instances that are currently executing in the VM, referenced by benchId. It is encouraged that each individual request has a unique benchId, although this is not required unless you need to use the getBenchmark(java.lang.String) method. (if multiple instances are created using the same benchId, only the most recent instance will be retrievable via getBenchmark()).

Multiple checkpoints can be set during the course of a benchmark, which will be written to disk after completion.

Output is generated only after the benchmark has completed. All output is buffered, and references to any open benchmark files are kept within this object in order to minimise the amount of overhead that benchmarking will impose.

This class does not expire benchmarks, and therefore may cause memory leaks if benchmarks are not closed correctly.

Output is of the form:

   benchId1,begin,timestamp,pointID1,timestamp1,pointID1,timestamp2,[...],end,timestampn,duration
   benchId2,begin,timestamp,pointID1,timestamp1,pointID1,timestamp2,[...],end,timestampn,duration
   :
   :
 

where benchId is the benchId indentifying this benchmark, and pointIDn are the individual checkpoint identifiers. Each line finishes with the text ",duration," followed by the duration of the entire benchmark, in milliseconds. Timestamps are displayed in the format defined by the setDateFormat(String) method.

There is no current way of following a benchmark through the EJB boundary layer; to do so would currently involve overriding the User object, which I'm not terribly keen to do. It is possible to set up a new Benchmark on the EJB side, and track performance independently over there. It should then be a simple matter to heuristically match up individual HTTP requests/struts actions and the EJB methods that they invoke (under normal load conditions). It's important, however, that two Benchmark engines in the same VM do not write to the same file, as they will corrupt each other's output.

Author:
knoxg
  • Field Details

    • checkpointList

      the set of checkpoints for this benchmark instance
    • startTime

      protected long startTime
      Start time, in milliseconds
    • startHiresTimestamp

      protected long startHiresTimestamp
      HiRes Timer token for start of benchmark
    • active

      protected boolean active
      True if benchmark is active, false if the benchmark has completed.
  • Constructor Details

    • Benchmark

      public Benchmark(String benchId, String filename)
      Instantiates a new benchmark. Automatically creates a 'begin' checkpoint for this new instance.
  • Method Details

    • setBenchId

      public void setBenchId(String benchId)
      Alters the ID for a benchmark that is already running.
    • checkpoint

      public void checkpoint(String pointID)
      Performs checkpoint.
    • annotate

      public void annotate(String annotation)
      Creates a benchmark annotation
    • toString

      public String toString()
      Returns a string representation of this benchmark
      Overrides:
      toString in class Object
    • end

      public void end() throws IOException
      Completes benchmark and writes to disk
      Throws:
      IOException
    • isActive

      public boolean isActive()
      Returns whether this benchmark is currently active
    • cancel

      public void cancel()
      Cancel this benchmark
    • getCheckpointList

      Returns all checkpoints stored in this benchmark
    • getId

      public String getId()
      Returns the benchmark ID
    • getBenchmark

      public static Benchmark getBenchmark(String benchId)
      Returns the benchmark with the supplied ID
    • setDateFormat

      public static void setDateFormat(String dateFormatText)
      Sets the date format for all output of this class. The date format is represented as a string, rather than a DateFormat object, since most SimpleDateFormats are not thread-safe.
    • flushWriters

      public static void flushWriters() throws IOException
      Flushes all writers within this benchmark object (commits any unwritten data to disk). Files are still kept open.
      Throws:
      IOException
    • closeWriters

      public static void closeWriters() throws IOException
      Closes all writers within this benchmark object. Files will be automatically reopened for any running benchmarks.
      Throws:
      IOException
    • toJson

      public String toJson()