CodeGuru is a service from AWS that can automatically analyze your code for common performance issues and other bugs like race conditions and memory leaks. If you’re paying for the server it runs on, wasted CPU time is wasted money.
What Does CodeGuru Do?
Basically, CodeGuru is a machine-learning algorithm trained and tuned to provide recommendations to improve performance of your applications. CodeGuru scans your codebase to find problematic lines of code that can be optimized to prevent wasted CPU cycles. This feature is called the CodeGuru Profiler.
In addition to finding performance issues, CodeGuru also has another feature, the CodeGuru Reviewer, which is trained on application best practices and can catch common problems like resource leaks, race conditions, or problematic error handling.
Of course CodeGuru isn’t meant to completely replace human code reviews, and it isn’t meant to do UI testing in any form. However, it will catch the things that humans may miss, and can do automated testing on every area of your application.
CodeGuru, like most of AWS, has pay-as-you-go pricing, and is low enough to be run for every code review. For a typical pull request with 500 lines of code, AWS estimates that it would only cost $3.75 to run CodeGuru Reviewer on it. Obviously that will add up depending on how many pull requests you have, but for what it offers and the potential compute savings, CodeGuru will pay for itself for many people.
Unfortunately, CodeGuru currently only supports Java applications. However, more languages are planned to be supported soon, and it will work largely the same. If you’re reading this in the future, you can check their website to see which languages are supported.
Using the Profiler
Head over to the CodeGuru Management Console, and select “Profiling Groups” from the sidebar. You can create your own here, but there are two groups already made that you can explore to see how it works. We’ll open up the one “With Issues” to take a look.
On the main screen, you’ll see a graph of all the time the CPU spends executing various functions. The flat tops are areas where the CPU is doing work in a particular function, and not spent calling other methods.
For example, compared to other the demo repository without issues, the code runs quicker and spends more time doing work.
If you click on the “Recommendations” button, you’ll be presented with a list of things CodeGuru finds that can be optimized. For example, in the demo with issues, 10% of the time was spent creating
ObjectMappers, which are slow to create and should only be created once as a result. CodeGuru recommends initializing this as a static variable to solve this issue.
CodeGuru will tell you which functions contain the problematic code, so you can narrow down the problem and get to fixing it quickly.
As for testing your own applications, it’s pretty easy. Create a new profiling group, and you’ll be given instructions on how to integrate the profiler in your application, either by running your JVM with a special
-javaagent parameter, or by integrating it directly into your app.
Either way, start your application and run the profiler. After 5 minutes, the first profile will be submitted, which may take up to 15 minutes to process.
For the code reviewer, which gives non-performance recommendations, you’ll simply need to link your repository to CodeGuru under “Associated Repositories.” After that, you’ll see code reviews for every pull request.