Saturday, August 9, 2014

Book Review: Java EE 7 Performance Tuning and Optimization

Packt Publishing recently published 's Osama Oransa's Java EE 7 Performance Tuning and Optimization. This post is my review of this book of twelve chapters and approximately 430 pages of substantive content.


The Preface of Java EE 7 Performance Tuning and Optimization provides brief summaries of all 12 chapters. The Preface also lists the main tools used throughout the book: JDK 7 Update 45, NetBeans 7.4, GlassFish 4.0, MySQL 5.5, Eclipse with Eclipse Test   Performance Tools Platform Project, Apache JMeter 2.10, and JProfiler 8. The Preface states that Java EE 7 Performance Tuning and Optimization is a "strong entry point for the persons without any performance tuning experience" and that the book is for "experienced Java developers, architects, team leaders, consultants, support engineers, and all people working in the performance tuning in the Java applications, and particularly in Java Enterprise applications."

Chapter 1: Getting Started with Performance Tuning

The first chapter of Java EE 7 Performance Tuning and Optimization is a nearly 30 pages long high-level discussion on "the art of performance tuning." The chapter discusses "Java EE performance tuning art" before looking at identifying performance issues and classifying these performance issues by waterfall development lifecycle discovery phase (when the issue was identified) and by root phase (when the issue was introduced).

Chapter 1 spends several pages discussing high-level "soft skills" one needs as a leader of a performance tuning team and the very-high-level approach used to narrow down causes of performance issues. There are several other high-level discussions covered in this chapter such as descriptions of the different common architectures and the different application layers to be performance tuned.

Java EE 7 Performance Tuning and Optimization's initial chapter has a few things for experienced Java developers, but is really better suited for those entirely new to Java performance tuning considerations. It might also be better suited to the readers who are team leaders and consultants than those readers who are developers. The most beginning Java developers might benefit from the brief, very high level descriptions of application layers, but most of this information should be something most developers already know.

Chapter 2: Understanding Java Fundamentals

Chapter 2 of Java EE 7 Performance Tuning and Optimization provides lower-level details than the first chapter. However, much of this chapter is identical to online resources upon which it is based. One of the first sentences in Chapter 2 states, "Most of the content of this chapter adheres to Oracle Java documentations as the source of Java specifications and features." I didn't know what this meant when I first read it, but I believe it is the author's way of saying that much of the material in the chapter comes directly from Oracle's Java-related documentation.

The second chapter begins with a section on new Java EE 7 features. Each new Java EE 7 feature is briefly described, illustrated with pseudo code, and has its underlying specification referenced with a link. I agree with the author's recommendation that anyone with awareness of the new Java EE 7 features should skip this section.

Chapter 2's section "Understanding memory structure in the JVM" is good, but much of it is word-for-word the same as the text provided in Chapter 2 ("The Structure of the Java Virtual Machine") of the Java Virtual Machine specification and in Memory Management in the HotSpot JVM. In fact, many of the diagrams are the same or nearly the same except that the original sources have colors included in the diagrams, which are grayscale in the PDF version of Java EE 7 Performance Tuning and Optimization that I reviewed.

I also liked the "Understanding concurrency in Java" section of Java EE 7 Performance Tuning and Optimization, but it seemed to be much the same content (including several passages that are word-for-word the same) as the "Concurrency Basics" section of the Java EE 7 Tutorial and Chapter 17 ("Threads and Locks") of the Java Virtual Machine specification.

Chapter 2 also includes brief discussion of JSP/servlet application scopes and more focus on Java EE 7 concurrency features. There is some good information in this chapter, but the downsides are that some of its content seems a little out of order and much of the content relies heavily, even directly, on Oracle's documentation.

Chapter 3: Getting Familiar with Performance Testing

