public class PerformanceProfiler extends Object implements javassist.Translator
This class implements an easy-to-use, relatively light-weight performance profiler that does not need operating system support or special start-up parameters to the Java Virtual Machine.
Use it in one of the following ways:if (PerformanceProfiler.startProfiling(args)) return;
setActive(boolean)
, print a report using
report(PrintStream)
, or print a report to a file with
report(File, int)
.
PerformanceProfiler.report(null);
.
The idea behind the profiler is that a Javassist-specific class
Loader
with a Translator
is used to load the main class and
all its dependent classes. The translator instruments every method such that
a method-specific counter is incremented, and the total time spent in the
method is recorded, too. A list of instrumented methods with their counters
is maintained globally.
To record the time spent in a method, either the getNanos()
method
(thread-specific, but unfortunately very, very slow) or the
getNanosQnD()
(not thread-specific, but does not dominate even small
methods' timing) is used. The latter is the default.
Modifier and Type | Field and Description |
---|---|
protected static boolean |
debug |
protected static Method |
realReport |
Constructor and Description |
---|
PerformanceProfiler()
The constructor.
|
Modifier and Type | Method and Description |
---|---|
static long |
getNanos()
Gets nanoseconds per thread for relative time measurements
|
static long |
getNanosQnD()
A much faster, but less accurate version of
getNanos()
The problem with this is that it does not measure the current Thread's CPU cycles. |
static boolean |
isActive()
Reports whether profiling is in progress
|
static void |
main(String... args)
The main method.
|
void |
onLoad(javassist.ClassPool pool,
String classname) |
static void |
report(File file,
int column)
Writes a profile report to a file.
|
static void |
report(PrintStream writer)
Writes a report.
|
static void |
report(PrintStream writer,
int column)
Writes a report.
|
protected static void |
reportCaller(PrintStream writer,
int level)
Reports a caller.
|
static void |
setActive(boolean active)
Start or stop profiling
This method initializes the profiling class loader if necessary.
|
void |
start(javassist.ClassPool pool) |
static boolean |
startProfiling(String mainClass,
String... args)
Convenience method to call in your main(String[]) method.
|
protected static final boolean debug
protected static Method realReport
public PerformanceProfiler()
public static boolean startProfiling(String mainClass, String... args) throws Throwable
if (PerformanceProfiler.startProfiling(null, args)) return;
It will start the profiling class loader, run the main method of the
calling class (which will call startProfiling() again, but that will now
return false since it is already profiling) and return true.mainClass
- the main class, or null to refer to the caller's classargs
- the arguments for the main methodThrowable
public static void main(String... args) throws Throwable
args
- the main class to profile, followed by the arguments to pass to the main methodThrowable
public static void setActive(boolean active)
active
- public static boolean isActive()
public static void report(File file, int column) throws FileNotFoundException
report(PrintStream, int)
.file
- where to write tocolumn
- what column to sort byFileNotFoundException
public static void report(PrintStream writer)
writer
- where to write to.public static void report(PrintStream writer, int column)
writer
- where to write to.public static final long getNanos()
public static final long getNanosQnD()
getNanos()
The problem with this is that it does not measure the current Thread's CPU cycles.public void start(javassist.ClassPool pool) throws javassist.NotFoundException, javassist.CannotCompileException
start
in interface javassist.Translator
javassist.NotFoundException
javassist.CannotCompileException
public void onLoad(javassist.ClassPool pool, String classname) throws javassist.NotFoundException
onLoad
in interface javassist.Translator
javassist.NotFoundException
protected static void reportCaller(PrintStream writer, int level)
writer
- where to write tolevel
- how many levels to go back in the stack traceCopyright © 2014–2022 ImageJ. All rights reserved.