Performance Analysis and Monitoring with Perf4j


Published on

This is a lightning presentation given by Sudhan Kanade to our team for the purpose of knowledge sharing in support of our efforts to create a culture of learning.

Published in: Technology, Business

Performance Analysis and Monitoring with Perf4j

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