Profiling a Server Application
This tutorial provides a brief introduction to the AppPerfect Java Profiler product of the AppPerfect Java Profiler using a set of hands-on practice exercises. This tutorial assumes you have successfully downloaded and installed AppPerfect Profiler on your machine with the default options. Apart from this pre-requisite, this tutorial is self contained. Instructions given below are Windows-specific; if you are using a non-Windows machine, please use equivalent commands/instructions.
This document is divided into following sections
Within each section, multiple exercises are listed. Each exercise forms a logical unit which is expected to take no more than a few minutes to perform. All exercises use the PetStore demo application running inside Apache Tomcat. The modified PetStore application and Tomcat are both bundled with this product.
All exercises assume you have installed the product in C:\AppPerfect\Profiler folder and will be referred as Profiler_HOME henceforth in tutorial. If you have installed the product in some other folder, modify the instructions below appropriately.
This tutorial is not intended as a comprehensive training guide. Please refer to the product documentation for detailed information. However, this tutorial will give you a very good overview of the products and substantially improve your productivity with the product.
Creating Project
Exercise 1: Launch AppPerfect Java Profiler
- Click on Start -> Programs ->AppPerfect Profiler x.x.x -> AppPerfect Java Profiler
- On launching AppPerfect Java Profiler a Welcome page will be displayed. Go through the brief description
given for product.
NB: Welcome page is displayed only when Profiler x.x.x is launched and last time no project was opened.
Exercise 2: Creating a Project
- Launch the Project Wizard by clicking File ->New... menu option. The New Project wizard will be launched.
- Go through the instruction provided on top of the General tab.
- Keep the default project name and location for the purpose of this exercise. We don't have to provide "Notification" settings or "Target Application's Machine" settings for this exercise. Click on the Next button.
- We dont have to import any existing project so click Next on Import Tab.
- In the Source tab click on the "Add File" button on the r.h.s., select "Files of type:" as *.jsp in the Open dialog and add jsp files from the location "Profiler_HOME\tomcat\webapps\petstore"
- Now click on the "Add Folder" button on the r.h.s. and select the folder "Profiler_HOME\tomcat\webapps\petstore\src". Click the Next button.
- Use the default JDK which is bundled with AppPerfect Java Profiler and click on the Next button.
- In the "Environment" tab we will provide the classpath for the sources added in the project.
- Click on the "Add File" button on the r.h.s. and navigate to the location - "Profiler_HOME\tomcat\webapps\petstore\WEB-INF\lib" - select all the jars present in the "lib" folder.
- Click on the "Add File" button on the r.h.s. and navigate to the location - "Profiler_HOME\tomcat\lib" - select "ant.jar", "el-api.jar", "jsp-api.jar" and "servlet-api.jar" jars and add them to classpath.
- Click on "Verify Classpath" button to validate the classpath.
- Classpath validation dialog will be launched and the classpath will be verified. A message saying that the classpath specified is correct should be displayed. Click on the "OK" button. Click on the Next button.
- In the "Target" tab select "Target Application type" as "Server/ Web Application".
- Keep the default URL in the "Starting URL" field i.e., "http://localhost:8384/petstore". Select the "AppServer Settings" tab.
- Select the "Specify AppServer Settings" checkbox and select "Tomcat_5.x/6.x" from the drop down menu.
- Specify the Server Home path - "Profiler_HOME\tomcat" and the "Startup file" for the server - "Profiler_HOME\tomcat\bin\catalina.bat".
- Select the "Launch server automatically (When required)" checkbox. Click on the Finish button.
- If applications asks for Starting URL validation, click on 'No' button. Normally Confirmation for Starting URL validation is not asked when "Launch server automatically (When required)" is checked. A confirmation message saying that the project is saved will be displayed. Click on the OK button.
Now using this Project we will create tests to demonstrate the functionalities of AppPerfect Java Profilfer product.
AppPerfect Java Profiler
NB:Please follow the steps provided in the "Creating Project" section to first create a Project, then proceed further.
Exercise 1: Define a Java Profiler project
- Once the Project is successfully created another dialog - Define Project Properties dialog - will be displayed.
- Read the instructions at top of each tab.
- Go through the descriptions for Profiling Types. Keep the default Development Mode Profiling and select Profiling Options tab.
- Study the descriptions of the three profiling options. You can configure Filters using Customize Filters... option. Use default values.
- Study the descriptions of Instrumentation Options. Use the default: Dynamic Instrumentation enabled.
- Study the changes indicated in the Launch Instructions tab. AppPerfect Java Profiler must modify your application server's startup file to add instructions to load the profiling agent, etc. This tab shows the exact changes that needs to be made to the startup script. AppPerfect Java Profiler understands how to configure startup scripts for most commonly available application servers. Also, since it does not modify the original file and instead makes a copy to store the modifications, it is almost always desirable to let AppPerfect Java Profiler make the changes. Click OK button.
- Click through all the menu items to familiarize yourself with the available features and how to access them. Viewing through all menu items will provide a reasonable overview of the application.
- Click on Tools ->Options... menu item. Click on the "Browsers, JDKs & DBs" node and ensure that the JDK path has been set correctly. This is the path provided for JDK during installation of AppPerfect Java Profiler. You may modify the path or add new JDK through this dialog box. It is critical that a correct version of JDK is available for AppPerfect Java Profiler to perform correctly.
- Click Help -> Table of Contents menu item to see AppPerfect Java Profiler product documentation.
Exercise 2: Start Profiling
- To start profiling click on Project -> Run from the menubar.
- A message will be displayed confirming that a modified appserver startup file called catalina_AppPerfect.bat has been created.
- This will launch Tomcat with PetStore running. The status of Tomcat startup could be seen in the Server Console window. Once Tomcat is fully started and available for servicing requests, you will see a message similar to: "INFO: Server startup in XXX ms" where XXX will be a number.
- Observe the dynamic updation of data in the default Project Summary view. You can see various profiling metrics such as heap memory usage, object instance count and Thread count.
Exercise 3: Memory Profiling
- AppPerfect Java Profiler provides detailed information about the profiled JVM's heap memory. Since memory allocation/de-allocation is a key determinant of performance, understanding the heap memory composition and behavior is critical in designing high performance Java applications.
- Click on Memory Profiling to view summary of Memory Profiling
- Click on Memory-> Garbage Collection node in the tree on the left. Four charts are displayed showing details on each call to the Garbage Collector. For additional details on GC, click on the GC Details tab at the top of the screen.
- Click on Memory ->Class-wise Allocation node in the tree on the left. A tree-table with all the packages/classes loaded will be displayed. Double clicking on a Package or a class will take you to the Allocations Tree tab which will show you allocation tree of selected class. Expand the tree to needed depth to see the allocation hierarchy.
- Click on Memory ->Method-wise Allocations node. Method-wise Allocations shows details of methods where allocations of objects has happened, here you can get details of what all classes were instantiated from a particular method. This is useful to find method where max allocation is happening.
Exercise 4: CPU Profiling
- CPU Profiling helps you understand the breakdown of how time is used by the CPU when an action is processed
- Click on CPU Profiling node to view CPU Profiling summary.
- Click on CPU -> Method Details node in the tree on the left. A table is displayed with details on all classes and their methods, including the number of invocations, CPU time consumed, etc. At the bottom you can see incoming and outgoing invocation trees with respect to selected method. You can filter view to see hotspot methods by providing desired hotspot threshold value. Here methods that take more than the "Threshold Value", as specified when the project was created, are displayed. Hotspot views are very helpful in identifying classes and methods that take the bulk of the processing time.
- Click on CPU ->Invocation Tree node in the tree on the left. The view might take a few moments to refresh or click on the Refresh button on l.h.s top. Once refreshed, you will see a tree table with each thread displayed along with the time it took. Expand the node "Thread - main" to view details on how your application's processing breaks down for CPU usage.
Exercise 5: Thread Profiling
- The process of identifying, monitoring and debugging issues related to the various threads in your multi-threaded application is called Thread Profiling.
- Click on Threads Profiling node to view thread profiling summary.
- Click on Thread Profiling-> Threads node in the tree on the left. A chart displaying the state of each thread started by the target application are displayed. Note that the left edge of each bar represents the start time of the application and right edge represents the current time. During this time, the state of any of the threads may have changed. This view helps graphically identify any threads that may be blocked or waiting.
- Click on Status tab in the Threads view. This table displays the current status and other information about each thread. If there are too many threads, you may sort the data based on any of the columns.
- Click on Thread Profiling-> Monitors node in the tree on the left. A monitor is a resource (an object, system resource, etc.) which two are more threads attempt to access concurrently. Concurrent access forces all the other threads except for one accessing the monitor to wait for the monitor. This view details all such monitors and details about them. Higher the monitor contention rate, the more likely your application is not properly multithreaded and will perform poorly.
- Click on Thread Profiling-> Deadlocks node in the tree on the left. Click on the Detect Dead Lock button on the r.h.s. If a deadlock condition was detected (a cyclical waiting of two or more threads), it would be graphically displayed in this view.
Exercise 6: Detecting Memory Leaks
- Detecting memory leaks is a key function of the AppPerfect Java Profiler. A memory leak is defined as an application error in which objects that were thought to have been disposed are not disposed because of a reference to that object from somewhere in the application. Over time, if such objects continue being created without getting garbage collected, they will grow to consume all available memory, leading to application shutdown. Memory leak detection requires significant knowledge of the underlying application being profiled.
- Start a browser and connect to the PetStore application (http://localhost:8384/petstore). Click on "enter the store".
- Select "Memory Leaks" node in the l.h.s. tree and click on "Start Scenario..." button.
- AppPerfect Java Profiler will force a Garbage Collection and register all live objects present in memory after the GC.
- A message asking to execute the test scenario in your application will be displayed. Click on the OK button.
- Now go through the action of ordering "Adult Amazon Parrot". Check out, and then log out. Close the browser.
- Click on "Stop Scenario" button.
- A message will be displayed informing that Memory leak detection scenario has been completed and do you want to take a heap snapshot to view the allocations and reference details of the reported leak objects. Click on the Yes button.
- Now the focus will move to Heap Browser node. A dialog will be displayed asking whether you want to view details for any specific class. For the sake of this exercise will not specify any particular class. Click on the OK button to take snapshot. This will take a few seconds.
- As soon as the snapshot is taken, the 'Packages' tab will be in view. Double click on a Class to view "Allocation Tree" view. Expand the tree to needed depth to see the allocation hierarchy. Further details such as each instance of the class can be obtained by right-clicking on a class and selecting the option "Show Object Instances" or by selecting the tab Object Instances.