Profile running applications using machine learning

with Amazon CodeGuru Profiler

In this tutorial, you learn how to use machine learning to profile running applications in the cloud with Amazon CodeGuru Profiler.

Amazon CodeGuru Profiler collects runtime performance data from your live applications, and provides recommendations that can help you fine-tune your application performance. Using machine learning algorithms, CodeGuru Profiler can help you find your most expensive lines of code and suggest ways you can improve efficiency and remove CPU bottlenecks.

In this tutorial, you learn how to:

  1. Create a new profiling group
  2. Download sample source code to profile
  3. Build and configure the sample application
  4. Run the sample application and review recommendations
  5. Clean up tutorial resources

This tutorial is AWS Free Tier eligible.

Prerequisites

About this Tutorial
Time 10 minutes                                  
Cost AWS Free Tier eligible
Use Case Machine Learning
Products Amazon CodeGuru
Audience Developer
Level Beginner
Last Updated February 15, 2021

Before you begin

You must have an AWS account to complete this tutorial. If you do not already have an account, choose Sign up for AWS and create a new account.

Already have an account?
Log in to your account

Step 1. Create a profiling group

Complete the following steps to create a profiling group in Amazon CodeGuru.

You must create a profiling group to receive application data sent by the Amazon CodeGuru Profiler profiling agent. A profiling group is a set of applications that are profiled together as a unit.

Note: For more information, see Working with profiling groups in the Amazon CodeGuru documentation.


a. Sign in to the Amazon CodeGuru console. In the Getting started box, choose CodeGuru Profiler and then choose Get started.

Get started with CodeGuru Profiler

b. On the Create profiling group page, in the Name field, type Tutorial-WithIssues. (This name is referenced later by the Amazon CodeGuru Profiler profiling agent.) For Compute platform, choose Other. Then, choose Create.

Create a profiling group in CodeGuru Profiler

Step 2. Download sample source code to profile

In this step, you use sample application source code to connect to Amazon CodeGuru Profiler. This sample application is available on GitHub for download from aws-samples.

Complete the following steps to download the sample source code.

Note: Make sure you have the prerequisite tools installed on your workstation before proceeding with this step. For more information, see Getting started with CodeGuru Profiler in the Amazon CodeGuru Profiler documentation.


a. On your workstation, open a terminal and run the following command to download the sample source code using Git:
git clone https://github.com/aws-samples/aws-codeguru-profiler-demo-application.git

b. Run the following command to change directories into the source code:
cd aws-codeguru-profiler-demo-application

Step 3. Build and configure the sample application

In this step, you use build and configure the sample application to be profiled with CodeGuru Profiler. You use Maven to build the application, then create a few resource dependencies, and finally configure some sample application parameters.

Complete the following steps to build and configure the sample application.

Note: For more information, see Getting started with CodeGuru Profiler in the Amazon CodeGuru Profiler documentation.


a. In the aws-codeguru-profiler-demo-application directory on your terminal, run the following command to build the sample application with Maven:
mvn clean install

b. Run the following command to set up AWS credentials:
aws configure

c. Run the following command to create an Amazon S3 bucket:

Note: The S3 bucket name must be unique. Make sure to replace <BUCKET-ID> with a unique identifier, such as the current date/time and your initials in the format demo-application-test-bucket-YYYYMMDDHHMM-[initials]

For example, demo-application-test-bucket-202102150123-JD

This S3 bucket is used by the demo application to store images for processing. The demo application uses image processing since it is a CPU intensive process.

aws s3 mb s3://demo-application-test-bucket-<BUCKET-ID>

d. Run the following command to create an Amazon Simple Queue Service (Amazon SQS) queue.

This Amazon SQS queue is used by the demo application to queue messages for images found in the S3 bucket for processing.

aws sqs create-queue --queue-name DemoApplicationQueue

e. Run the following commands to set the environmental variables and configure application parameters.

Note: Make sure to replace <YOUR-ACCOUNT-ID>, <YOUR-AWS-REGION>, and <BUCKET-ID> with your values. For your AWS Region, specify the Region parameter name, such as us-east-1.