The third chapter of Java EE 7 Performance Tuning and Optimization opens with a definition of performance testing and contrasts performance with the more common types of testing done to test functionality. The chapter then lists and briefly discusses each "important aspect of the performance testing process." This section contains useful definitions and terminology for those new to performance testing and performance optimization and is largely at a higher or more general level than Java EE. Someone wanting to know the different between load testing and stress testing, for example, would find this section very useful.

The third chapter provides a list of some tools that can used in performance testing, but its focus is on JMeter, which it devotes numerous pages to. The section on JMeter could have likely been a chapter of its own and provides quite a bit of detail about setting up and applying JMeter in performance testing of a web service, of a web application, and of JDBC code.

Chapter 4: Monitoring Java Applications

Chapter 4 of Java EE 7 Performance Tuning and Optimization provides a survey of some of the monitoring tools available for monitoring an application at different layers. The section on monitoring operating systems discusses and provides illustrations of Windows-based monitoring tools such as Windows 7 Task Manager, Resource Monitor, netstat, and Typeperf. The portion covering Linux monitoring tools is shorter (about half of a page), but lists tools such as ps, pgrep, pstree, top, vmstat, iostat, sar, and tcpdump, and refers the reader to these respective tools' man pages for additional details. The section on operating system monitoring concludes with an example of applying Windows and Linux tools to diagnose heavily contended CPUs.

Chapter 4's coverage of "The Java monitoring tools" talks about "the JDK monitoring tools" that are specifically delivered with the Oracle/Sun JDK such as JConsole, VisualVM, and Java Mission Control. The section on "Java monitoring tools" also mentions IDEs (and monitoring plug-ins for the IDEs) and application server-specific monitoring tools.

The JVM Tool Interface (JVM TI) is introduced in Chapter 4 as a replacement for the deprecated JVMPI/JVMDI. This section is essentially a condensed version of the Introduction portion of the JVM Tool Interface 1.2 specification.

The Chapter 4 section "Understanding the different JDK tools" discusses tools provided with the Oracle/Sun JDK for monitoring (jps, jstat, jstatd), profiling (JConsole, VisualVM, Java Mission Control), and troubleshooting (jhat, jstack, jinfo, jmap, jsadebugd). The overviews of each of these tools and their respective syntax seem to be borrowed directly from the Oracle tools pages (online and similar to man pages), but the section does include some specifics about using these tools in the book's examples at the end of each tool's discussion. Even the categorization of the tools is the same as the main Oracle tools page.

Chapter 4 barely scratches the surface of using JConsole, but provides more detailed and nicely illustrated examples of using VisualVM, Oracle Java Mission Control, and the NetBeans Profiler. The chapter introduces Eclipse plug-ins and provides some focus on JVM Monitor and Test and Performance Tools Platform (Eclipse Memory Analyzer Tool is covered in a later chapter). Chapter 4 concludes by introducing and significantly covering JProfiler.

Chapter 5: Recognizing Common Performance Issues

Java EE 7 Performance Tuning and Optimization's fifth chapter outlines categories of performance issues, briefly describes each category, and then looks in more detail at a subset of the individual categorized performance issues. The sections on "Performance Symptoms" of each performance issue are probably the most interesting to me. For developers new to performance tuning, it could be helpful to have "performance symptoms" collected and associated with particular likely causes of the symptoms. The discussion of performance issues also hits on some commonly accepted practices to adopt and some commonly accepted practices to avoid.

The second major part of Chapter 5 deals with "client-side performance issues." In this case, "client-side" appears to be synonymous with (assumes) a web application and web browser. This portion of Chapter 5 introduces and illustrates with screen snapshots several browser-based tools such as Chrome Developer Tools, Chrome plugin Speed Tracer, Internet Explorer Developer Tools, and FireFox Developer Tools.

Chapter 6: CPU Time Profiling

Chapter 6 of Java EE 7 Performance Tuning and Optimization is the first of three chapters to focus on use of Java profilers. This chapter focuses on profiling CPU usage and the subsequent two chapters focus on profiling threading and memory usage. This sixth chapter introduces three Java profiling tools that feature CPU profiling capabilities: NetBeans Profiler, Java Mission Control (JMC), and JProfiler. These sections on using these three tools provide sharp screen snapshots (full color in electronic version) to demonstrate all three graphically-oriented tools in action.

