Performance Analysis and Monitoring with Perf4j

28
Performance Analysis and Monitoring with Perf4j Code timing, logging, analyzing and monitoring tool Sudhan Kanade

description

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.

Transcript of Performance Analysis and Monitoring with Perf4j

Page 1: Performance Analysis and Monitoring with Perf4j

Performance Analysis and Monitoring with Perf4jCode timing, logging, analyzing and

monitoring tool

Sudhan Kanade

Page 2: Performance Analysis and Monitoring with Perf4j

Why do we need Perf4J

• Code works on dev / stage env

• Scalability

• Distributed application

1

Page 3: Performance Analysis and Monitoring with Perf4j

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

Page 4: Performance Analysis and Monitoring with Perf4j

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

Page 5: Performance Analysis and Monitoring with Perf4j

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

Page 6: Performance Analysis and Monitoring with Perf4j

Timing using StopWatch

• Base class : LoggingStopWatch

• SubClass : Log4JStopWatch, CommonsLogStopWatch and Slf4JStopWatch

5

Page 7: Performance Analysis and Monitoring with Perf4j

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

Page 8: Performance Analysis and Monitoring with Perf4j

Sample for LogParser

7

Page 9: Performance Analysis and Monitoring with Perf4j

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

Page 10: Performance Analysis and Monitoring with Perf4j

sample log4j.xml – perf4j

9

Page 11: Performance Analysis and Monitoring with Perf4j

sample log4j.xml – log4j and perf4j

10

Page 12: Performance Analysis and Monitoring with Perf4j

Perf4J appender for JMX:

11

Page 13: Performance Analysis and Monitoring with Perf4j

@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

Page 14: Performance Analysis and Monitoring with Perf4j

@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

Page 15: Performance Analysis and Monitoring with Perf4j

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

Page 16: Performance Analysis and Monitoring with Perf4j

Pitfalls and Best … cont

• When deciding which methods and code blocks to profile, focus on the big fish first.

15

Page 17: Performance Analysis and Monitoring with Perf4j

Pitfalls and Best … cont

• Perf4J is designed to offload performance analysis to a separate thread or process.

16

Page 18: Performance Analysis and Monitoring with Perf4j

Pitfalls and Best … cont

• Don’t forget the benefits of performance regression testing

17

Page 19: Performance Analysis and Monitoring with Perf4j

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

Page 20: Performance Analysis and Monitoring with Perf4j

Pitfalls and Best … cont

• Don’t forget about parts of your application that execute outside of the JVM

19

Page 21: Performance Analysis and Monitoring with Perf4j

Future Directions for Perf4J

• Methods to be profiled using a separate configuration file

• To time method executions without access to source code

20

Page 22: Performance Analysis and Monitoring with Perf4j

The End

5 minutes of question time

starts now!

Page 23: Performance Analysis and Monitoring with Perf4j

Questions

4 minutes left!

Page 24: Performance Analysis and Monitoring with Perf4j

Questions

3 minutes left!

Page 25: Performance Analysis and Monitoring with Perf4j

Questions

2 minutes left!

Page 26: Performance Analysis and Monitoring with Perf4j

Questions

1 minute left!

Page 27: Performance Analysis and Monitoring with Perf4j

Questions

30 seconds left!

Page 28: Performance Analysis and Monitoring with Perf4j

Questions

TIME IS UP!