| java.lang.Object com.commsen.stopwatch.Stopwatch
Stopwatch | public class Stopwatch (Code) | | Stopwatch allows you to measure performance of any given piece of code. It's basic usage is as follows:
.....
long swId = Stopwatch.start("group", "label");
// some code to be measured
Stopwatch.stop(swId);
....
To skip already started mensuration (for example if an Exception is thrown) something similar to following code may be used:
.....
long swId = Stopwatch.start("group", "label");
try {
// some code to be measured
} catch (Exception) {
Stopwatch.skip(swId);
} finally {
Stopwatch.stop(swId);
}
....
By default Stopwatch is not active! It means all calls to
Stopwatch.start(String,String) ,
Stopwatch.skip(long) and
Stopwatch.stop(long) methods are simply ignored.
To activate Stopwatch do one of the following:
- explicitly call Stopwatch.setActive(true)
- pass
-Dcom.commsen.stopwatch.activeOnStart=true JVM parameter
- create "stopwatch.properties" file on classpath and set
activeOnStart=true
Stopwatch can also be activated/deactivated at runtime via JMX, RMI, etc.
author: Milen Dyankov |
Method Summary | |
public static Report[] | getAllByGroupReports() | public static Report[] | getAllByLabelReports() | public static Report[] | getAllReports() | public static String | getEngineClass() | public static long[] | getGroupLoad(String group, int periodField, int numberOfPeriods) Returns information of how many instances of any masured code in group group ware running for the last numberOfPeriods periods. | public static Report[] | getGroupReports(String group) | public static long[] | getLabelLoad(String label, int periodField, int numberOfPeriods) Returns information of how many instances of any masured code labeled label ware running for the last numberOfPeriods periods. | public static Report[] | getLabelReports(String label) | public static long[] | getLoad(int periodField, int numberOfPeriods) Returns information of how many instances of any masured code ware running for the last numberOfPeriods periods. | public static long[] | getLoad(String group, String label, int periodField, int numberOfPeriods) Returns information of how many instances of any masured code in group gropup labeled label ware running for the last numberOfPeriods periods. | public static String | getPersistenceMode() | public static String | getProperty(String key, String defaultValue) | public static Report | getSingleReport(String group, String label) | public static String | getStorageClass() | public static boolean | isActive() Called to check if Stopwatch is active. | public static boolean | isDebugEnabled() Called to check if Stopwatch should produce debug information. | public static void | reset() | public static void | setActive(boolean active) This method changes stopwatch's status
Should be used to activate/inactivate Stopwatch at runtime. | public static void | setDebugEnabled(boolean debugEnabled) Used to disable/enable Stopwatch's debug information.
The reason for this method to exist is to be able to minimize the performance impact
Stopwatch may have on the measured application. | public static void | skip(long id) Skip mensuration with id id .
Method should be called if for some reason current mensuration should be skipped.
Default stopwatch engine will delete any information related to this id
but other engines may behave differently (for example to provide a "skipped mensurations" report). | public static long | start(String group, String label) Start new mensuration. | public static void | stop(long id) Stop mensuration with id id . |
DEFAULT_ENGINE | final public static String DEFAULT_ENGINE(Code) | | |
DEFAULT_MODE | final public static int DEFAULT_MODE(Code) | | |
DEFAULT_STORAGE | final public static String DEFAULT_STORAGE(Code) | | |
PROPERTY_ACTIVE | final public static String PROPERTY_ACTIVE(Code) | | |
PROPERTY_DEBUG | final public static String PROPERTY_DEBUG(Code) | | |
PROPERTY_ENGINE | final public static String PROPERTY_ENGINE(Code) | | |
PROPERTY_JMX_MANAGED | final public static String PROPERTY_JMX_MANAGED(Code) | | |
PROPERTY_MBEAN_SERVER_NAME | final public static String PROPERTY_MBEAN_SERVER_NAME(Code) | | |
PROPERTY_STORAGE | final public static String PROPERTY_STORAGE(Code) | | |
SYSTEM_PROPERTIES_PREFIX | final public static String SYSTEM_PROPERTIES_PREFIX(Code) | | |
getAllByGroupReports | public static Report[] getAllByGroupReports()(Code) | | |
getAllByLabelReports | public static Report[] getAllByLabelReports()(Code) | | |
getAllReports | public static Report[] getAllReports()(Code) | | Generates an array of reports which contains exectly 1 element for each combination of group and label
If there is no enough data to produce reports, this method returns null
array of reports of null . |
getGroupLoad | public static long[] getGroupLoad(String group, int periodField, int numberOfPeriods)(Code) | | Returns information of how many instances of any masured code in group group ware running for the last numberOfPeriods periods.
Period length is defined by periodField which can be one of
java.util.Calendar.FIELD_NAME
For example to see how many peaces of masured code in group g1 were running per day for the last 10 days, one could use:
long[] load = Stopwatch.getLoad(
java.util.Calendar.DAY_OF_MONTH , 10);
In this case load[0] will contain the number of code instances running 10 days ago and
load[9] number of code instances running today.
Parameters: group - the group for which report should be generated Parameters: periodField - can be one of java.util.Calendar.FIELD_NAME Parameters: numberOfPeriods - number of periods array of length numberOfPeriods where every element represents the load for given pariod. |
getGroupReports | public static Report[] getGroupReports(String group)(Code) | | Generates an array of reports which contains exectly 1 element for each group
If there is no enough data to produce the report, this method returns null
Parameters: group - the group for which report should be generated array of reports of null . |
getLabelLoad | public static long[] getLabelLoad(String label, int periodField, int numberOfPeriods)(Code) | | Returns information of how many instances of any masured code labeled label ware running for the last numberOfPeriods periods.
Period length is defined by periodField which can be one of
java.util.Calendar.FIELD_NAME
For example to see how many peaces of masured code labeled l1 were running per second for the last 15 seconds, one could use:
long[] load = Stopwatch.getLoad(
java.util.Calendar.SECOND , 15);
In this case load[0] will contain the number of code instances running 15 seconds ago and
load[14] number of code instances running in the last second.
Parameters: label - the label for which report should be generated Parameters: periodField - can be one of java.util.Calendar.FIELD_NAME Parameters: numberOfPeriods - number of periods array of length numberOfPeriods where every element represents the load for given pariod. |
getLabelReports | public static Report[] getLabelReports(String label)(Code) | | Generates an array of reports which contains exectly 1 element for each label
If there is no enough data to produce the report, this method returns null
Parameters: label - the label for which report should be generated array of reports or null . |
getLoad | public static long[] getLoad(int periodField, int numberOfPeriods)(Code) | | Returns information of how many instances of any masured code ware running for the last numberOfPeriods periods.
Period length is defined by periodField which can be one of
java.util.Calendar.FIELD_NAME
For example to see how many peaces of masured code were running per minute for the last 30 minutes, one could use:
long[] load = Stopwatch.getLoad(
java.util.Calendar.MINUTE , 30);
In this case load[0] will contain the number of code instances running 30 minutes ago and
load[29] number of code instances running in the last minute.
Parameters: periodField - can be one of java.util.Calendar.FIELD_NAME Parameters: numberOfPeriods - number of periods array of length numberOfPeriods where every element represents the load for given pariod. |
getLoad | public static long[] getLoad(String group, String label, int periodField, int numberOfPeriods)(Code) | | Returns information of how many instances of any masured code in group gropup labeled label ware running for the last numberOfPeriods periods.
Period length is defined by periodField which can be one of
java.util.Calendar.FIELD_NAME
For example to see how many peaces of masured code in group g1 labeled l1 were running per second for the last 3 weeks, one could use:
long[] load = Stopwatch.getLoad(
java.util.Calendar.WEEK_OF_YEAR , 3);
In this case load[0] will contain the number of code instances running 3 weeks ago and
load[2] number of code instances running in the last week.
Parameters: group - the group for which report should be generated Parameters: label - the label for which report should be generated Parameters: periodField - can be one of java.util.Calendar.FIELD_NAME Parameters: numberOfPeriods - number of periods array of length numberOfPeriods where every element represents the load for given pariod. |
getPersistenceMode | public static String getPersistenceMode()(Code) | | |
getProperty | public static String getProperty(String key, String defaultValue)(Code) | | Tries to get the value of property key
Parameters: key - Parameters: defaultValue - the value of property key of defaultValue if property not found. |
getSingleReport | public static Report getSingleReport(String group, String label)(Code) | | Generates a single report for provided group and label
If there is no enough data to produce the report, this method returns null
Parameters: group - the group for which report should be generated Parameters: label - the label for which report should be generated single report for provided group and label of null . |
isDebugEnabled | public static boolean isDebugEnabled()(Code) | | Called to check if Stopwatch should produce debug information.
See Also: Stopwatch.setDebugEnabled(boolean) See Also: Returns the true or false |
reset | public static void reset()(Code) | | |
setActive | public static void setActive(boolean active)(Code) | | This method changes stopwatch's status
Should be used to activate/inactivate Stopwatch at runtime.
Parameters: active - the Stopwatch's status. |
setDebugEnabled | public static void setDebugEnabled(boolean debugEnabled)(Code) | | Used to disable/enable Stopwatch's debug information.
The reason for this method to exist is to be able to minimize the performance impact
Stopwatch may have on the measured application. Generating debug info consumes additional
CPU units, which may become a problem if Stopwatch is heavily used.
Setting this to false (is is false by default) will cause no debug info being generated by Stopwatch
even when log4j's level is set to DEBUG.
Parameters: debugEnabled - should debug information be generated |
skip | public static void skip(long id)(Code) | | Skip mensuration with id id .
Method should be called if for some reason current mensuration should be skipped.
Default stopwatch engine will delete any information related to this id
but other engines may behave differently (for example to provide a "skipped mensurations" report).
This method will do nothing if Stopwatch is not active or mensuration has been stopped already.
Parameters: id - which mensuration to stop |
start | public static long start(String group, String label)(Code) | | Start new mensuration.
This method will do nothing and simply return a negative long if Stopwatch is not active.
Parameters: group - the name of the group this mensuration should be placed in Parameters: label - how this mensuration should be labeled Unique ID representing current mensuration. |
stop | public static void stop(long id)(Code) | | Stop mensuration with id id .
This method will do nothing if Stopwatch is not active or mensuration has been skipped already.
Parameters: id - which mensuration to stop |
|
|