Class TPS

java.lang.Object
org.jpos.util.TPS
All Implemented Interfaces:
AutoCloseable, Loggeable

public class TPS extends Object implements Loggeable, AutoCloseable
TPS can be used to measure Transactions Per Second (or transactions during other period of time).

It can operate in two different modes:

  • Auto update.
  • Manual update.

When operating in auto update mode, a shared scheduler is used and the number of transactions (calls to tick()) is automatically calculated for every period. In this mode, callers should invoke stop() (or close()) when the TPS object is no longer needed to cancel its scheduled task. The shared scheduler thread is daemon and reused across instances.

When operating in manual update mode, user has to call one of its floatValue() or intValue() method at regular intervals. The returned value will be the average TPS for the given period since the last call.

Since:
1.6.7 r2912
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected long
    If set (via setSimulatedNanoTime), getNanoTime() returns this value.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TPS()
    Default constructor.
    TPS(boolean autoupdate)
    Creates a TPS meter with the default one-second period.
    TPS(long period, boolean autoupdate)
    Creates a TPS meter with the given sampling period.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    dump(PrintStream p, String indent)
    Dumps a human-readable representation of this object to the print stream.
    float
    Returns the current transactions-per-second value.
    float
    Returns the average TPS across sampled intervals.
    long
    Returns elapsed wall-clock time since the current sampling window started.
    protected long
    Returns the current monotonic time in nanoseconds.
    int
    Returns the highest recorded TPS peak.
    long
    Returns the wall-clock time when the peak TPS was recorded.
    long
    Returns the configured sampling period.
    int
    Returns the current transactions-per-second value rounded to an integer.
    void
    resets average and peak.
    void
    setSimulatedNanoTime(long simulatedNanoTime)
    Overrides the monotonic nano time source for deterministic tests.
    void
    Stops auto-update scheduling and leaves the meter in manual mode.
    void
    Increments the transaction counter for the current sampling window.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Loggeable

    dump
    Modifier and Type
    Method
    Description
    default void
    Dumps a representation of this object using the specified renderer type.
  • Field Details

    • simulatedNanoTime

      protected volatile long simulatedNanoTime
      If set (via setSimulatedNanoTime), getNanoTime() returns this value. This is intended for deterministic tests.
  • Constructor Details

    • TPS

      public TPS()
      Default constructor.
    • TPS

      public TPS(boolean autoupdate)
      Creates a TPS meter with the default one-second period.
      Parameters:
      autoupdate - true to auto update
    • TPS

      public TPS(long period, boolean autoupdate)
      Creates a TPS meter with the given sampling period.
      Parameters:
      period - sampling period in milliseconds
      autoupdate - true to auto update
  • Method Details

    • tick

      public void tick()
      Increments the transaction counter for the current sampling window.
    • floatValue

      public float floatValue()
      Returns the current transactions-per-second value.
      Returns:
      current TPS as a floating-point value
    • intValue

      public int intValue()
      Returns the current transactions-per-second value rounded to an integer.
      Returns:
      current TPS rounded to an integer
    • getAvg

      public float getAvg()
      Returns the average TPS across sampled intervals.
      Returns:
      average TPS
    • getPeak

      public int getPeak()
      Returns the highest recorded TPS peak.
      Returns:
      peak TPS
    • getPeakWhen

      public long getPeakWhen()
      Returns the wall-clock time when the peak TPS was recorded.
      Returns:
      peak timestamp in epoch milliseconds, or -1 if unavailable
    • reset

      public void reset()
      resets average and peak.
    • getPeriod

      public long getPeriod()
      Returns the configured sampling period.
      Returns:
      period in milliseconds
    • getElapsed

      public long getElapsed()
      Returns elapsed wall-clock time since the current sampling window started.
      Returns:
      elapsed time in milliseconds
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • stop

      public void stop()
      Stops auto-update scheduling and leaves the meter in manual mode.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • dump

      public void dump(PrintStream p, String indent)
      Description copied from interface: Loggeable
      Dumps a human-readable representation of this object to the print stream.
      Specified by:
      dump in interface Loggeable
      Parameters:
      p - the output stream
      indent - indentation prefix
    • setSimulatedNanoTime

      public void setSimulatedNanoTime(long simulatedNanoTime)
      Overrides the monotonic nano time source for deterministic tests.
      Parameters:
      simulatedNanoTime - simulated monotonic time value in nanoseconds
    • getNanoTime

      protected long getNanoTime()
      Returns the current monotonic time in nanoseconds.
      Returns:
      monotonic nano time, simulated when configured