org.javasimon.utils
Class SimonUtils

java.lang.Object
  extended by org.javasimon.utils.SimonUtils

public final class SimonUtils
extends Object

SimonUtils provides static utility methods.

Human readable outputs

Both Stopwatch and Counter provide human readable toString outputs. All nanosecond values are converted into few valid digits with proper unit (ns, us, ms, s) - this is done via method presentNanoTime(long). Max/min counter values are checked for undefined state (max/min long value is converted to string "undef") - via method presentMinMaxCount(long).

Simon tree operations

Method for recursive reset of the Simon and all its children is provided - recursiveReset(org.javasimon.Simon). For various debug purposes there is a method that creates string displaying the whole Simon sub-tree. Here is example code that initializes two random Simons and prints the whole Simon hierarchy (note that the method can be used to obtain any sub-tree of the hierarchy):
 Split split = SimonManager.getStopwatch("com.my.other.stopwatch").start();
 SimonManager.getCounter("com.my.counter").setState(SimonState.DISABLED, false);
 split.stop();
 System.out.println(SimonUtils.simonTreeString(SimonManager.getRootSimon()));
And the output is:
 (+): Unknown Simon:  [ ENABLED]
   com(+): Unknown Simon:  [com INHERIT]
     my(+): Unknown Simon:  [com.my INHERIT]
       other(+): Unknown Simon:  [com.my.other INHERIT]
         stopwatch(+): Simon Stopwatch: total 24.2 ms, counter 1, max 24.2 ms, min 24.2 ms, mean 24.2 ms [com.my.other.stopwatch INHERIT]
       counter(-): Simon Counter: counter=0, max=undef, min=undef [com.my.counter DISABLED]
Notice +/- signs in parenthesis that displays effective Simon state (enabled/disabled), further details are printed via each Simon's toString method.

Other utilities

It is possible to obtain "local name" of the Simon (behind the last dot) via localName(String) or check if the name is valid Simon name via checkName(String).

Since:
1.0
Author:
Richard "Virgo" Richter, Radovan Sninsky

Field Summary
static String ALLOWED_CHARS
          Allowed Simon name characters.
private static int CLIENT_CODE_STACK_INDEX
           
private static DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS
           
private static DecimalFormat DEFAULT_FORMAT
           
private static int HUNDRED
           
static long INIT_MILLIS
          Value of System.currentTimeMillis() at a particular time, when INIT_NANOS is initialized as well.
static long INIT_NANOS
          Value of System.nanoTime() at a particular time, when INIT_MILLIS is initialized as well.
static String MANAGER_SERVLET_CTX_ATTRIBUTE
          Name of the attribute where manager is searched for in an appropriate context - used for Spring/JavaEE/console integration.
static long MILLIS_IN_SECOND
          Number of milliseconds in one second.
static Pattern NAME_PATTERN
          Regex pattern for Simon names.
static String NAME_PATTERN_CHAR_CLASS_CONTENT
          Regex character class content for NAME_PATTERN.
static long NANOS_IN_MILLIS
          Number of nanoseconds in one millisecond.
private static String SHRINKED_STRING
           
private static int TEN
           
private static SimpleDateFormat TIMESTAMP_FORMAT
           
private static String UNDEF_STRING
           
private static DecimalFormat UNDER_HUNDRED_FORMAT
           
private static DecimalFormat UNDER_TEN_FORMAT
           
private static int UNIT_PREFIX_FACTOR
           
 
Constructor Summary
private SimonUtils()
           
 
Method Summary
static String compact(String input, int limitTo)
          Shrinks the middle of the input string if it is too long, so it does not exceed limitTo.
static
<T> T
doWithStopwatch(String name, Callable<T> callable)
          Calls a block of code with stopwatch around and returns result.
static void doWithStopwatch(String name, Runnable runnable)
          Calls a block of code with stopwatch around, can not return any result or throw an exception (use doWithStopwatch(String, java.util.concurrent.Callable) instead).
private static String formatTime(double time, String unit)
           
static String generateName()
          Autogenerates name for the Simon using the class name and the method name.
static String generateNameForClass(String suffix)
          Autogenerates name for the Simon using the fully-qualified class name.
static String generateNameForClassAndMethod(String suffix)
          Autogenerates name for the Simon using the fully qualified class name and the method name.
private static String generatePrivate(String suffix, boolean includeMethodName)
           
static boolean checkName(String name)
          Checks if the input string is correct Simon name.
static String localName(String name)
          Returns last part of Simon name - local name.
static long millisForNano(long nanos)
          Converts nano timer value into millis timestamp compatible with System.currentTimeMillis().
static String presentMinMaxCount(long minmax)
          Returns min/max counter values in human readable form - if the value is max or min long value it is considered unused and string "undef" is returned.
static String presentMinMaxSplit(long minmax)
          Returns min/max split values in human readable form - if the value is max or min long value it is considered unused and string "undef" is returned.
static String presentNanoTime(double nanos)
          Returns nano-time in human readable form with unit.
static String presentNanoTime(long nanos)
          Returns nano-time in human readable form with unit.
private static String presentOverNanoTime(double time)
           
static String presentTimestamp(long timestamp)
          Returns timestamp in human readable (yet condensed) form "yyMMdd-HHmmss.SSS".
private static void printSimon(int level, Simon simon, StringBuilder sb)
           
private static void printSimonTree(int level, Simon simon, StringBuilder sb)
           
static void recursiveReset(Simon simon)
          Resets the whole Simon subtree - calls Simon.reset() on the Simon and recursively on all its children.
static String simonTreeString(Simon simon)
          Returns multi-line string containing Simon tree starting with the specified Simon.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MILLIS_IN_SECOND

public static final long MILLIS_IN_SECOND
Number of milliseconds in one second.

See Also:
Constant Field Values

NANOS_IN_MILLIS

public static final long NANOS_IN_MILLIS
Number of nanoseconds in one millisecond.

See Also:
Constant Field Values

NAME_PATTERN_CHAR_CLASS_CONTENT

public static final String NAME_PATTERN_CHAR_CLASS_CONTENT
Regex character class content for NAME_PATTERN.

See Also:
Constant Field Values

NAME_PATTERN

public static final Pattern NAME_PATTERN
Regex pattern for Simon names.


ALLOWED_CHARS

public static final String ALLOWED_CHARS
Allowed Simon name characters.

Since:
2.3
See Also:
Constant Field Values

MANAGER_SERVLET_CTX_ATTRIBUTE

public static final String MANAGER_SERVLET_CTX_ATTRIBUTE
Name of the attribute where manager is searched for in an appropriate context - used for Spring/JavaEE/console integration. While the name can be used in any context supporting named attributes, it is primarily aimed for ServletContext. Manager can be shared in ServletContext by SimonWebConfigurationBean (Spring module) and then picked up by SimonServletFilter (JavaEE module) and SimonConsoleFilter (Embeddable console). If no manager is found in the attribute of the context, it is expected that components will use default SimonManager instead.

Since:
3.2
See Also:
Constant Field Values

INIT_NANOS

public static final long INIT_NANOS
Value of System.nanoTime() at a particular time, when INIT_MILLIS is initialized as well. Used in millisForNano(long).

Since:
3.3

INIT_MILLIS

public static final long INIT_MILLIS
Value of System.currentTimeMillis() at a particular time, when INIT_NANOS is initialized as well. Used in millisForNano(long).

Since:
3.3

TIMESTAMP_FORMAT

private static final SimpleDateFormat TIMESTAMP_FORMAT

UNIT_PREFIX_FACTOR

private static final int UNIT_PREFIX_FACTOR
See Also:
Constant Field Values

DECIMAL_FORMAT_SYMBOLS

private static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS

TEN

private static final int TEN
See Also:
Constant Field Values

UNDER_TEN_FORMAT

private static final DecimalFormat UNDER_TEN_FORMAT

HUNDRED

private static final int HUNDRED
See Also:
Constant Field Values

UNDER_HUNDRED_FORMAT

private static final DecimalFormat UNDER_HUNDRED_FORMAT

DEFAULT_FORMAT

private static final DecimalFormat DEFAULT_FORMAT

UNDEF_STRING

private static final String UNDEF_STRING
See Also:
Constant Field Values

CLIENT_CODE_STACK_INDEX

private static final int CLIENT_CODE_STACK_INDEX

SHRINKED_STRING

private static final String SHRINKED_STRING
See Also:
Constant Field Values
Constructor Detail

SimonUtils

private SimonUtils()
Method Detail

presentNanoTime

public static String presentNanoTime(long nanos)
Returns nano-time in human readable form with unit. Number is always from 10 to 9999 except for seconds that are the biggest unit used.

Parameters:
nanos - time in nanoseconds
Returns:
human readable time string

presentNanoTime

public static String presentNanoTime(double nanos)
Returns nano-time in human readable form with unit. Number is always from 10 to 9999 except for seconds that are the biggest unit used.

Parameters:
nanos - time in nanoseconds
Returns:
human readable time string

presentOverNanoTime

private static String presentOverNanoTime(double time)
Parameters:
time - time in nanoseconds
Returns:
human readable time string

formatTime

private static String formatTime(double time,
                                 String unit)

