Container: Developing an Application - Remote Profiling

Overview

Profiling is a form of performance analysis for any type of application. There exist already-built profilers for many kinds of applications. Using a profiler, we can gather information about utilization of various resources like memory, the CPU, threads, exceptions, and more.

Profilers give the developer lots of useful information on how the application utilizes resources. Often applications are very large in scope, so without a profiler it could be very difficult to identify problem areas. For example, a profiler could expose a memory leak that causes the application to consume more and more memory until the system runs out of memory and crashes. We will be specifically looking at the YJP (YourKit Java Profiler) and how to attach it to an Openshift application.

Remote Profiling Example

In this example, we will be attaching the YJP to the Canvas Course Manager application. We can do this relatively simply with a few additions to our application’s Dockerfile. This URL is a direct link to build 75 of YJP. Given this link, we can instruct our Dockerfile to attach this profiler to our application. All we need to do is add a few lines to our Dockerfile:

WORKDIR /usr/local
RUN wget https://www.yourkit.com/download/yjp-2017.02-b75.zip \
&& unzip yjp-2017.02-b75.zip

First, we specify the working directory as /usr/local. Next, we run the wget command to download the profiler from the URL given and unzip it in the working directory.

The last addition to our Dockerfile is the -agentpath option which we add to the JAVA_OPTS environment variable as follows:

ENV JAVA_OPTS="
-agentpath:/usr/local/yjp-2017.02/bin/linux-x86-64/libyjpagent.so=disablestacktelemetry,disableexceptiontelemetry,delay=10000,sessionname=Tomcat \"

This line enables profiling with the -agentpath option as the java app server (in our case, tomcat) will read this variable upon start up. The agent library path depends on your OS. You can find how to enable this option for each OS here.

Since our application is running in a Linux environment, we specify

:/usr/local/yjp-2017.02/bin/linux-x86-64/libyjpagent.so

Additionally, we can remove =disablestacktelemetry,disableexceptiontelemetry from the -agentpath option to run a more development focused configuration of the profiler. This will cause a performance hit but it will provide much more information.

That's all. We have successfully attached the YourKit Java Profiler to our application. You can view the updated Dockerfile here.

Note:The use of profilers is discouraged within production environments. The information they provide is valuable, but their use will result in slower application performance. Application profiling should always be done in pre-production.

Last Updated: 
Wednesday, September 12, 2018