Package org.codehaus.janino.util
Class Benchmark
- java.lang.Object
-
- org.codehaus.janino.util.Benchmark
-
public class Benchmark extends java.lang.Object
Implements a scheme for benchmarking, i.e. for determining and/or reporting the time elapsed between the beginning and the end of an activity.The measurement is done by invoking
begin()
and later callingend()
whichs returns the time elapsed since the call tobegin()
.Notice that calls to
begin()
andend()
can be nested, and each call toend()
refers to the matchingbegin()
call. To ensure that all calls match, the preferred way to write a benchmark is... Benchmark b = new Benchmark(); ... b.begin(); try { .... } finally { long ms = b.end(); }
This code layout also makes it visually easy to write correct pairs ofbegin()
/end()
pairs.The pair
beginReporting()
andendReporting()
do basically the same, but report the benchmarking information through an internalBenchmark.Reporter
object. The defaultBenchmark.Reporter
prints its messages bySystem.out.println()
.Reporting is only enabled if the Benchmark object was created through
Benchmark(boolean)
with atrue
argument.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Benchmark.Reporter
Interface used to report messages.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Stack
beginTimes
private static java.lang.String
PAD
private Benchmark.Reporter
reporter
private boolean
reportingEnabled
-
Constructor Summary
Constructors Constructor Description Benchmark()
Benchmark(boolean reportingEnabled)
Benchmark(boolean reportingEnabled, Benchmark.Reporter reporter)
Set up aBenchmark
with a customBenchmark.Reporter
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
begin()
void
beginReporting()
Begin a benchmark (seebegin()
) and report the fact.void
beginReporting(java.lang.String message)
Begin a benchmark (seebegin()
) and report the fact.long
end()
void
endReporting()
End a benchmark (seeend()
) and report the fact.void
endReporting(java.lang.String message)
End a benchmark (seebegin()
) and report the fact.void
report(java.lang.String message)
Report the given message.void
report(java.lang.String optionalTitle, java.lang.Object o)
Report thetitle
, a colon, a space, and the pretty-printedObject
.private void
reportIndented(java.lang.String message)
Report a message throughreporter
, indent by N spaces where N is the current benchmark stack depth.
-
-
-
Field Detail
-
beginTimes
private final java.util.Stack beginTimes
-
reportingEnabled
private final boolean reportingEnabled
-
reporter
private final Benchmark.Reporter reporter
-
PAD
private static final java.lang.String PAD
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Benchmark
public Benchmark()
-
Benchmark
public Benchmark(boolean reportingEnabled)
-
Benchmark
public Benchmark(boolean reportingEnabled, Benchmark.Reporter reporter)
Set up aBenchmark
with a customBenchmark.Reporter
.
-
-
Method Detail
-
begin
public void begin()
- See Also:
Benchmark
-
end
public long end()
- See Also:
Benchmark
-
beginReporting
public void beginReporting()
Begin a benchmark (seebegin()
) and report the fact.
-
beginReporting
public void beginReporting(java.lang.String message)
Begin a benchmark (seebegin()
) and report the fact.
-
endReporting
public void endReporting()
End a benchmark (seeend()
) and report the fact.
-
endReporting
public void endReporting(java.lang.String message)
End a benchmark (seebegin()
) and report the fact.
-
report
public void report(java.lang.String message)
Report the given message.
-
report
public void report(java.lang.String optionalTitle, java.lang.Object o)
Report thetitle
, a colon, a space, and the pretty-printedObject
.- Parameters:
optionalTitle
-o
-
-
reportIndented
private void reportIndented(java.lang.String message)
Report a message throughreporter
, indent by N spaces where N is the current benchmark stack depth.
-
-