export DEMO_APP_SQS_URL=https://sqs.<YOUR-AWS-REGION>.amazonaws.com/<YOUR-ACCOUNT-ID>/DemoApplicationQueue

export DEMO_APP_BUCKET_NAME=demo-application-test-bucket-<BUCKET-ID>

export AWS_CODEGURU_TARGET_REGION=<YOUR-AWS-REGION>

export AWS_CODEGURU_PROFILER_GROUP_NAME=Tutorial-WithIssues

Step 4. Run the sample application and review recommendations

In this step, you use run the sample application and then use the Amazon CodeGuru Profiler console to visualize metrics and review recommendations.

Complete the following steps to run the application and review recommendations.

Note: For more information, see Working with Visualizations in the Amazon CodeGuru Profiler documentation.


a. In the aws-codeguru-profiler-demo-application directory on your terminal, run the following command to run the application with the CodeGuru Profiler profiling agent:
 
Then, leave the application running while you move on to the next step.
java -javaagent:codeguru-profiler-java-agent-standalone-1.1.0.jar -jar target/DemoApplication-1.0-jar-with-dependencies.jar with-issues

b. In the Amazon CodeGuru console, choose Profiling groups, then choose the Tutorial-WithIssues profiling group you created for this tutorial.

Note: It will take several hours for Amazon CodeGuru Profiler to generate recommendations for your application. If you do not want to wait for this process to complete you can alternatively use the demo profiling group named {CodeGuru} DemoProfilingGroup-WithIssues. This demo group is pre-populated with data from the same application you are running in this tutorial.


c. Choose Recommendations.


Each performance improvement recommendation explains why CodeGuru Profiler recommends a change in your code. CodeGuru Profiler gives you suggestions on how and where to improve your code.

In this example, CodeGuru Profiler observed the Recreation of AWS SDK Service Clients in the code which consumed 18.57% of the time and results in an estimated cost of $398/year. CodeGuru Profiler is recommending resolution steps to create only one or as few AWS SDK service clients as possible.

For more infromation on recommedations, see Working with anomalies and recommendation reports.

Step 5. Clean up

In this step, you terminate the resources you used in this lab.

Important: Terminating resources that are not actively being used reduces costs and is a best practice. Not terminating your resources will result in charges to your account.


Stop the running application:

In your terminal, press Ctrl+C to terminate the running application.

Delete the Amazon SQS queue:

In your terminal, run the following command to delete the Amazon SQS queue:

Note: Make sure to replace <YOUR-AWS-REGION> and <YOUR-ACCOUNT-ID> with your values.

aws sqs delete-queue --queue-url https://sqs.<YOUR-AWS-REGION>.amazonaws.com/<YOUR-ACCOUNT-ID>/DemoApplicationQueue

Note that this operation will take some time to run as all files must be deleted from the Amazon S3 bucket.

Delete the Amazon S3 bucket:

In your terminal, run the following command to delete the Amazon S3 bucket:

Note: Make sure to replace <BUCKET-ID> with your value.

aws s3 rb --force s3://demo-application-test-bucket-<BUCKET-ID>

Delete the profiling group:

  1. Open the CodeGuru console.
  2. Choose Profiling groups.
  3. Choose the Tutorial-WithIssues profiling group you created for this tutorial.
  4. Choose Actions, then choose Delete.
  5. Choose Delete.

Congratulations

You have evaluated a running application in the cloud using machine learning with Amazon CodeGuru Profiler. You completed this task by creating a CodeGuru Profiler profiling group, associating the profiling group with a running application, and then reviewing the recommendations using the Amazon CodeGuru console.

Was this tutorial helpful?

Learn more

Learn more about Amazon CodeGuru Profiler by reading the Amazon CodeGuru Profiler User Guide.

Learn about Amazon CodeGuru Reviewer

Amazon CodeGuru Reviewer uses program analysis and machine learning to detect potential defect in source code.

Explore other machine learning services tutorials