Performance Analysis and Monitoring with Perf4j Code timing, logging, analyzing and monitoring tool Sudhan Kanade
Jun 17, 2015
Performance Analysis and Monitoring with Perf4jCode timing, logging, analyzing and
monitoring tool
Sudhan Kanade
Why do we need Perf4J
• Code works on dev / stage env
• Scalability
• Distributed application
1
Analogy: Perf4J to Log4J
• Log4j : System.out.println()
• Perf4J : System.currentTimeMillis()
• Can log similarly to any appenders defined in log4j.
• Can use AspectJ / Spring AOP
2
What difference Perf4J makes:
• Without Perf4Jlong start = System.currentTimeMillis();// execute the block of code to be timedlog.info("ms for block n was: " + (System.currentTimeMillis() -
start));
• With Per4JStopWatch stopWatch = new LoggingStopWatch();//... execute code here to be timedstopWatch.stop("example1", "custom message text");
• Allows parsing, analyzing and monitoring the logs.
3
Features / Highlights of Perf4J
• A simple stop watch mechanism for concise timing statements.• A command line tool for generating aggregated statistics and
performance graphs from raw log files.• Custom log4j appenders to generate statistics and graphs in a
running application, with java.util.logging and logback support scheduled for subsequent releases.
• The ability to expose performance statistics as JMX attributes, and to send notifications when statistics exceed specified thresholds.
• A @Profiled annotation and a set of custom aspects that allow unobtrusive timing statements when coupled with an AOP framework such as AspectJ or Spring AOP.
4
Timing using StopWatch
• Base class : LoggingStopWatch
• SubClass : Log4JStopWatch, CommonsLogStopWatch and Slf4JStopWatch
5
Log Parsers: Statistics and Graphs
• Groups stop watch output by tag and by time slice
• Generating detailed statistics information• Time series graphs using the Google Chart API• LogParser reads from standard input thus
allowing it to get output generated in real time.– tail -f performance.log | java -jar perf4j-0.9.8.1.jar
6
Sample for LogParser
7
Integrating Directly With Log4J
• Set of custom log4j appenders
• Allows Perf4J to expose performance data as attributes on JMX MBeans, and to send JMX notifications.
• Provides graphing appenders that generate performance graphs which can be exposed through a web front-end using a Perf4J graphing servlet.
8
sample log4j.xml – perf4j
9
sample log4j.xml – log4j and perf4j
10
Perf4J appender for JMX:
11
@Profiled Annotation
• Reduces the "signal-to-noise" ratio of the code.• Allowing the method body to remain free of
StopWatch code.• Perf4J custom timing aspect can be enabled
with an aspect-oriented programming framework such as AspectJ or Spring AOP
• Minimal overhead.
12
@Profiled Annotation - sample
@Profiled(tag = "dynamicTag_{$0}")
public void profiledExample(String tagSuffix) {
... business logic only here
//method body to remain free of StopWatch code
}
13
Pitfalls and Best Practices
• Application monitoring - Fail to optimally deliver their intended benefits
• Either too much or not enough where it is required.
• Overhead
14
Pitfalls and Best … cont
• When deciding which methods and code blocks to profile, focus on the big fish first.
15
Pitfalls and Best … cont
• Perf4J is designed to offload performance analysis to a separate thread or process.
16
Pitfalls and Best … cont
• Don’t forget the benefits of performance regression testing
17
Pitfalls and Best … cont
• Take advantage of the @Profiled annotation and AspectJ’s load-time weaving to decide which methods should be timed at deployment time.
18
Pitfalls and Best … cont
• Don’t forget about parts of your application that execute outside of the JVM
19
Future Directions for Perf4J
• Methods to be profiled using a separate configuration file
• To time method executions without access to source code
20
The End
5 minutes of question time
starts now!
Questions
4 minutes left!
Questions
3 minutes left!
Questions
2 minutes left!
Questions
1 minute left!
Questions
30 seconds left!
Questions
TIME IS UP!