After introducing the three profiling tools in action profiling CPU usage and method execution times, Chapter 6 moves onto using the results provided by those tools to identify and categorize potential causes of the most significant time-consuming operations. This includes high level discussion of how to resolve many of these types of issues and a little more detailed coverage of addressing issues related to inappropriate algorithms.

Chapter 7: Thread Profiling

Chapter 7 demonstrates using the same three Java profiling tools demonstrated in Chapter 6 (NetBeans Profiler, Java Mission Control, JProfiler), but this time used for profiling threads. The demonstration of these three profiling tools used for profiling thread usage is followed by some discussion on what the results might indicate.

The seventh chapter of Java EE 7 Performance Tuning and Optimization also looks at "options from the operating system to produce a thread dump for the executing Java process" (CTRL+Break and sending SIGQUIT signal [kill -3]). There is also a section on using the Oracle JDK tools jstack, JVisualVM, and Java Mission Control to dump heaps. The section on "Understanding the thread dump structure" provides a highly useful overview of the data included in a thread dump and how to use it. The chapter also introduces the Thread Dump Analyzer.

Chapter 7 includes very high level discussion of potential threading performance issues. It also points out that -XX:+PrintFlagsFinal and jinfo can be used with the Oracle JDK to determine which JVM flags are currently set. High level discussion on "detecting the root cause of a hung application" is also in Chapter 7. There is a very brief overview of dealing with thread-related issues at the end of the chapter.

Chapter 8: Memory Profiling

The eighth chapter of Java EE 7 Performance Tuning and Optimization is the concluding chapter of the three-chapter series on applying Java profiling tools and this chapter specifically covers memory profiling. The chapter illustrates using NetBeans Profiler and JProfiler to perform memory profiling. This chapter also describes what is contained in a garbage collection log before briefly demonstrating use of JVisualVM's plugin called Visual GC (the plugin version of the Oracle visualgc command-line tool that is downloaded separately from the JDK command-line tools) to see memory and garbage collection activities in the different heap memory pools.

Chapter 8 provides an overview of heap dumps and their content. It explains approaches that can be used to obtain a heap dump such as use of JVM options (-XX:+HeapDumpOnOutOfMemoryError and -XX:HeapDumpPath), Oracle JDK tools (jmap, JVisualVM, and Java Mission Control), and with Java profiler tools (NetBeans, Eclipse Memory Analyzer Tool, and JProfiler). This chapter also describes Object Query Language (OQL) and provides examples of OQL similar to those provided by the OQL Help that can be seen at http://localhost:7000/oqlhelp/ when running jhat against a heap dump on the local machine.

Chapter 8 wraps up with a nice high-level overview of the types of things that commonly lead to memory issues and OutOfMemoryError.

Chapter 9: Tuning an Application's Environment

Chapter 9 of Java EE 7 Performance Tuning and Optimization opens with a discussion of JVM tuning using the standard and non-standard Oracle JVM options. This includes high-level discussion of using JVM options related to garbage collection and different garbage collectors. There is a similar section on JRockit VM tuning options.

After covering JVM tuning, Chapter 9 moves onto application server tuning with general application server tuning discussion illustrated with examples specific to GlassFish and WebLogic application servers. The section "Tuning the Oracle GlassFish application server" states that it goes "through the configuration as specified in the GlassFish 4 performance tuning guide." In fact, this section is very close to a subset of what is in the GlassFish 4 Performance Tuning Guide but does offer a small number of screen snapshots not available in the guide. The chapter also includes brief, high-level overviews of "tuning the Oracle Weblogic application server" and "tuning web servers (HTTP servers)" (including Apache HTTP Server and Oracle Web Server).

