⭐⭐⭐⭐⭐ ML-Powered Code Intelligence

šŸ¤– Amazon CodeGuru

CodeGuru uses machine learning to analyze your code and your running application. It finds performance bottlenecks, identifies expensive code paths, and recommends specific fixes — automatically.

šŸ”‘ Covers: CodeGuru Reviewer Ā· CodeGuru Profiler Ā· Application Profiling Ā· Performance Bottlenecks Ā· Recommendations

šŸ¤– What is Amazon CodeGuru?

CodeGuru is an ML-powered developer tool with two distinct services:

šŸ” CodeGuru Reviewer

Analyzes code BEFORE it runs. Integrates with GitHub, CodeCommit, Bitbucket. Reviews pull requests and comments on code that looks problematic: security vulnerabilities, resource leaks, concurrency issues, AWS best practice violations.

Think of it as an automatic expert code review on every pull request.

šŸ“Š CodeGuru Profiler

Analyzes code WHILE it runs. Instruments your running application (on EC2, Lambda, ECS, or on-premises) and collects CPU and memory usage data. Shows which lines of code are consuming the most resources.

Think of it as a continuous performance X-ray of your live application.

šŸ“Š CodeGuru Profiler — Deep Dive

The Profiler is more relevant for Domain 4 (Troubleshooting & Optimization). Here's how it works:

šŸ”§ How Profiler Works — Under the Hood

1

Instrumentation

You add the CodeGuru Profiler agent (library) to your application. For Lambda, there's a special layer. The agent runs alongside your code and samples its execution.

2

Sampling

The agent periodically (every few milliseconds) takes a snapshot of the call stack — which functions are currently executing. Over time, this reveals which functions are called most frequently and consume the most CPU time.

3

Aggregation

Samples are aggregated into a "profiling group" and uploaded to the CodeGuru service. This happens asynchronously with very low overhead (<1% CPU).

4

Analysis and Visualization

CodeGuru visualizes the data as a flame graph (also called a call graph). The wider a bar, the more time is spent in that function. You can see at a glance where your CPU time goes.

5

Recommendations

CodeGuru provides specific, actionable recommendations. Example: "Function processOrders() on line 47 of OrderService.java is consuming 68% of CPU time. Consider caching the result of the database query on line 52."

šŸ”„ Flame Graphs — Reading Performance Data

Flame Graph
A visualization where the x-axis represents time (CPU usage) and each row represents a function in the call stack. The bottom row is the entry point, rows above are functions called by it. Wider = more CPU time spent there. Wide functions at the top of the stack are the actual "hot spots" consuming your CPU. Named "flame" because the visualization often looks like flames.

šŸ“Š What Profiler Identifies

šŸ’° Expensive Code

Lines or functions that consume disproportionate CPU time. Often found to be: inefficient algorithms (O(n²) where O(n) would work), excessive object creation in loops, redundant database calls.

šŸ”„ Redundant Work

Code that repeatedly computes the same result. Example: calling the same database query inside a loop 100 times instead of calling it once and caching the result.

ā±ļø Wait Time

Time spent waiting for I/O (database calls, network calls, disk reads). Helps identify whether your bottleneck is CPU-bound or I/O-bound (different optimization strategies for each).

šŸ“‰ Anomalies

Detects when CPU usage suddenly increases over time — might indicate a memory leak causing garbage collection pressure, or a gradual performance regression.

šŸŽÆ Exam Tip
CodeGuru Profiler = "identify performance bottlenecks in running code" = "find expensive code paths" = "which function is using the most CPU". It's the answer for code-level performance investigation, not infrastructure-level (for infrastructure, use Compute Optimizer).

šŸ” CodeGuru Reviewer — Smart Code Reviews

Reviewer integrates with your source code repositories and reviews code on pull requests (PRs). It uses ML trained on thousands of open source repositories and Amazon's own code to spot issues.

Types of Issues Reviewer Finds

CategoryExamples
AWS Best PracticesNot closing S3 connections, incorrect SDK usage, missing retry logic
Resource LeaksUnclosed database connections, streams not closed in finally blocks
SecurityHardcoded credentials, insecure random number generation, potential injection flaws
ConcurrencyRace conditions, thread safety issues, deadlock risks
Input ValidationMissing null checks, unvalidated user input
šŸ“Œ Example CodeGuru Reviewer Finding
Code: AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); (inside a Lambda handler function)

Reviewer comment: "This creates a new DynamoDB client on every invocation. Move the client initialization to module level so it's reused across warm invocations. This reduces latency by ~50ms per call."

Why this matters: Creating SDK clients is expensive. In Lambda, module-level variables persist across warm invocations.

šŸ”— CodeGuru Integrations

Source Control

GitHub, GitHub Enterprise, GitLab, Bitbucket, AWS CodeCommit — Reviewer analyzes PRs automatically

Languages

Java and Python (Profiler). Java and Python (Reviewer). More languages via community extensions.

Compute

Profiler works on EC2, Lambda (via layer), ECS, EKS, Fargate, and even on-premises JVMs.