PerformanceMonster is a tool that basically just shows you a coarse-grained view of the execution of your program, in the form of a graph of horizontal bars. In theory, it works much like a profiler, but there are notable differences:
PerformanceMonster bases its graph on logging events sent from a running program. Actually sending those logging events can happen in different ways:
We will come back to these different methods later. In any case, you yourself define when to send logging events. Sending logging events everywhere in your code will not prove usable, as the graphs shown by PerformanceMonster will then be a chaos, and the overhead introduced by sending all those events will be substantial. Instead, you should focus on what makes sense. Typically, these places will be a good starting point:
Of course, these will just be the starting point. Once you start using PerformanceMonster, you will quickly discover that code you did not consider before will be valuable to log. Once you get into this process of looking at your program that way, you will be able to understand how your program works, and why it doesn't necessarily work as efficiently as you think. Once there, fixing the problem is normally easy.
Also, since you will end up logging only performance-critical portions of your code, adding the overhead of logging there will not make much of a difference to the performance of your application. As a result, PerformanceMonster will add much less overhead than a normal profiler.
Call the begin and end methods defined in the MonsterLogger class.
#import performancemonster.MonsterLogger;
public class MyClass {
public int myMethod(String s) {
MonsterLogger.begin("my.log.category", "myMethod(" + s + ")");
try {
// ...some performance-intensive thing here...
} finally {
MonsterLogger.end("my.log.category", "myMethod");
}
}
}
Remember to surround your code with a try-finally block, so that exceptions in your code won't ruin the logging.
Use a Log4J logger, surround your code with logging statements. The first logging statement must begin with "BEGIN ", the last with "END ". Set up Log4J so that messages logged to your logger will use the PerformanceMonster appender.
public class MyClass {
private static final Logger logger = Logger.getLogger("my.log.category");
public int myMethod(String s) {
logger.debug("BEGIN myMethod(" + s + ")");
try {
// ...some performance-intensive thing here...
} finally {
logger.debug("END myMethod");
}
}
}
Remember to surround your code with a try-finally block, so that exceptions in your code won't ruin the logging.
Using the instrumentation GUI, you can automatically at run-time make your code behave just as if you had instrumented specified methods manually. Doing this automatically has many benefits: