Performance Profiling Java Applications Using the NetBeans Profiler

Gregg Sporar, Sang Shin


 

NetBeans IDE 6.0 includes a powerful profiling tool that can provide important information about the runtime behavior of your application. The NetBeans profiling tools easily enable you to monitor thread states, CPU performance, and memory usage of your application from within the IDE, and imposes relatively low overhead.

In this hands-on lab, in the first exercise, you are going to learn how to profile only part of an application (instead of the entire application or entire classes used by the application) using root method selection scheme.  In the second exercise, you will learn how to use profiling points for breakpoint-like profiling.  In the third and fourth exercises, you are going to learn how to capture thread information and CPU performance respectively.


Expected duration: 120 minutes (excluding homework)




Software Needed

Before you begin, you need to install the following software on your computer. 


Operating Systems/platforms you can use

Change Log


Things to do (by Authors)


Lab Exercises


Exercise 0: Perform JDK calibration


In this exercise, you will perform calibration against the JDK running on your system.  The calibration is required in order to perform profiling. It does not affect in any way the behavior of your Java applications.  This needs to be done only once.

Note: Instrumenting the bytecode of the profiled application imposes some overhead. To guarantee the high accuracy of profiling results, NetBeans Profiler needs to collect calibration data in order to "factor out" the time spent in code instrumentation. You need to run the calibration process for each JDK you will use for profiling. The calibration data for each JDK is saved in the .nbprofile directory in your home directory. You are prompted to run the calibration the first time you invoke NetBeans Profiler. You are also prompted if the calibration data for the local machine and JVM is unavailable.

(0.1) Run profiler calibration


1. Select Profile from top-level menu and select Advanced Commands->Run Profiler Calibration.  (Figure-0.20 below)


Figure-0.20: Run Profiler Calibration

Figure-0.21: Select Java Platform to calibrate

Figure-0.22: Information dialog box


Exercise 1: Perform profiling on "Java2D" sample application

One of the benefits of using NetBeans profiler is that it lets you profile only the parts of your application that are really of interest. You can, if you want, profile the entire application, but that can have a big impact on performance.

In this exercise, you are going to profile the entire application and see the performance impact of it.  Then you are going to learn how to profile only part of the application by the scheme of root method selection, thus limit the overhead imposed by the profiler to only those parts of an application that you suspect have a performance problem.

  1. Open, build, and run "Java2D" sample application
  2. Profile the entire  application (just to see how slow it gets)
  3. Profile part of an application through root method selection

(1.1) Open, build, and run "Java2D" sample application (without profiling)

1. Open Java2D NetBeans project. 


Figure-1.11: Open a project


Figure-1.12: Open Java2D project
2. Build and run the application.

Figure-1.13: Run various Java2D features

Figure-1.14: Exit the application

                                                                                                                       return to top of the exercise


(1.2) Profile the entire application (just to see how slow it gets)


1. Profile Java2D sample application.

Figure-1.21: Start Profiling Java2D application

Figure-1.22: One time change of the build script of the project confirmation

Figure-1.23: Analyze Performance

Figure-1.24: Result of running the entire application

2. Observe the number of methods that are being profiled.

Figure-1.25: Number of methods being profiled (instrumented)

3. Display the Live Results.

Figure-1.26: Live Results

4. Stop profiling.

Figure-1.27: Stop profiling the entire application

                                                                                                                       return to top of the exercise



(1.3) Profile part of the application through "Root Method"


Since you now know the perils of profiling an entire application, in this step, you are going to take a different approach.  What some profiling tools force you to do is define filters of package and/or class names for what should or should not be instrumented by the profiler. The NetBeans Profiler supports filters, but it also has a more powerful feature: root methods.

1. Open Rotator3D.java.

Figure-1.31: Find render(..) method

2. Add render(...) method as a root method for profiling

Figure-1.32: Add render(..) method as a root method for profiling

Figure-1.33: Select Settings Configuration

3. Add step(..) as the second root method for profiling.

Figure-1.34: Add step(..) ad a root method

Figure-1.35: Select Settings Configuration

4. Run the application with Profiling

Figure-1.36: Profile the application with 2 root methods

Figure-1.37:  Part of application is profiled

Figure-1.38: Observe that render(..) and step(..) methods are defined as root methods

Figure-1.39: Start the profiling

5. Run various part of the Java2D application.

Figure-1.40: Run various part of the Java2D application

6. Run Colors of the application.

Figure-1.41: Run Colors

7. Observe the number of methods that are being profiled.

Figure-1.42: Number of instrumented methods under 2 root methods

8. Take the snapshot.

Figure-1.43: Take the Snapshot of the profiling

                                                                                                                       return to top of the exercise

Summary

In this exercise, you learned how to profile only part of an application by designating methods of an application as root methods.

                                                                                                                        return to the top

Exercise 2:  Profiling Points

You can place profiling points in your source code to more precisely control the collection of profiling results, similar to debugger breakpoints.

You can use profiling points to automatically trigger actions when certain conditions are met, such as execution of a line of code, time elapsed, or memory used. Profiling points can trigger heap dumps, the resetting of results collected, the running of a load generator script, and the taking of a results snapshot.

For example, there are situations where you want to control the features the profiler provides at specific points in your application and/or at specific points in time.  Profiling points allow you to do that.  So they are very similar to breakpoints in a debugger.  To take just one example, you can set a profiling point to clear the profiler's buffer of collected results at the beginning of a method and then set a second profiling point at the end of the method to save off the results that accumulated during the execution of that method.

  1. Build and run "Anagram Game" sample application
  2. Insert profiling points
  3. Profile the application with profiling points


(2.1) Build and run "Anagram Game" sample application


1. Create a new NetBeans project from Sample applications (that are being shipped with NetBeans IDE).

Figure-2.11: Create Anagram Game project

Figure-2.12: Name and Location

                                                                                                                       return to top of the exercise


(2.2) Insert profiling points


1. Insert a profiling point at the start of the constructor of the Anagram class.

Figure-2.21: Insert Profiling Point

Figure-2.22: Select Reset Results

Figure-2.23: Customize Properties of the profiling point

2. Insert a profiling point at the end of the constructor of the Anagram class.

Figure-2.24: Insert Profiling point


Figure-2.25: Select Take Snapshot on the profiling point

Figure-2.26: Customize Properties of the profiling point

Figure-2.27: Profiling Points tab window

                                                                                                                       return to top of the exercise


(2.3) Profile "Anagram Game" sample application (with two profiling points)


1. Profile the application.

Figure-2.31: Profile the application

Figure-2.32: Enable Profiling of AnagaramGame

Figure-2.33: Analyze Performance dialog box

2. Observe that the AnagramGame application GUI appears. (Figure-2.24 below)


Figure-2.24: AnagramGame GUI

3. See the profiling result.

Figure-2.25: Display the report

Figure-2.26: Take Snapshot at Anagrams:64 page

Figure-2.27: Result

4. Display the source code.

Figure-2.28: Go to Source

Figure-2.29: Display the source code

5. Stop the profiling.
                                                                                                                        return to top of the exercise

Summary

In this exercise,  you learned how to use profiling points.

                                                                                                                        return to the top


Exercise 3: Monitor Threads Status


In this exercise, you learn how to use the NetBeans Profiler to monitor thread state in a J2SE application. This will allow you to diagnose a performance problem in the sample application.

The Swing library provides graphical user interface components for J2SE applications. Multiple threads are used by the Swing library and the NetBeans Profiler is a powerful tool that can be used to understand the amount of processing that occurs on each thread. This understanding can be used to solve performance problems.
  1. Build and run "MonitorThreads" sample application and observe the undesirable behavior
  2. Profile the application and display thread information
  3. Look under the hood of the ill-behaving application
  4. Correct the badly designed code fragment and profile it again

(3.1) Build and run "MonitorThreads" sample application and observe the undesirable behavior


1. Open MonitorThreads NetBeans project. 
2. Build and run the application.
3. Observe the undesirable behavior of the application

Figure-3.11: Start button does not work as expected

Figure-3.12: The Exit button does not work as expected.

Figure-3.13: Obstructed view of the window

4. Even notification occurred in 30 seconds.

Figure-3.14: Notification message box

                                                                                                                       return to top of the exercise


(3.2) Profile the application and display the thread information


1. Run the application with profiling.

Figure-3.21: Profile the application

Figure-3.22: Enable Profiling of MonitorThreads

Figure-3.23: Analyze Performance pane

2. Click Start button when application GUI appears. (Figure-3.24 below)


Figure-3.24: Click Start button

3. Observe the Threads status.

Figure-3.26: thread information

<Study points>  The color of a thread indicates different state of the thread.
This graph shows why the application is not responding. The thread labeled AWT-EventQueue-0 is the Event Dispatch Thread (EDT) used by Swing to process window events. In a well behaved Swing application, the EDT spends most of its time waiting and very little time running; it should only run for the brief amount of time required to dispatch an event. If the event handlers in the application do not return quickly, however, then the program will become unresponsive, just like in this example.
</Study points>

4. Close the application and stop the profiling.
                                                                                                                       return to top of the exercise


(3.3) Look under the hood of the ill-behaving application


1. Display the DisplayForm.java, which contains badly designed code.

Figure-3.31: Badly designed code

                                                                                                                       return to top of the exercise


(3.4) Correct the badly designed code and profile again


1. Remove the badly designed code from line 122 to line 128. The result should be as shown in Figure-3.41 below.


Figure-3.41: Remove the badly designed code fragment

2. Use SwingWorker thread.

Figure-3.42: Uncomment the code fragment that uses SwingWorker

3. Run the application with profiling.

Figure-3.43: Profile the application with corrected code

Figure-3.44: Monitor Application

4. Click Start button and observe that it responds correctly. (Figure-3.45 below)


Figure-3.45: Click Start button

5. Observe the thread status.

Figure-3.46: Thread status

6. Display the Threads Details.

Figure-3.47: Thread Details

Figure-3.48: Thread details

Figure-3.49: Details information

5. Stop the profiling.
                                                                                                                       return to top of the exercise

Summary

In this exercise you learned how to use the Profiler to start an application and how to interpret the Profiler's thread information graphs in order to track down a performance problem in a Swing application.

                                                                                                                        return to the top




Exercise 4: Profile "ComputePrimeNumberWebapp" application


In this exercise you learn how to use the Profiler to determine the amount of time spent in one of an application's methods.  CPU performance problems are usually associated with specific features in an application. For example, in a reporting system one report might run more slowly than the others. Profiling just the portion of an application that is experiencing performance problems can dramatically reduce the overhead imposed by the profiler. In this exercise you will use the NetBeans Profiler to examine CPU usage in a web application.
  1. Build and run "ComputePrimeNumberWebapp" sample application
  2. Run the "ComputePrimeNumberWebapp" application in profiling mode
  3. Analyze the profile data.

(4.1) Build and run "ComputePrimeNumberWebapp" sample application


1. Open Java2D NetBeans project. 
2. Build and run the application and observe the performance problem.

Figure-4.11: Welcome to We-have-performance-problems.com page

Figure-4.12: Experience the performance problem

3. Run the application with the optimized code.

Figure-4.13: Do the prime number generation with optimized code

Figure-4.14: Result of using optimized prime number generation code

                                                                                                                       return to top of the exercise


(4.2) Run "ComputePrimeNumberWebapp" application in profiling mode


1. Run the application in profiling mode.

Figure-4.21:  Profile the application

Figure-4.22: Confirm the profiling

2. Define a new root method.

Figure-4.23: Define the root method

Figure-4.24: Add Root method

Figure-4.25: Select a root method

Figure-4.26: processRequest(..) method is added as a root method

Figure-4.27: 1 root method is displayed

3. Observe the result.

Figure-4.28: Result is displayed.

4. Take the snapshot of the profiling.

Figure-4.31: Profiling result

                                                                                                                       return to top of the exercise


(4.3) Modify Profiling


1. Modify the profiling.

Figure-4.41: Modify Profiling Session

Figure-4.42: Profile only project classes

3. Observe the result.
4. Take the snapshot.
Figure-4.43: Profiling result.

Figure-4.44: Source code of the calculate()

5. Stop the profiling.
                                                                                                                       return to top of the exercise


Summary

In this exercise you learned how to have the Profiler do performance analysis on a method.

                                                                                                                                return to the top




Homework Exercise (for people who are taking Sang Shin's "Advanced Java SE Programming online course")


<to be provided>