public class SampleProfiler
extends java.lang.Object
Set the ComparableProfiler
to follow using addProfiler(ComparableProfiler)
function. Then set the
code block using enter(String)
and exit(String)
. Some intermediate laps can be added using
lap(String)
.
Usage:
SampleProfiler
:
ComparableProfiler
:
SampleProfiler.addProfiler(new TimeProfiler());
ComparableProfiler
:
List<ComparableProfiler> list = new ArrayList<>(); list.add(new TimeProfiler()); list.add(new HeapProfiler()); list.add(new ThreadsProfiler()); SampleProfiler.addProfilers(list);
enter(String)
, enter(Group)
or enter(String, Group)
.lap(String)
.exit(String)
, exit(Group)
or exit(String, Group)
.print()
.Modifier and Type | Method and Description |
---|---|
static void |
addProfiler(ComparableProfiler profiler)
Adds a
SampleProfiler to follow. |
static void |
addProfilers(java.util.Collection<ComparableProfiler> profilers)
Adds a collection of
SampleProfiler to follow. |
static void |
clear()
Resets the register values.
|
static void |
clearProfilers()
Clears all the Profilers registered.
|
static void |
enter(Group group)
Enters a new code block.
|
static void |
enter(java.lang.String tag)
Enters a new code block.
|
static void |
enter(java.lang.String tag,
Group group)
Enters a new code block.
|
static void |
exit(Group group)
Exits a code block.
|
static void |
exit(java.lang.String tag)
Exits a code block.
|
static void |
exit(java.lang.String tag,
Group group)
Exits a code block.
|
static void |
lap(java.lang.String tag)
Enters a new lap.
|
static void |
print()
Prints the information gathered.
|
static void |
setLoggerLevel(java.util.logging.Level level)
Sets the log LoggerLevel.
|
public static void setLoggerLevel(java.util.logging.Level level)
level
- the LoggerLevel to log the message.public static void addProfilers(java.util.Collection<ComparableProfiler> profilers) throws ej.bon.IllegalStateException
SampleProfiler
to follow.profilers
- the Profilers to follow.ej.bon.IllegalStateException
- when the profiler is already started (an enter(String)
, enter(Group)
,
enter(String, Group)
or lap(String)
has already been called.clear()
public static void addProfiler(ComparableProfiler profiler) throws ej.bon.IllegalStateException
SampleProfiler
to follow.profiler
- the profiler to follow.ej.bon.IllegalStateException
- when the profiler is already started (an enter(String)
, enter(Group)
,
enter(String, Group)
or lap(String)
has already been called.clear()
public static void clearProfilers() throws ej.bon.IllegalStateException
ej.bon.IllegalStateException
- when the profiler is already started (an enter(String)
, enter(Group)
,
enter(String, Group)
or lap(String)
has already been called.clear()
public static void lap(java.lang.String tag)
For example:
SampleProfiler.addProfiler(new TimeProfiler()); for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { f(); } }).start(); } Thread.sleep(5000); SampleProfiler.print();With the functions to profile:
private void f() { SampleProfiler.enter("f()"); try { g(); SampleProfiler.lap("f"); // Some code SampleProfiler.lap("2 f"); g(); } finally { SampleProfiler.exit("f()"); } }
private void g() throws InterruptedException { SampleProfiler.enter("g()"); try { // Some code } finally { SampleProfiler.exit("g()"); } }Will output
sampleprofiler INFO: thread5: - Time: f() 312 ms g() 102 ms * lap f 103 ms * lap 2 f 106 ms g() 100 ms thread6: - Time: f() 309 ms g() 101 ms * lap f 102 ms * lap 2 f 105 ms g() 100 ms thread4: - Time: f() 310 ms g() 101 ms * lap f 104 ms * lap 2 f 101 ms g() 102 ms thread3: - Time: f() 318 ms g() 100 ms * lap f 105 ms * lap 2 f 100 ms g() 101 ms thread2: - Time: f() 311 ms g() 100 ms * lap f 107 ms * lap 2 f 100 ms g() 102 ms
tag
- the lap name.public static void enter(java.lang.String tag)
For example:
SampleProfiler.addProfiler(new TimeProfiler()); for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { f(); } }).start(); } Thread.sleep(5000); SampleProfiler.print();With the functions to profile:
private void f() { SampleProfiler.enter("f()"); try { g(); // Some code g(); } finally { SampleProfiler.exit("f()"); } }
private void g() throws InterruptedException { SampleProfiler.enter("g()"); try { // Some code } finally { SampleProfiler.exit("g()"); } }Will output
sampleprofiler INFO: thread5: - Time: f() 316 ms g() 104 ms g() 106 ms thread4: - Time: f() 317 ms g() 102 ms g() 102 ms thread6: - Time: f() 316 ms g() 101 ms g() 107 ms thread3: - Time: f() 308 ms g() 103 ms g() 102 ms thread2: - Time: f() 311 ms g() 101 ms g() 101 ms
tag
- the code block tag.public static void enter(java.lang.String tag, Group group)
static Group groupF = new Group("groupF"); static Group groupG = new Group("groupG"); SampleProfiler.addProfiler(new TimeProfiler()); for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { f(); } }).start(); } Thread.sleep(5000); SampleProfiler.print();With the functions to profile:
private void f() { SampleProfiler.enter("f()", groupF); try { g(); // Some code g(); } finally { SampleProfiler.exit("f()", groupF); } }
private void g() { SampleProfiler.enter("g()", groupG); try { // Some code } finally { SampleProfiler.exit("g()", groupG); } }Will output
thread5: - Time: f() 340 ms g() 107 ms g() 100 ms thread3: - Time: f() 351 ms g() 101 ms g() 100 ms thread4: - Time: f() 338 ms g() 103 ms g() 111 ms thread2: - Time: f() 450 ms g() 208 ms g() 106 ms thread6: - Time: f() 338 ms g() 102 ms g() 100 ms Groups: groupF: - Time: count=5, min=338 ms, max=450 ms, avg=363 ms groupG: - Time: count=10, min=100 ms, max=208 ms, avg=114 ms
tag
- the code block tag.group
- the group to profile.public static void enter(Group group)
For example:
static Group groupF = new Group("f()"); static Group groupG = new Group("g()"); SampleProfiler.addProfiler(new TimeProfiler()); for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { f(); } }).start(); } Thread.sleep(5000); SampleProfiler.print();With the functions to profile:
private void f() { SampleProfiler.enter(groupF); try { g(); // Some code g(); } finally { SampleProfiler.exit(groupF); } }
private void g() throws InterruptedException { SampleProfiler.enter(groupG); try { // Some code } finally { SampleProfiler.exit(groupG); } }Will output
thread5: - Time: f() 340 ms g() 107 ms g() 100 ms thread3: - Time: f() 351 ms g() 101 ms g() 100 ms thread4: - Time: f() 338 ms g() 103 ms g() 111 ms thread2: - Time: f() 450 ms g() 208 ms g() 106 ms thread6: - Time: f() 338 ms g() 102 ms g() 100 ms Groups: f(): - Time: count=5, min=338 ms, max=450 ms, avg=363 ms g(): - Time: count=10, min=100 ms, max=208 ms, avg=114 ms
group
- the group to profile.public static void exit(java.lang.String tag)
enter(Group)
, enter(String)
or
enter(String, Group)
.
Using a try
and finally
will force the use
SampleProfiler.enter(""); try { // Some code } finally { SampleProfiler.exit(""); }
tag
- the code block name, should map the tag of the enter.public static void exit(Group group)
enter(Group)
, enter(String)
or
enter(String, Group)
.
Using a try
and finally
will force the use
SampleProfiler.enter(""); try { // Some code } finally { SampleProfiler.exit(""); }
group
- the group, should map the group of the enter.public static void exit(java.lang.String tag, Group group)
enter(Group)
, enter(String)
or
enter(String, Group)
.
Using a try
and finally
will force the use
SampleProfiler.enter(""); try { // Some code } finally { SampleProfiler.exit(""); }This function is equivalent to
exit(String)
with the same tag.tag
- the code block, should map the tag of the enter.group
- the group, should map the group of the enter.public static void print()
public static void clear()
clearProfilers()