presentTimestamp

public static String presentTimestamp(long timestamp)
Returns timestamp in human readable (yet condensed) form "yyMMdd-HHmmss.SSS".

Parameters:
timestamp - timestamp in millis
Returns:
timestamp as a human readable string

presentMinMaxCount

public static String presentMinMaxCount(long minmax)
Returns min/max counter values in human readable form - if the value is max or min long value it is considered unused and string "undef" is returned.

Parameters:
minmax - counter extreme value
Returns:
counter value or "undef" if counter contains Long.MIN_VALUE or Long.MAX_VALUE

presentMinMaxSplit

public static String presentMinMaxSplit(long minmax)
Returns min/max split values in human readable form - if the value is max or min long value it is considered unused and string "undef" is returned.

Parameters:
minmax - split extreme value
Returns:
extreme value or "undef" if extreme contains Long.MIN_VALUE or Long.MAX_VALUE

simonTreeString

public static String simonTreeString(Simon simon)
Returns multi-line string containing Simon tree starting with the specified Simon. Root Simon can be used to obtain tree with all Simons. Returns null for input value of null or for NullSimon or any Simon with name equal to null (anonymous Simons) - this is also the case when the Manager is disabled and tree for its root Simon is requested.

Parameters:
simon - root Simon of the output tree
Returns:
string containing the tree or null if the Simon is null Simon

printSimonTree

private static void printSimonTree(int level,
                                   Simon simon,
                                   StringBuilder sb)

printSimon

private static void printSimon(int level,
                               Simon simon,
                               StringBuilder sb)

localName

public static String localName(String name)
Returns last part of Simon name - local name.

Parameters:
name - full Simon name
Returns:
string containing local name

recursiveReset

public static void recursiveReset(Simon simon)
Resets the whole Simon subtree - calls Simon.reset() on the Simon and recursively on all its children. Operation is not truly atomic as a whole, consistency on the Simon level depends on the implementation of Simon.reset() (which is thread-safe in all current implementations).

Parameters:
simon - subtree root

checkName

public static boolean checkName(String name)
Checks if the input string is correct Simon name. Simon name is checked against public NAME_PATTERN.

Parameters:
name - checked string
Returns:
true if the string is proper Simon name

generateNameForClass

public static String generateNameForClass(String suffix)
Autogenerates name for the Simon using the fully-qualified class name. This method has inherent performance penalty (getting the stack-trace) so it is recommended to store pregenerated name. (Whole get-start-stop cycle is roughly 30-times slower with this method included.)

Parameters:
suffix - name suffix for eventual Simon discrimination
Returns:
autogenerated name for Simon
Since:
3.1

generateNameForClassAndMethod

public static String generateNameForClassAndMethod(String suffix)
Autogenerates name for the Simon using the fully qualified class name and the method name. This method has inherent performance penalty (getting the stack-trace) so it is recommended to store pregenerated name. (Whole get-start-stop cycle is roughly 30-times slower with this method included.)

Parameters:
suffix - name suffix for eventual Simon discrimination
Returns:
autogenerated name for Simon
Since:
3.1

generatePrivate

private static String generatePrivate(String suffix,
                                      boolean includeMethodName)

generateName

public static String generateName()
Autogenerates name for the Simon using the class name and the method name.

Returns:
autogenerated name for Simon
See Also:
generateNameForClassAndMethod(String)

doWithStopwatch

public static <T> T doWithStopwatch(String name,
                                    Callable<T> callable)
                         throws Exception
Calls a block of code with stopwatch around and returns result.

Type Parameters:
T - return type
Parameters:
name - name of the Stopwatch
callable - callable block of code
Returns:
whatever block of code returns
Throws:
Exception - whatever block of code throws
Since:
3.0

doWithStopwatch

public static void doWithStopwatch(String name,
                                   Runnable runnable)
Calls a block of code with stopwatch around, can not return any result or throw an exception (use doWithStopwatch(String, java.util.concurrent.Callable) instead).

Parameters:
name - name of the Stopwatch
runnable - wrapped block of code
Since:
3.0

compact

public static String compact(String input,
                             int limitTo)
Shrinks the middle of the input string if it is too long, so it does not exceed limitTo.

Since:
3.2

millisForNano

public static long millisForNano(long nanos)
Converts nano timer value into millis timestamp compatible with System.currentTimeMillis(). Method does not just divide nanos by one million, but also works with remembered values for milli- and nano-timers at one particular moment.

Parameters:
nanos - nano timer value
Returns:
ms timestamp
Since:
3.3 (moved from SimonManager where it was since 3.1)


Copyright © 2013. All Rights Reserved.