"Tuning the operating system and hardware" rounds out Chapter 9 and is a very brief discussion of issues to consider and strategies for tuning operating system and hardware.

Chapter 10: Designing High-performance Enterprise Applications

The tenth chapter of Java EE 7 Performance Tuning and Optimization begins with the assertion, "Design is our key to a good performing enterprise application" and then mentions the objective of the chapter to "cover some of the design areas that we should consider while designing our enterprise applications." The first section in this chapter provides high level discussion on things to think about when designing and how there are trade-offs between different design objectives. This high level discussion includes comparing "potential performance impacts" of various considerations (such as security and framework choices) on performance.

Chapter 10 devotes a couple pages to high-level discussion of "performance anti-patterns." This is followed by sections on Service-Oriented Architecture (SOA) performance aspects and Resource-Oriented Architecture (ROA) performance aspects. The chapter concludes with coverage of data caching performance aspects.

Chapter 11: Performance Tuning Tips

Java EE 7 Performance Tuning and Optimization's Chapter 11 begins with a discussion on performance considerations during Agile software development. This discussion addresses when in the agile development process performance considerations should be made. It also introduces Test-Driven Development (TDD) and code reviews (manual and automated) as software approaches to improve performance tuning ability. The section on automated code reviews lists several code analysis tools as standalone tools and as NetBeans tools.plugins (such as PMD).

The "Java EE Performance Tuning Tips" section of Chapter 11 provides very brief, high-level descriptions of Java EE performance tuning tips categorized by Java EE component (web services, Enterprise JavaBeans, servlets/JavaServer Pages, JavaServer Faces, and Java Persistence API). The "Java Performance Tuning Tips" section provides a high-level overview of some common issues in Java SE code that lead to performance issues and mentions tips (coding and JVM options) to reduce or avoid those issues. There is also discussion of logging and exception handling best practices that can help performance.

Chapter 11's section "Using the javap tool to understand micro-optimizations" provides an overview of one of my favorite Oracle JDK command-line tools, javap, to determine how changes are impacting the byte code. The "Database performance tuning tips" section of Chapter 11 discusses a few database performance tips such as use of SQL optimizer hints and batch processing. Chapter 11 ends with a section on "Client-side optimization" that addresses "presentation layer performance." The author introduces and briefly illustrates PageSpeed Insights as part of this client-size optimization discussion.

Chapter 12: Tuning a Sample Application

The objective of the final chapter of Java EE 7 Performance Tuning and Optimization is "to demonstrate the performance tuning activities of a small application so that we can practice some of the activities that we have learned in this book." The example used here is called ExcellentSurvey and numerous pages are devoted to identifying and fixing performance-related issues in this ExcellentSurvey sample application. The chapter's summary also summarizes the book as a whole.

General Observations
  • Java EE 7 Performance Tuning and Optimization covers a lot of ground. It is mostly at an introductory level. Much of the content in this book is available online, but the book brings all of this content together in a single source. Readers likely to benefit most from reading this book are those with minimal Java performance tuning experience. The book provides a wide overview of available tools, tips, techniques, and best practices related to Java performance tuning. This book is not as deep as a book like Java Performance, but is instead targeted at higher level performance tuning concepts and approaches.
  • Although Java EE 7 Performance Tuning and Optimization does cover performance concepts related to Java EE application servers and Java EE, much of what it covers can be applied even more generally to all types of Java-based/JVM-hosted applications.
  • Screen snapshots are generally sharp and readable. They tend to be full color in the electronic version (PDF) of the book that I reviewed.
  • Code listings are in black font on white background (no color syntax) and there are no line numbers included in the code listings.
  • A flowchart for troubleshooting Java performance issues is built as the book progresses and this is a good starting point for someone wanting a systematic approach to resolving performance issues.
  • Java EE 7 Performance Tuning and Optimization is lengthier than it needs to be.
    • Content reproduced from Oracle's sites and other sites could have been summarized and referenced rather than being reproduced. The section on Linux monitoring tools used the better approach of listing the tools and referencing where to get the details on those tools. I think it would have been better if other parts of the book had followed that same tactic instead of merely reproducing content from those other sources.
    • Some of the overview and high-level concepts chapters seemed overly verbose to me, but that could be more a matter of taste. I prefer books to dive right into technical detail with only a little description and background; others prefer more background and discussion.
  • Some portions of Java EE 7 Performance Tuning and Optimization were difficult to understand due to awkward sentences or imprecise wording.
    • For example, the fourth chapter and sixth chapters discuss use of profiling tools to "catch HotSpot areas." At first, I was trying to figure out how the paragraph in Chapter 4 was specific to the HotSpot virtual machine because it was referenced with the same trademarked-style "HotSpot." Only after its second use in the same section, where "HotSpot areas" were described as "underperforming areas," did I confirm my suspicion that these references were intended to be to "hot spots" rather than to "HotSpots. Once I realized this, I was able to use context to distinguish when the author was talking about the HotSpot JVM versus talking about "Hot Spots" spots where code execution times are longer or more memory is being consumed.
    • A second example is this phrase: "Missed cache hits are very costive..."
    • An otherwise interesting brief discussion on potential memory issues due to inconsistencies between equals(Object) and hashCode() is a bit marred by a typo (uses "mush" rather than "must") "In Java, if any two objects are equal, then their hash code values mush be equal as well."
    • There are numerous more examples like these and I feel that this book could have used a round or two more of text editing. More editing would have probably helped tighten the book up a bit as well.
  • Java EE 7 Performance Tuning and Optimization makes heavy use (sometimes word for word identical text) of some significant online resources on JVM and application server performance tuning. These excellent online resources include:
  • Although Java EE 7 Performance Tuning and Optimization generally provides broad coverage of performance tuning Java EE 7 applications, it is important to note in a book review the things it doesn't cover or only covers very briefly. These include:
    • Specific Java EE implementations (specific application servers) - this is understandable as entire books and guides can be written to each particular Java EE application server implementation.
    • Java Management Extensions (JMX) - JMX is briefly referenced in a few locations, but its direct use is relatively ignored (it is indirectly discussed in terms of much of the coverage of VisualVM and other JMX-powered tools).
    • Virtual Machine Environments - Although operating system level performance tuning is covered multiple times in this book, running Java applications in virtual machines is not discussed in any level of significant detail.
  • Readers of Java EE 7 Performance Tuning and Optimization will likely want to download the associated code samples. In particular, the code examples that intentionally have problems that can be diagnosed with the described tools can provide practice on applying those tools.
  • Because different reviewers have different tastes, different background, different experiences, and different preferences, I think it's important to read book reviews from multiple sources. A different perspective on Java EE 7 Performance Tuning and Optimization is available in Abhishek's post Book Review: Java EE 7 Performance Tuning and Optimization.

Java EE 7 Performance Tuning and Optimization is a lengthy book that at times felt lengthier to me than it needed to be. However, developers new to Java performance (both EE and SE) may appreciate this extra background. Significant portions of some of the earlier chapters are nearly word-for-word the same as online Oracle Java documentation, but some readers new to these tools may find this more convenient than reading the original sources. Experienced Java EE developers who have had to do basic tuning of their applications in the past are likely to find a few new ideas or concepts in this book, but the people most likely to benefit are those with relatively little or no Java performance experience. For them, this book provides an overall view of the landscape of tools and concepts related to Java EE (and really general JVM) performance tuning.

The book's strengths include clear screen snapshots that are especially useful in demonstrating graphical tools and discussion and demonstration of applying tools to code examples with performance issues. Its weaknesses include numerous grammar issues and typos that make the text less readable than it otherwise would have been and stating the same things too many times and being lengthier than it needs to be. On one hand, the repetition seems monotonous to someone familiar with the concepts, but, on the other hand, this repetition might benefit those new to the concepts.

No comments: